【问题标题】:How to get html height in code behind?如何在后面的代码中获取html高度?
【发布时间】:2017-03-05 19:46:41
【问题描述】:

我想设置我的“b”文本框的高度并将其设置为我的“a”高度。

这是我目前在后面的代码中获取高度的代码。

Dim a As TextBox = CType(e.Item.FindControl("txtNote1"), TextBox)
Dim b As TextBox = CType(e.Item.FindControl("txtNote2"), TextBox)

Dim aHeight as Integer =  a.Height.Value
b.Height = aHeight

但它只返回 0px。如果我没有为我的文本框设置静态值,如何获取 html 值?

【问题讨论】:

  • 如果您的文本框的高度是动态的或在客户端发生变化,服务器不会知道它,因为默认情况下该值不会发送到服务器。在回发之前,您必须确保在表单元素中设置了高度值,例如隐藏输入。
  • 哦,好吧,我现在明白了,但问题是,在进行回发之前,我将如何获取高度值并在表单元素中设置?

标签: html css asp.net vb.net


【解决方案1】:

试试这个,

<asp:TextBox ID="a" runat="server" Height="30px"></asp:TextBox>  
<asp:TextBox ID="b" runat="server"></asp:TextBox> 

code behind

b.Height = a.Height.Value

【讨论】:

  • 不幸的是,我的 textarea 的高度是空白的。因为我的身高是不可预测的,或者应该说是动态的
【解决方案2】:

您可以将 TextBox 的高度设置为 100% 并使用 JavaScript 操作围绕 TextBox 的 &lt;div&gt;。这样你就不需要后面代码中的确切高度了。

<div style="height:400px;" id="div_01">
    <asp:TextBox ID="TextBox1" runat="server" Height="100%" TextMode="MultiLine"></asp:TextBox>
</div>

<div style="height:200px;" id="div_02">
    <asp:TextBox ID="TextBox2" runat="server" Height="100%" TextMode="MultiLine"></asp:TextBox>
</div>

<script type="text/javascript">
    document.getElementById("div_02").style.height = document.getElementById("div_01").style.height;
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    相关资源
    最近更新 更多