Dynamically set properties of contorls in gridview
I have a gridview which has a textbox in an itemtemplate. I want to set the maxlength property of this textbox dynamically.
The code I have now is -
<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>
My code in the RowDataBound event handler -
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.
}
}
Does anyone know why I am not able to set the MaxLength property dynamically? And, how can I set the value of a property of the control in gridview dynamically?
A multiline textbox can't have a MaxLength. Although you wont get any error but it won't work.
You can try changing the TextMode of textbox to SingleLine to see if it would work.
精彩评论