Mohan
Mohan

Reputation: 248

Trying to set value inside a textbox in gridview header

I am trying to set the value of a textbox inside the header template of a grid view but not able to do.

<asp:GridView ID="gv" runat="server" 
<Columns>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="ABC">
<HeaderTemplate><asp:TextBox ID="d1" runat="server">ABC</asp:TextBox></HeaderTemplate> 
</asp:TemplateField>
 </Columns>
</asp:GridView>

.cs side trying to set value

gv.HeaderRow.FindControl("d1"). ="DEF"

Expected:textbox with Id="d1" should be set as DEF Actual:Not getting appropriate syntax to bind it after this code gv.HeaderRow.FindControl("d1").

Upvotes: 2

Views: 840

Answers (2)

ArunPratap
ArunPratap

Reputation: 5020

you can do that by adding onRowDataBound event to your grid and write the following code server side

protected void gridView_RowDataBound(object sender,GridViewRowEventArgs e)
{          
 TextBox txtd1= (TextBox)e.Row.FindControl("d1");     
 txtd1.Text="your text";
}

Upvotes: 1

Burak
Burak

Reputation: 196

You can set Text of textbox after gridview binded. Below code sets text value of textbox

gv.DataSource = list;
gv.DataBind();
((TextBox)gv.HeaderRow.FindControl("d1")).Text = "DEF";

Upvotes: 1

Related Questions