2011-09-27 47 views
0

我有一个gridview,它在itemtemplate中有一个文本框。我想动态地设置这个文本框的maxlength属性。在gridview中动态设置控制的属性

我现在的代码是 -

<asp:GridView ID="grd" runat="server" EnableViewState="true" AutoGenerateColumns="false" 
        OnRowDataBound="grd_RowDataBound" > 
        <Columns>       
         <asp:TemplateField HeaderText="Textbox"> 
          <ItemTemplate> 
           <asp:TextBox ID="txtValue" Text="" runat="server" TextMode="MultiLine" Columns="8" Rows="3"></asp:TextBox> 
          </ItemTemplate> 
         </asp:TemplateField> 
        </Columns> 
       </asp:GridView> 

我在RowDataBound事件处理程序代码 -

protected void grd_RowDataBound(object sender, GridViewRowEventArgs e) 
     {    
       if (e.Row.RowType == DataControlRowType.DataRow) 
       {      
        TextBox txtText = (TextBox)e.Row.FindControl("txtValue"); 

        txtText.Text = "test"; //this works fine 

        txtText.MaxLength = 10; //this does not work. 
       }   
     } 

有谁知道为什么我不能够动态地设置MaxLength属性?而且,我怎样才能动态地在gridview中设置控件属性的值?

回答

1

多行文本框不能有MaxLength。 虽然你不会得到任何错误,但它不会工作。

您可以尝试将文本框的TextMode更改为SingleLine以查看它是否可行。

+0

如果我的常识占上风,我会省下我花在这上面的时间.. :)谢谢你,你是对的。 – pavanred