Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jun 25, 2012 09:01 AM by superguppie
Jun 19, 2012 09:20 PM|LINK
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.Rows[myIndex].Cells.Text = "<p><b>I'm able to do something here</p></b>;
//myButton.Click += new EventHandler(this.myButton_Click);
myButton.CommandName = "Update";
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...
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.
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.)