2

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 .

lglifesgood
  • 51
  • 1
  • 4

1 Answers1

1

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
  • 6,677
  • 2
  • 31
  • 40