2

I am using a gridview to display a table queried from a database...

In this gridview I also added a buttonfield, and 3 template fields...

When i press the buttonfield the data from the database is acquired, but the manual data inserted from the template fields are not.. It seems that null values are acquired.

From the xml file i am firing the following event:

OnRowCommand="GridView1_SelectedIndexChanged1"

and have the following method to catch that event:

protected void GridView1_SelectedIndexChanged1(object sender, GridViewCommandEventArgs e)
        {
            int x = Convert.ToInt32(e.CommandArgument);
            GridView1.SelectRow(x);

            GridViewRow row = GridView1.Rows[x];

            Response.Write(row.Cells[0].Text + "\t");
            Response.Write(row.Cells[1].Text + "\t");
            Response.Write(row.Cells[2].Text + "\t");
            Response.Write(row.Cells[3].Text + "\t");
            Response.Write(row.Cells[4].Text + "\t");
            Response.Write(row.Cells[5].Text + "\t");
            Response.Write(row.Cells[6].Text + "\t");
            Response.Write(row.Cells[7].Text + "\t");
            Response.Write(row.Cells[8].Text + "\t");  
           }

Any ideas on how i can get the values from that template field? do i need to catch another event rather than GridViewCommandEventArgs? If so what event should i throw from the xml part?

Thanks,

cgval
  • 1,096
  • 5
  • 14
  • 31

2 Answers2

0

I am fighting with the same, I used to use the Cells[] code with asp BoundField. Now i had to switch to template fields as I need buttons etc in the footer. I am pulling my hair out, I use the same code as suggested by Ashley, and I get System.NullReferenceException: Object reference not set to an instance of an object.

I am doing this within the row command event. Been this way since 3 days :(

Edit: Quick Solution to my issue: I've created the ID given Field on the footer, and I had not given and ID to my ItemTemplate, i.e the only thing I had in there was <%# Eval("Description")%> There looks like to be NO WAY to select this data without giving it some kind of ID in code behind. I was stupid and had not noticed this simple fact later on.

regeter
  • 1,442
  • 1
  • 11
  • 12
0

You can find the control u added on to the row and then use the text property to get the value of it. Something similar to row.FindControl(<urIdgoeshere>)\\cast it to textBoxand then use .Text to get the value of it .

GridViewRow row = GridView1.Rows[x];

Response.Write((TextBox)row.FindControl('txtbox1')).Text;

Ashley John
  • 2,379
  • 2
  • 21
  • 36
  • I've done something siliar to this and it is still not working : TextBox my = ((TextBox)row.FindControl("numTC")); Response.Write(my.Text); It's returning an empty string... I think that since when i am pressing the buttonfield, the page sort of reloads and all data that was entered in the template fields are cleared.. Maybe that the problem is that as soon as the edit button is pressed, all templatefields are cleared and the event is triggered with empty tempaltefields? – cgval Jul 26 '11 at 08:08
  • U have your viewstate enabled on your page?Are you re-binding your gridview on Page_Load? – Ashley John Jul 26 '11 at 09:08