checkbox and viewstatehttp://forums.asp.net/t/1123906.aspx/1?checkbox+and+viewstateThu, 21 Jun 2007 01:19:10 -040011239061762118http://forums.asp.net/p/1123906/1762118.aspx/1?checkbox+and+viewstatecheckbox and viewstate <p>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.</p> &nbsp;pageload <font color="#0000ff" size="2"></font><font color="#0000ff" size="2"> <p>If</font><font size="2"> Page.IsPostBack </font><font color="#0000ff" size="2">Then</p> </font><font size="2"> <p></font><font color="#008000" size="2">'Dim grRow As GridViewRow</p> </font><font size="2"> <p></font><font color="#008000" size="2">'For Each grRow In GridView1.Rows</p> </font><font size="2"> <p></font><font color="#008000" size="2">' Dim ck1 As CheckBox = CType(grRow.FindControl(&quot;Chkerr&quot;), CheckBox)</p> </font><font size="2"> <p></font><font color="#008000" size="2">' Dim txtUniqueID As TextBox = CType(grRow.FindControl(&quot;txtUniqueID&quot;), TextBox)</p> </font><font size="2"> <p></font><font color="#008000" size="2">' ViewState(txtUniqueID.Text) = ck1.Checked.ToString()</p> </font><font size="2"> <p></font><font color="#008000" size="2">'Next</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">Else</p> </font><font size="2">BindGrid(2424, </font><font color="#800000" size="2">&quot;M&quot;</font><font size="2">, 1)</font><font size="2"> <p></font><font color="#0000ff" size="2">End</font><font size="2"> </font><font color="#0000ff" size="2">If</p> </font><font size="2"> <p></font><font color="#008000" size="2">'Protected Sub GridView1_PreRender(ByVal sender As Object, ByVal e As EventArgs)</p> </font><font size="2"> <p></font><font color="#008000" size="2">' Dim grRow As GridViewRow</p> </font><font size="2"> <p></font><font color="#008000" size="2">' For Each grRow In GridView1.Rows</p> </font><font size="2"> <p></font><font color="#008000" size="2">' Dim chkSelect As CheckBox = CType(grRow.FindControl(&quot;Chkerr&quot;), CheckBox)</p> </font><font size="2"> <p></font><font color="#008000" size="2">' Dim txtUniqueID As TextBox = CType(grRow.FindControl(&quot;txtUniqueID&quot;), TextBox)</p> </font><font size="2"> <p></font><font color="#008000" size="2">' If chkSelect.Checked = True Then</p> </font><font size="2"> <p></font><font color="#008000" size="2">' Dim bChecked As Boolean = Boolean.Parse(ViewState(txtUniqueID.Text).ToString())</p> </font><font size="2"> <p></font><font color="#008000" size="2">' chkSelect.Checked = bChecked</p> </font><font size="2"> <p></font><font color="#008000" size="2">' End If</p> </font><font size="2"> <p></font><font color="#008000" size="2">' Next</p> </font><font color="#0000ff" size="2">Sub</font><font size="2"> Check_Clicked(</font><font color="#0000ff" size="2">ByVal</font><font size="2"> sender </font><font color="#0000ff" size="2">As</font><font size="2"> </font><font color="#0000ff" size="2">Object</font><font size="2">, </font><font color="#0000ff" size="2">ByVal</font><font size="2"> e </font><font color="#0000ff" size="2">As</font><font size="2"> EventArgs)</font><font size="2"> <p></font><font color="#0000ff" size="2">Dim</font><font size="2"> ck1 </font><font color="#0000ff" size="2">As</font><font size="2"> CheckBox = </font><font color="#0000ff" size="2">CType</font><font size="2">(sender, CheckBox)</p> </font><font color="#0000ff" size="2">Dim</font><font size="2"> row </font><font color="#0000ff" size="2">As</font><font size="2"> GridViewRow = </font><font color="#0000ff" size="2">CType</font><font size="2">(ck1.Parent.Parent, GridViewRow)</font><font size="2"> <p></font><font color="#0000ff" size="2">Dim</font><font size="2"> currentrow </font> <font color="#0000ff" size="2">As</font><font size="2"> </font><font color="#0000ff" size="2">Integer</font><font size="2"> = row.RowIndex</p> <p></font><font color="#008000" size="2">' Display the primary key value of the selected row.</p> </font><font size="2"> <p>txtmessage.Text = GridView1.DataKeys(currentrow).Value.ToString()</p> <p></font><font color="#0000ff" size="2">If</font><font size="2"> ck1.Checked = </font> <font color="#0000ff" size="2">True</font><font size="2"> </font><font color="#0000ff" size="2">Then</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">If</font><font size="2"> GridView1.DataKeys(currentrow).Value.ToString() &lt;&gt; 21 </font><font color="#0000ff" size="2">Then</p> </font><font size="2"> <p>GridView1.EditIndex = currentrow</p> BindGrid(2424, </font><font color="#800000" size="2">&quot;M&quot;</font><font size="2">, 1)</font><font size="2"> <p></font><font color="#0000ff" size="2">Else</p> </font><font size="2"> <p></font><font color="#008000" size="2">'if it is 21 do that.</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">End</font><font size="2"> </font><font color="#0000ff" size="2">If</p> </font><font size="2"> <p></font><font color="#008000" size="2">'if not checked</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">Else</p> </font><font size="2"> <p></font><font color="#008000" size="2">'if uncheck check box, get out edit mode.</p> </font><font size="2"> <p>GridView1.EditIndex = -1</p> <p>'even tried to gridview1.databind() instead calling bindgrid()</p> BindGrid(2424, </font><font color="#800000" size="2">&quot;M&quot;</font><font size="2">, 1)</font><font size="2"> <p></font><font color="#0000ff" size="2">End</font><font size="2"> </font><font color="#0000ff" size="2">If</p> </font><font size="2"> <p>Fill_DropDown()</p> <p></font><font color="#0000ff" size="2">End</font><font size="2"> </font><font color="#0000ff" size="2">Sub</p> </font> 2007-06-19T19:07:47-04:001762278http://forums.asp.net/p/1123906/1762278.aspx/1?Re+checkbox+and+viewstateRe: checkbox and viewstate <p>I see that you do data binding manually.&nbsp; However, do you bind your controls to data source?&nbsp; </p> <p>If so, it means that you bind data bind GridView twice and you lose values because of this.&nbsp; </p> <p>If you prefer manual data binding remove DataSourceID property and set DataSource property immediately before calling DataBind.&nbsp; Moreover, be sure that you don't call DataBind twice.</p> <p>You may disable ViewState for gridview if you rebind cotntrol on every postback, unless you need it for some other reason.&nbsp; </p> <p>-yuriy</p> 2007-06-19T20:54:00-04:001762298http://forums.asp.net/p/1123906/1762298.aspx/1?Re+checkbox+and+viewstateRe: checkbox and viewstate <p>thank you for replying, can you be more specific where and what shoudl I do?</p> 2007-06-19T21:07:36-04:001762307http://forums.asp.net/p/1123906/1762307.aspx/1?Re+checkbox+and+viewstateRe: checkbox and viewstate <ol> <li>&lt;div mce_keep=&quot;true&quot;&gt;Remove DataSourceID property from your GridView in ASPX&lt;/div&gt; </li><li>&lt;div mce_keep=&quot;true&quot;&gt;Assign DataSource property inside BindGrid by setting<br> GridView.DataSource = YourSQLSource&lt;/div&gt; </li><li>&lt;div mce_keep=&quot;true&quot;&gt;Check that you nevel call BindGrid twice during one postback.&nbsp; (Set breakpoint in debugger and ensure that you reeach it only once after you click something on the webpage)&lt;/div&gt;</li></ol> 2007-06-19T21:15:35-04:001762325http://forums.asp.net/p/1123906/1762325.aspx/1?Re+checkbox+and+viewstateRe: checkbox and viewstate <p>I have checked that I don't have datasourceid in the aspx page, I called that in bindgrid()</p> <p>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.</p> 2007-06-19T21:31:32-04:001762360http://forums.asp.net/p/1123906/1762360.aspx/1?Re+checkbox+and+viewstateRe: checkbox and viewstate <p>In you original code that you posted to this forum you were calling BindGrid after you set EditIndex.&nbsp; If you still do it, it may be a reason why GridView does not switch to editing mode. </p> <p>Regarding the error with loading view state, can you describe the whole sequence starting from fresh loaded page.</p> <p>-yuriy</p> 2007-06-19T22:00:54-04:001762373http://forums.asp.net/p/1123906/1762373.aspx/1?Re+checkbox+and+viewstateRe: checkbox and viewstate <p>I have take it bindgrid() out on checkchanged event. So, I don't have to call twice, because I am calling at page load.</p> <p>&nbsp;But, that time, edit mode is not showing in my gridview&nbsp;when I click checkbox and postbacks,&nbsp;even though on checkchanged event I have <font size="2">GridView1.EditIndex = currentrow , still doesn't show edit mode, it only shows my checkbox I have checked.</font></p> <p><font size="2">&nbsp;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.</font></p> <p>So, feels like I don't need bindgrid on checkchanged event but when I&nbsp;take it out&nbsp; that I don't even see edit mode for fist checkbox.</p> <p><font size="2">&nbsp;</p> </font> 2007-06-19T22:14:39-04:001762387http://forums.asp.net/p/1123906/1762387.aspx/1?Re+checkbox+and+viewstateRe: checkbox and viewstate <p>Well, you are right that you need data binding to make GridView create edit row for the EditIndex .&nbsp; However, if you do data binding twice, the second time checkboxes wil lnot process post data and you lose their state.&nbsp; How do you use your checkboxes?&nbsp; Can you restore their checked state from&nbsp;data source?&nbsp; If so, you can data bind their checked property.&nbsp; 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? </p> 2007-06-19T22:24:38-04:001762410http://forums.asp.net/p/1123906/1762410.aspx/1?Re+checkbox+and+viewstateRe: checkbox and viewstate <p>I just checked on the very simple sample with DataSourceID bound GridView and enabeld viewstate.&nbsp; unfortunatelly, GridView loses both checkboxes value and viewstate of any controls in templated columns if you switch gridview to edit mode. </p> <p>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)</p> 2007-06-19T22:55:08-04:001762466http://forums.asp.net/p/1123906/1762466.aspx/1?Re+checkbox+and+viewstateRe: checkbox and viewstate <p>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?</p> 2007-06-20T00:13:53-04:001762487http://forums.asp.net/p/1123906/1762487.aspx/1?Re+checkbox+and+viewstateRe: checkbox and viewstate <p>I have changed my code and first I go thru and find the checked ones then I want to bind with dropdown</p> <p>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</p> <font size="2"></font><font color="#0000ff" size="2">Dim</font><font size="2"> key </font><font color="#0000ff" size="2">As</font><font size="2"> DataKey</font><font size="2"> <p></font><font color="#0000ff" size="2">Dim</font><font size="2"> row </font><font color="#0000ff" size="2">As</font><font size="2"> GridViewRow</p> </font><font color="#0000ff" size="2">For</font><font size="2"> </font><font color="#0000ff" size="2">Each</font><font size="2"> row </font><font color="#0000ff" size="2">In</font><font size="2"> GridView1.Rows</font><font size="2"> <p></font><font color="#0000ff" size="2">Dim</font><font size="2"> result </font> <font color="#0000ff" size="2">As</font><font size="2"> </font><font color="#0000ff" size="2">Boolean</font><font size="2"> = (</font><font color="#0000ff" size="2">CType</font><font size="2">(row.FindControl(</font><font color="#800000" size="2">&quot;Chkerr&quot;</font><font size="2">), CheckBox)).Checked</p> <p></font><font color="#0000ff" size="2">If</font><font size="2"> (result) </font> <font color="#0000ff" size="2">Then</p> </font><font size="2"> <p>key = GridView1.DataKeys(row.RowIndex)</p> Response.Write(</font><font color="#0000ff" size="2">CType</font><font size="2">(key.Value, </font><font color="#0000ff" size="2">Integer</font><font size="2">))</font><font size="2"> <p></font><font color="#0000ff" size="2">&gt;&gt;&gt;&gt;&gt; (gives me error)&nbsp; For</font><font size="2"> </font><font color="#0000ff" size="2">Each</font><font size="2"> key </font><font color="#0000ff" size="2">In</font><font size="2"> GridView1.Rows</p> <p></font><font color="#0000ff" size="2">If</font><font size="2"> GridView1.DataKeys(row.RowIndex).Value.ToString() &lt;&gt; 21 </font><font color="#0000ff" size="2">Then</p> </font><font size="2"> <p>GridView1.EditIndex = row.RowIndex</p> BindGrid(2424, </font><font color="#800000" size="2">&quot;M&quot;</font><font size="2">, 1)</font><font size="2"> <p></font><font color="#0000ff" size="2">End</font><font size="2"> </font><font color="#0000ff" size="2">If</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">Next</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">End</font><font size="2"> </font><font color="#0000ff" size="2">If</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">Next </p> </font> 2007-06-20T00:53:57-04:001763028http://forums.asp.net/p/1123906/1763028.aspx/1?Re+checkbox+and+viewstateRe: checkbox and viewstate <p>GridView.Rows collection is a collection of a GridViewRow.&nbsp; You try to use variable key which is DataKey.&nbsp; </p> <p>Did you mean&nbsp;&nbsp;</p> <p>for each row in GridView1.Rows? </p> 2007-06-20T07:47:48-04:001764071http://forums.asp.net/p/1123906/1764071.aspx/1?Re+checkbox+and+viewstateRe: checkbox and viewstate <p>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 &lt;&gt;21 I want this row to be edit mode. How can I do that for the second part of code?</p> 2007-06-20T17:04:19-04:001764253http://forums.asp.net/p/1123906/1764253.aspx/1?Re+checkbox+and+viewstateRe: checkbox and viewstate <p>I have tried this way, and even though I set my txtmessage = &quot;&quot; still at the end, I see two values in that text box,why is that?</p> <font color="#008000" size="2"> <p>Dim key As DataKey</p> </font><font size="2"> <p></font><font color="#008000" size="2">'Dim row As GridViewRow</p> </font><font size="2"> <p></font><font color="#008000" size="2">'For Each row In GridView1.Rows</p> </font><font size="2"> <p></font><font color="#008000" size="2">&nbsp;Dim result As Boolean = (CType(row.FindControl(&quot;Chkerr&quot;), CheckBox)).Checked</p> </font><font size="2"> <p></font><font color="#008000" size="2">'If (result) Then</p> </font><font size="2"> <p></font><font color="#008000" size="2">'If txtmessage.Text &lt;&gt; &quot;&quot; Then</p> </font><font size="2"> <p></font><font color="#008000" size="2">'txtmessage.Text = &quot;&quot;</p> </font><font size="2"> <p></font><font color="#008000" size="2">'End If</p> </font><font size="2"> <p></font><font color="#008000" size="2">&nbsp;key = GridView1.DataKeys(row.RowIndex)</p> </font><font size="2"> <p></font><font color="#008000" size="2">&nbsp;Response.Write(CType(key.Value, Integer))</p> </font><font size="2"> <p></font><font color="#008000" size="2">&nbsp;txtmessage.Text = (CType(key.Value, Integer))</p> </font><font size="2"></font><font color="#008000" size="2">&nbsp;'For Each key In GridView1.DataKeys &gt;&gt;&gt; I comment it out that line</font><font color="#008000" size="2"> <p>&nbsp;</p> </font><font size="2"> <p></font><font color="#008000" size="2">&nbsp;If GridView1.DataKeys(row.RowIndex).Value.ToString() &lt;&gt; 21 Then</p> </font><font size="2"> <p></font><font color="#008000" size="2">GridView1.EditIndex = row.RowIndex</p> </font><font color="#008000" size="2"> <p>&nbsp;</p> </font><font size="2"></font><font color="#008000" size="2">End If</font><font color="#008000" size="2"> <p>&nbsp;</p> </font><font size="2"> <p></font><font color="#008000" size="2">End If</p> </font><font size="2"> <p></font><font color="#008000" size="2">Next</p> </font> 2007-06-20T18:38:58-04:001764618http://forums.asp.net/p/1123906/1764618.aspx/1?Re+checkbox+and+viewstateRe: checkbox and viewstate <p>I am not sure what you are asking.. </p> <p><font color="#008000">Response.Write(CType(key.Value, Integer)) - writeys some value to response.&nbsp; where do you expect to get it? </font></p> 2007-06-20T21:47:49-04:001764617http://forums.asp.net/p/1123906/1764617.aspx/1?Re+checkbox+and+viewstateRe: checkbox and viewstate <p>I am not sure what you are asking.. </p> <p><font color="#008000">Response.Write(CType(key.Value, Integer)) - writeys some value to response.&nbsp; where do you expect to get it? </font></p> 2007-06-20T21:47:49-04:001764801http://forums.asp.net/p/1123906/1764801.aspx/1?Re+checkbox+and+viewstateRe: checkbox and viewstate <p><b>HI, jaws1021:&nbsp;</b></p> <p><b>Adding Controls at the Right Time</b><br> 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 <pre class="prettyprint">Page_Load</pre> event handler, causing the controls to be added during the Load stage of the page's lifecycle. This would work fine if we don't need to worry about saving the controls' view state across postbacks, but if we do need to persist the view state of the dynamically added controls the Load stage is not where we should be adding these controls. </P> <P>If we need our dynamically added controls to maintain their view state it is paramount that these controls be added <I>before</I> 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 <pre class="prettyprint">Init</pre> event. <P>If you are using Visual Studio .NET, the code-behind class already contains an event handler for the page's <pre class="prettyprint">Init</pre> event. This event handler (named <pre class="prettyprint">Page_Init</pre> in VB.NET code-behind classes and <pre class="prettyprint">OnInit</pre> in C# code-behind classes) is tucked away in the hidden "Web Form Designer Generated Code" region. What I typically do is create a separate method in the code-behind class and simply call this method from the <pre class="prettyprint">Init</pre> event handler. Once you have configured everything so that the dynamic controls are added during the Initialization stage, you can then read/write the controls' properties in the Load stage. You'll find that the users' interactions with the dynamic controls remains &quot;remembered&quot; across postbacks if you follow this pattern. </p> <p>Here are some corresponding material for you:</p> <p><a href="http://aspnet.4guysfromrolla.com/articles/092904-1.aspx">http://aspnet.4guysfromrolla.com/articles/092904-1.aspx</a></p> <p><a href="http://msdn2.microsoft.com/en-us/library/hbdfdyh7.aspx">http://msdn2.microsoft.com/en-us/library/hbdfdyh7.aspx</a></p> <p>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</p> 2007-06-21T01:19:10-04:00