属性在asp .net TextBox中消失

时间:2015-11-08 19:48:52

标签: html asp.net webforms

我有以下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属性奇怪地消失,任何想法?

1 个答案:

答案 0 :(得分:2)

<%# ...

是数据绑定标记

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

 <%= ... 

编辑:

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>