1
votes

I have a GridView that is bound to an ObjectDataSource. I add a textbox and a button to each row in the grid to allow the user to enter a date and click the button to subscribe. No field exists in GridView for this textbox and return null refrence when run this page. What should I do? please help.. Here's the ASPX file and the code behind:

<asp:TemplateField HeaderText="content" ItemStyle-Width="150"> <ItemTemplate> <asp:TextBox ID="Name" runat="server" /> </ItemTemplate> <ItemStyle Width="150px"></ItemStyle> </asp:TemplateField> <asp:ButtonField CommandName="Select" Text="save" ButtonType="Button" />

Code-Behind:

 protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
    GridViewRow row = GridView1.Rows[e.NewSelectedIndex];

    string s = row.Cells[0].Text;
            //Fetch value of Name.

    TextBox tb = (TextBox)GridView1.FindControl("Name");
    string name = tb.Text;
}

value of name is null...

2

2 Answers

0
votes

GridViewRow also has FindControl method. You may try,

protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
    GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
    TextBox tb = row.FindControl("Name") as TextBox;
    String name = tb.Text;
}
0
votes

you can do it in the row command event also .try this..

add OnRowCommand="gvProducts_RowCommand" then in code behind

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
     if (e.CommandName.Equals("Select"))
        {
         GridViewRow row = GridView1.Rows[e.RowIndex];
         TextBox tb = (TextBox)row.FindControl("Name");
         string name = tb.Text;
         }
     }