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.
pageload
If
Page.IsPostBack Then
'Dim grRow As GridViewRow
'For Each grRow In GridView1.Rows
' Dim ck1 As CheckBox = CType(grRow.FindControl("Chkerr"), CheckBox)
' Dim txtUniqueID As TextBox = CType(grRow.FindControl("txtUniqueID"), TextBox)
' ViewState(txtUniqueID.Text) = ck1.Checked.ToString()
'Next
Else
BindGrid(2424, "M", 1)
End If
'Protected Sub GridView1_PreRender(ByVal sender As Object, ByVal e As EventArgs)
' Dim grRow As GridViewRow
' For Each grRow In GridView1.Rows
' Dim chkSelect As CheckBox = CType(grRow.FindControl("Chkerr"), CheckBox)
' Dim txtUniqueID As TextBox = CType(grRow.FindControl("txtUniqueID"), TextBox)
' If chkSelect.Checked = True Then
' Dim bChecked As Boolean = Boolean.Parse(ViewState(txtUniqueID.Text).ToString())
' chkSelect.Checked = bChecked
' End If
' Next
Sub Check_Clicked(ByVal sender
As Object,
ByVal e As EventArgs)
Dim ck1 As CheckBox =
CType(sender, CheckBox)
Dim row As GridViewRow =
CType(ck1.Parent.Parent, GridViewRow)
Dim currentrow
As Integer = row.RowIndex
' Display the primary key value of the selected row.
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.
-yuriy
Marked as answer by Rex Lin - MSFT on Jun 22, 2007 06:01 AM
<div mce_keep="true">Check that you nevel call BindGrid twice during one postback. (Set breakpoint in debugger and ensure that you reeach it only once after you click something on the webpage)</div>
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.
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.
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.
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?
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)
jaws1021
Member
50 Points
189 Posts
checkbox and viewstate
Jun 19, 2007 07:07 PM|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.
pageloadIf
Page.IsPostBack Then 'Dim grRow As GridViewRow 'For Each grRow In GridView1.Rows ' Dim ck1 As CheckBox = CType(grRow.FindControl("Chkerr"), CheckBox) ' Dim txtUniqueID As TextBox = CType(grRow.FindControl("txtUniqueID"), TextBox) ' ViewState(txtUniqueID.Text) = ck1.Checked.ToString() 'Next Else BindGrid(2424, "M", 1) End If 'Protected Sub GridView1_PreRender(ByVal sender As Object, ByVal e As EventArgs) ' Dim grRow As GridViewRow ' For Each grRow In GridView1.Rows ' Dim chkSelect As CheckBox = CType(grRow.FindControl("Chkerr"), CheckBox) ' Dim txtUniqueID As TextBox = CType(grRow.FindControl("txtUniqueID"), TextBox) ' If chkSelect.Checked = True Then ' Dim bChecked As Boolean = Boolean.Parse(ViewState(txtUniqueID.Text).ToString()) ' chkSelect.Checked = bChecked ' End If ' Next Sub Check_Clicked(ByVal sender As Object, ByVal e As EventArgs) Dim ck1 As CheckBox = CType(sender, CheckBox) Dim row As GridViewRow = CType(ck1.Parent.Parent, GridViewRow) Dim currentrow As Integer = row.RowIndex ' Display the primary key value of the selected row.txtmessage.Text = GridView1.DataKeys(currentrow).Value.ToString()
If ck1.Checked = True Then If GridView1.DataKeys(currentrow).Value.ToString() <> 21 ThenGridView1.EditIndex = currentrow
BindGrid(2424, "M", 1) Else 'if it is 21 do that. End If 'if not checked Else 'if uncheck check box, get out edit mode.GridView1.EditIndex = -1
'even tried to gridview1.databind() instead calling bindgrid()
BindGrid(2424, "M", 1) End IfFill_DropDown()
End SubViewState asp.net View state Problem
ysw
Contributor
2784 Points
515 Posts
Re: checkbox and viewstate
Jun 19, 2007 08:54 PM|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.
-yuriy
jaws1021
Member
50 Points
189 Posts
Re: checkbox and viewstate
Jun 19, 2007 09:07 PM|LINK
thank you for replying, can you be more specific where and what shoudl I do?
ysw
Contributor
2784 Points
515 Posts
Re: checkbox and viewstate
Jun 19, 2007 09:15 PM|LINK
GridView.DataSource = YourSQLSource</div>
jaws1021
Member
50 Points
189 Posts
Re: checkbox and viewstate
Jun 19, 2007 09:31 PM|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.
ysw
Contributor
2784 Points
515 Posts
Re: checkbox and viewstate
Jun 19, 2007 10:00 PM|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.
-yuriy
jaws1021
Member
50 Points
189 Posts
Re: checkbox and viewstate
Jun 19, 2007 10:14 PM|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.
ysw
Contributor
2784 Points
515 Posts
Re: checkbox and viewstate
Jun 19, 2007 10:24 PM|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?
ysw
Contributor
2784 Points
515 Posts
Re: checkbox and viewstate
Jun 19, 2007 10:55 PM|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)
jaws1021
Member
50 Points
189 Posts
Re: checkbox and viewstate
Jun 20, 2007 12:13 AM|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?