How can I bind data to a textbox which is in a templatefield of a gridview ? I want to use ExecuteScalar , get a value and throw it to that textbox .
views:
42answers:
2
A:
Basically you create a method to return your value and then call it in the databinding expression. Take a look at this example:
In your aspx page call the function GetValue in the data binding expression:
<asp:GridView ID="GridTest" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtValue" Width="200px" runat="server" Text='<%#GetValue((int)Container.DataItem)%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Then in your code behind you have the function to get the value:
protected void Page_Load(object sender, EventArgs e)
{
GridTest.DataSource = new List<int>{1, 2, 3};
GridTest.DataBind();
}
protected string GetValue(int ID)
{
return "Value from Execute Scalar " + ID;
}
Chris Mullins
2010-06-23 14:45:36
A:
Use gridview rowdatabound event, like...
protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((TextBox)e.Row.FindControl("yourTextboxID")).Text = "Throw new Value here";
}
}
Muhammad Akhtar
2010-06-23 14:52:12