I've been banging my head on this for quite a while. I've read and read and I'm not really finding anything that is leading me to where I want to go....
on the ASPX page I create a GridView and AutoGenerate the Columns and the Select Column.
On the page Load event I bind the GridView to a List<T> Datasource.
On the SelectedIndexChanged Event
I create a new List<T> that is a origional of what was put in on the GridView in the page load.
I insert a blank row in the List<T> at the desired SelectedIndex
I rebind the GridView to the List<T> that I inserted the blank row.
I than add controls to that Gridview at the desired row and the desired Cells.
Everything generates as expected, but I can't get the Button Click Event to fire for the life of me.
All I am trying to do is when the user selects the select button I want to create a new row right below that to add in Text and hit a Save button.
Right now all of the below code is sitting in my OnSelectedIndexChanged Event.
TextBox txtEdit = new TextBox();
Button myButton = new Button();
List<T>.Insert(myIndex, new List<T> Item);
GridView1.DataSource = List<T>;
GridView1.DataBind();
GridView1.Rows[myIndex].Cells[0].Text = "<p><b>I'm able to do something here</p></b>;
GridView1.Rows[myIndex].Cells[1].Controls.Add(txtEdit);
//myButton.Click += new EventHandler(this.myButton_Click);
myButton.CommandName = "Update";
//myButton.CommandArgument
GridView1.Rows[myIndex].Cells[1].Controls.Add(myButton);
GridView1.Rows[myIndex].Attributes.Add("class", "row-drop");
Because you are Creating the Button Dynamically , you need to create the Button on every PostBack , otherwise the events associated with it will not fire..
Adding to that: RowDataBound doesn't fire on every request. (In particular, it doesn't fire on PostBack, unless you call DataBind) RowCreated does fire on every request. But you won't always have DataItem available in that. If your added Control doesn't
depend on data, RowCreated is the way to go.
Also: If you must do DataBind in Page_Load do so only when IsPostBack is false. (Instead of doing it in Page_Load, I suggest handling the DataBinding event of the GridView instead. In the handler, all you have to do is set the DataSource. Calling DataBind
won't be necessary, as DataBinding is already in progress. No need to test IsPostBack in the DataBinding handler.)
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
rusty741
0 Points
1 Post
Adding Button to the GridView Row
Jun 19, 2012 09:20 PM|LINK
Hi Guys,
I've been banging my head on this for quite a while. I've read and read and I'm not really finding anything that is leading me to where I want to go....
on the ASPX page I create a GridView and AutoGenerate the Columns and the Select Column.
On the page Load event I bind the GridView to a List<T> Datasource.
On the SelectedIndexChanged Event
I create a new List<T> that is a origional of what was put in on the GridView in the page load.
I insert a blank row in the List<T> at the desired SelectedIndex
I rebind the GridView to the List<T> that I inserted the blank row.
I than add controls to that Gridview at the desired row and the desired Cells.
Everything generates as expected, but I can't get the Button Click Event to fire for the life of me.
All I am trying to do is when the user selects the select button I want to create a new row right below that to add in Text and hit a Save button.
Right now all of the below code is sitting in my OnSelectedIndexChanged Event.
TextBox txtEdit = new TextBox(); Button myButton = new Button(); List<T>.Insert(myIndex, new List<T> Item); GridView1.DataSource = List<T>; GridView1.DataBind(); GridView1.Rows[myIndex].Cells[0].Text = "<p><b>I'm able to do something here</p></b>; GridView1.Rows[myIndex].Cells[1].Controls.Add(txtEdit); //myButton.Click += new EventHandler(this.myButton_Click); myButton.CommandName = "Update"; //myButton.CommandArgument GridView1.Rows[myIndex].Cells[1].Controls.Add(myButton); GridView1.Rows[myIndex].Attributes.Add("class", "row-drop");sushanth009
Contributor
6243 Points
1168 Posts
Re: Adding Button to the GridView Row
Jun 19, 2012 11:50 PM|LINK
Because you are Creating the Button Dynamically , you need to create the Button on every PostBack , otherwise the events associated with it will not fire..
Make sure you create them on every PostBack...
AmalO.Abdull...
Contributor
3116 Points
764 Posts
Re: Adding Button to the GridView Row
Jun 20, 2012 06:29 AM|LINK
try to see your click button event in rowcommand event if not
try to add this button in your grid view with commandName="Update" but set it's visible to false
and whenever you need it set it's visible to true and again the click event will be fired on rowCommand Event
just check that
if e.commandName =="Update";
try it and let me know.
superguppie
All-Star
48225 Points
8679 Posts
Re: Adding Button to the GridView Row
Jun 25, 2012 09:01 AM|LINK
Adding to that: RowDataBound doesn't fire on every request. (In particular, it doesn't fire on PostBack, unless you call DataBind) RowCreated does fire on every request. But you won't always have DataItem available in that. If your added Control doesn't depend on data, RowCreated is the way to go.
Also: If you must do DataBind in Page_Load do so only when IsPostBack is false. (Instead of doing it in Page_Load, I suggest handling the DataBinding event of the GridView instead. In the handler, all you have to do is set the DataSource. Calling DataBind won't be necessary, as DataBinding is already in progress. No need to test IsPostBack in the DataBinding handler.)
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.