【发布时间】: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