Last post Jun 21, 2007 02:19 AM by Rex Lin - MSFT
Jun 19, 2007 08:07 PM|jaws1021|LINK
I have gridview and dynamic checkboxes in there, when user clicks one of the checkboxes, gridview's that row goes to edit mode and also filters data inside the dropdownlist and populates dropdownlist. I have created oncheckchanged() event handler that if
fires when someone clicks the checkbox, but when it is more than one checkbox clicks , after postback doesn't remember the previous values of the other checkbox. What I need to do? I am including commented ones so you can see what I even tried, didn't work.
txtmessage.Text = GridView1.DataKeys(currentrow).Value.ToString()
GridView1.EditIndex = currentrow
GridView1.EditIndex = -1
'even tried to gridview1.databind() instead calling bindgrid()
View state Problem
Jun 19, 2007 09:54 PM|ysw|LINK
I see that you do data binding manually. However, do you bind your controls to data source?
If so, it means that you bind data bind GridView twice and you lose values because of this.
If you prefer manual data binding remove DataSourceID property and set DataSource property immediately before calling DataBind. Moreover, be sure that you don't call DataBind twice.
You may disable ViewState for gridview if you rebind cotntrol on every postback, unless you need it for some other reason.
Jun 19, 2007 10:07 PM|jaws1021|LINK
thank you for replying, can you be more specific where and what shoudl I do?
Jun 19, 2007 10:15 PM|ysw|LINK
Jun 19, 2007 10:31 PM|jaws1021|LINK
I have checked that I don't have datasourceid in the aspx page, I called that in bindgrid()
Ihave take bindgrid out inside the checkchanged event and now, it can remmeber checkbox value, but doesn't go to edit mode, and when I check diffrent checkbox it gives me error , failed to load viewstate.
Jun 19, 2007 11:00 PM|ysw|LINK
In you original code that you posted to this forum you were calling BindGrid after you set EditIndex. If you still do it, it may be a reason why GridView does not switch to editing mode.
Regarding the error with loading view state, can you describe the whole sequence starting from fresh loaded page.
Jun 19, 2007 11:14 PM|jaws1021|LINK
I have take it bindgrid() out on checkchanged event. So, I don't have to call twice, because I am calling at page load.
But, that time, edit mode is not showing in my gridview when I click checkbox and postbacks, even though on checkchanged event I have
GridView1.EditIndex = currentrow , still doesn't show edit mode, it only shows my checkbox I have checked.
BUT, when I left Bindgrid() on the checkchanged() event that time I was getting my editmode along with whatever checkbox I have checked, but only thing was It was not remember previous checkbox values. For ex. If I have checked first checkbox,
postbacks and shows me edit mode of the first checkbox, then I go ahead and check second checkbox, it only remembers and shows me second checkbox, not first one, I want to see both.
So, feels like I don't need bindgrid on checkchanged event but when I take it out that I don't even see edit mode for fist checkbox.
Jun 19, 2007 11:24 PM|ysw|LINK
Well, you are right that you need data binding to make GridView create edit row for the EditIndex . However, if you do data binding twice, the second time checkboxes wil lnot process post data and you lose their state. How do you use your checkboxes?
Can you restore their checked state from data source? If so, you can data bind their checked property. If you cannot do this, I would walk all checkboxes before calling second DataBind and restore their state on second databinding. Do you think it is feasible?
Jun 19, 2007 11:55 PM|ysw|LINK
I just checked on the very simple sample with DataSourceID bound GridView and enabeld viewstate. unfortunatelly, GridView loses both checkboxes value and viewstate of any controls in templated columns if you switch gridview to edit mode.
I cannot find aney easy workaround, so I think the best is to find a way to set the correct checked state on second databinding. (perhaps as I suggested earlier to save and restore their state)
Jun 20, 2007 01:13 AM|jaws1021|LINK
Are you serious, Oh my God, I have been tried to do that past couple of days. What is your second suggestion, how can I do that?
Jun 20, 2007 01:53 AM|jaws1021|LINK
I have changed my code and first I go thru and find the checked ones then I want to bind with dropdown
So, I have done this and it does give me two key values, if I checked two checkboxes, but on my second for each loop, it is giving me error, because probably I didn't set that correct format
key = GridView1.DataKeys(row.RowIndex)
GridView1.EditIndex = row.RowIndex
Jun 20, 2007 08:47 AM|ysw|LINK
GridView.Rows collection is a collection of a GridViewRow. You try to use variable key which is DataKey.
Did you mean
for each row in GridView1.Rows?
Jun 20, 2007 06:04 PM|jaws1021|LINK
since my first part of code gives me two or more keys, I need to see if values of each key = 21 or not if it is not <>21 I want this row to be edit mode. How can I do that for the second part of code?
Jun 20, 2007 07:38 PM|jaws1021|LINK
I have tried this way, and even though I set my txtmessage = "" still at the end, I see two values in that text box,why is that?
Dim key As DataKey
Jun 20, 2007 10:47 PM|ysw|LINK
I am not sure what you are asking..
Response.Write(CType(key.Value, Integer)) - writeys some value to response. where do you expect to get it?
Jun 21, 2007 02:19 AM|Rex Lin - MSFT|LINK
Adding Controls at the Right Time
We already know that when adding controls dynamically through the page's code portion the controls must be added on every postback. But when in the page lifecycle should the controls be added? At first guess, we might decide to put such code in the
If we need our dynamically added controls to maintain their view state it is paramount that these controls be added before the Load View State stage. That is, these controls must exist within the page's control hierarchy before the view state is loaded. There's only one stage before Load View State - Initialization. That means, if we want our dynamic controls to persist view state we must add them to the control hierarchy in the page's
If you are using Visual Studio .NET, the code-behind class already contains an event handler for the page's
Here are some corresponding material for you:
I hope the above information will be helpful. If you have any issues or concerns, please let me know. It's my pleasure to be of assistance