【问题标题】:Attribute disappears in asp .net TextBox属性在asp .net TextBox中消失
【发布时间】:2015-11-08 19:48:52
【问题描述】:

我有以下 asp .net 标记:

<asp:TextBox CssClass="siteinput required" ID="TextTitle" runat="server" Width="100%" MaxLength='<%# int.Parse(Request.QueryString["id"]) == 49 ? 40 : 15 %>' placeholder="Title" required="required"></asp:TextBox>

但生成的标记是:

<input name="ctl00$MainContent$TextTitle" type="text" id="MainContent_TextTitle" class="siteinput required" placeholder="Title" required="required" style="width:100%;" />

MaxLength 属性奇怪地消失了,有什么想法吗?

【问题讨论】:

  • 你试过Convert.ToInt32()而不是int.Parse() / Int32.Parse()。可能是您的 id 不是有效整数
  • 它是有效整数,这肯定不是问题

标签: html asp.net webforms


【解决方案1】:
<%# ...

是数据绑定标签

如果您没有绑定任何东西并进行一些计算,请尝试以下操作。

 <%= ... 

编辑:

public int MaxLengthById
{
    get

    {
       //Check the QueryString before parse
        return int.Parse(Request.QueryString["id"]) == 49 ? 40 : 15;

    }
}

protected void Page_Load(object sender, EventArgs e)
{
   //option 1  
   TextTitle.MaxLength = MaxLengthById;

   //option 2 
   TextTitle.DataBind();

}

标记面;

选项 1 完全删除 MaxLength 属性。它将在页面加载时添加。

<asp:TextBox CssClass="siteinput required" ID="TextTitle" runat="server" Width="100%" placeholder="Title" required="required"></asp:TextBox>

选项 2 用变量名绑定你的属性

<asp:TextBox CssClass="siteinput required" ID="TextTitle" runat="server" Width="100%" MaxLength=<%# MaxLengthById %>  placeholder="Title" required="required"></asp:TextBox>

【讨论】:

  • 我需要根据查询字符串更改值,将#替换为=没有解决
  • 哦,好吧,那是一个服务器端元素。对不起。这里有两个选项;如果您在计算中仅设置一个属性,请在页面加载等时设置服务器端的值或放置 html 输入元素。
  • 我知道它可以设置在代码后面,这就是我真正所做的,但我的问题是,如果它可以在不使用任何代码的情况下完成
  • 无论如何感谢您的信息,感谢您的努力
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-15
  • 2017-06-10
  • 1970-01-01
  • 1970-01-01
  • 2010-10-20
  • 2014-08-18
相关资源
最近更新 更多