【问题标题】:Getting a Javascript value with ASP.NET Webforms [closed]使用 ASP.NET Webforms 获取 Javascript 值 [关闭]
【发布时间】:2019-08-14 09:02:42
【问题描述】:

我正在尝试获取 PX 中文本的宽度,我在 Javascript 中成功地做到了这一点。我现在想将此值传递给 C#。

<p id="Test">This is where I measure the text in PX</p>    
</br>

<!-- JS showing the value -->
<input type="hidden" id="widthOutput" runat="server" />
</br> 

<!-- Button to execute the C# -->
<asp:Button ID="calc" OnClick="calc_Click" runat="server" Text="Button" />
</br>   

<!-- C# showing the value -->
<asp:Literal ID="output" runat="server"></asp:Literal>

还有 Javascript,它确实返回正确的值,但只在控制台中

var fontSize = 12;
var test = document.getElementById("Test");
test.style.fontSize = fontSize;
var height = (test.clientHeight + 1) + "px";
var width = (test.clientWidth + 1) + "px"
document.getElementById("widthOutput").Value = width;
console.log(height, width);

还有我的 C#

protected void calc_Click(object sender, EventArgs e)
{
output.Text = widthOutput.Value;
}
  • 我已经在 Stack Overflow 上寻找答案,这让我稍微改变了我的 JS,但仍然没有运气。对 C# 来说还是比较新的,在 JS 方面很糟糕,如果它很简单,请道歉。

【问题讨论】:

    标签: javascript c# asp.net webforms


    【解决方案1】:

    在您的 JS 代码中,您使用的是 .Value 而不是 .value(区分大小写)

    【讨论】:

    • 哇,太尴尬了。它现在正在工作。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多