2011-09-27 44 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