【问题标题】:Using (static)HTML Controls in Code-Behind, Coding with C#/ASP.NET在代码隐藏中使用(静态)HTML 控件,使用 C#/ASP.NET 进行编码
【发布时间】:2012-07-06 04:43:51
【问题描述】:

我正在使用 ASP.NET 中的 C# 在 VS10 中工作。在我的设计表单中,我有一个 textArea(标准 HTML 控件)。

 <textarea id="Text1"  rows = "8"; cols="30" onkeyup="AutoGrowTextArea(this)"   name="S1"> </textarea>

现在在我的代码隐藏页面中,我使用 C# 对控件进行编码。我已经为 textArea 提供了一个自动扩展功能,这是我在项目中需要的。我需要这个 TextArea 作为服务器控件,就像我们在设计页面中放置一个文本框一样,我们可以在代码隐藏页面中使用它来编码,因为它是一个服务器控件。但是,textArea 不是服务器控件。 我已经浏览了网站上以前的帖子,但我没有得到足够有用的东西。 我什至尝试过使用 [] 系统和 [runat="server"] 但它没有帮助。 我想做的是在代码隐藏页面中使用 textArea,即在编码空间中调用它,就像我们可以调用 TextBox 控件对象一样。 所以,任何人都可以帮我解决这个问题,,, 问候.. 自动增长文本框的 javascript 是:

  <script type="text/javascript"> 

function AutoGrowTextArea(textField)

{

if (textField.clientHeight < textField.scrollHeight) 

{

 textField.style.height = textField.scrollHeight + "px";

 if (textField.clientHeight < textField.scrollHeight)

{

textField.style.height = (textField.scrollHeight * 2 - textField.clientHeight) + "px";

} 

}

} 

</script>      

【问题讨论】:

  • 删除row="8"之后的;

标签: c# asp.net html


【解决方案1】:
<asp:TextBox id="thisIsMyTextBox" runat="server" multiline="true" width="200px" onkeyup="AutoGrowTextArea(this)" height="80px" style="resize:none;"></asp:TextBox>

您可以随意使用宽度/高度!

函数 AutoGrowTextArea(textField)

{

   if(textField.Lenght()%50==0)//50 is the number of character in your textbox

   {

            if (textField.clientHeight < textField.scrollHeight) 

            {

                    textField.style.height = textField.scrollHeight + "px";

                    if (textField.clientHeight < textField.scrollHeight)

                    {

                             textField.style.height = (textField.scrollHeight * 2 - textField.clientHeight) + "px";

                    } 

     }

}

【讨论】:

  • 嗯,我试过了。我有 javascript:
  • 做你说的只是增加每次按键时文本区域的高度,我只是想增加高度,在行尾[facebook评论系统]
  • 我写的那行应该可以正常工作,我认为是你的 javascript 不能正常工作,可能会在你的主要帖子中添加那段代码,以便我可以缩进研究它?
  • JavaScript 已经写好了 :)
  • 如果 textbox%30==0(执行其余的 javascript 代码),我会为此使用模数。我正在编辑我的帖子
【解决方案2】:

您可以在标记中添加 runat="server",它将成为服务器控件。

您还可以在您的代码中创建服务器控件。声明(在 Visual Basic 中):

Protected WithEvents foo As Global.System.Web.UI.HtmlControls.HtmlGenericControl = Global.System.Web.UI.HtmlControls.HtmlGenericControl("textarea")

如果你想在你的所有项目中使用某些东西,你可以声明一个从 Control 或 HtmlGenericControl 继承的类,你可以在你的新控件的任何地方实现你需要实现的任何东西。

【讨论】:

    【解决方案3】:

    您可以设置控件的 runat="server" 属性来操作原始 html 控件。

     <textarea id="Text1" runat="server"  rows = "8"; cols="30" onkeyup="AutoGrowTextArea(this)"   name="S1"> </textarea>
    

    【讨论】:

      【解决方案4】:

      试试这个

      在标签中添加runat="server"

      <textarea id="Text1" runat="server"  rows = "8" cols="30" onkeyup="AutoGrowTextArea(this)" name="S1"> </textarea>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-22
        • 2014-11-18
        • 2012-03-19
        • 1970-01-01
        • 2012-12-04
        • 2011-11-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多