Solved

ASP.NET - keeping checkbox's state when in datagrid

Posted on 2002-06-17
10
396 Views
Last Modified: 2008-03-10
PLEASE HELP ME!!!!!!!!

I am creating a datagrid and populating it dynamically, no problem, and I have embedded checkboxes in the first 2 columns.

When the user clicks a button on the page, I am iterating through the datagrid to check the value of the checkboxes.

I have no problem referencing the checkboxes, but as soon as the button is pressed, they lose state.

Can I get the values from them before the button causes postback? the rest of the fields hold their state during postback, as does any checkbox created outside the datagrid.

Their 'holdstate' (can't remember the actual name, but you get the drift) property is set to true.

Could I just bind the checkbox to the cell it sits in?

Can I handle the statechange of the checkbox? (I dont seem to be able to do it normally)

HELP!!!!!

If you can give me the working answer to any of these (or a decent work around) the points are yours, if it's particularly neat, I may give you some more...
0
Comment
Question by:monkey3
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 22

Expert Comment

by:CJ_S
Comment Utility
Two options.
Either make those checkbox items part of the form, or loop through the checkboxes and store their values in a hidden field (to read them out on the server and then reset the checkboxes after loading.

Please note, I don't use ASP.Net yet so I'm not really sure.

CJ
0
 
LVL 20

Expert Comment

by:Silvers5
Comment Utility
You can trigger an event upon clicking on each of the checkboxes to set a hidden field in the form..

I don't have .Net here to past you an example but I guess you got the idea.. It's derived from CJ so if you got it split between us.. :o)
0
 
LVL 1

Author Comment

by:monkey3
Comment Utility
as far as on clicking goes, I tried that and they don't call the method. Again, if the checkbox is outside the grid it works. If I could handle the onclick, then I could simply store the checkbox values in their containing grid cells.

which method(s) can I use before the checkboxes lose their state?

and how would I make the checkboxes part of the form, as I don't know how many rows there are going to be in the grid?
0
 

Expert Comment

by:edyang75
Comment Utility
I'm afraid you may have accidentally set AutoPostBack of your checkbox to true. If it is set to true, it will cause page sumit action (the whole page reloads from the Web server!) and because it is in a DataGrid template column, its checked state won't be remembered!
0
 
LVL 1

Author Comment

by:monkey3
Comment Utility
I thought the same!,
but nope.

autopostback is set to false.

it seems that no control ***within*** a grid holds state.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Expert Comment

by:edyang75
Comment Utility
You should get their CHECKED state (not the VALUE) without problem using client-side script.

And any controls created DYNAMICALLY using client-side script must be processed using the old Form("control_name") way in order to get their states (values).

Hope this will give you some help.
0
 
LVL 2

Accepted Solution

by:
Check earned 200 total points
Comment Utility
I think your problem relates to the order that the events are fired off.  This is the same problem that bites me fairly regularly.  Let me see if I can run through what I believe to be your problem:

Page begins to load

All objects in the page are created

Code behind executes

Your code behind creates the checkbox objects

Page is delivered to the browser.

-----
The user then clicks on some of the checkboxes and then clicks your button. This causes a postback.
-----
Page begins to load

All objects in the page are created.  

All values are set for the objects on the page.

Code behind executes

Your code behind creates the checkbox objects

Your code for the button executes and tests the values of the checkboxes.

Your code behind exits and the .Net framework processes the viewstate information and sets the correct properties for the dynamically created objects on the page

Page is delivered to the browser.
----

As you can see the problem is that the checkboxes don't get their values set until after your code has checked their values.  Unfortunatly since your code has just finished creating them, they have no value.  You can see what I mean if you run through the debugger and see when they come in to existance and when their value actually gets set.  This problem usually only happens with dynamically created controls.

The workaround for this is to use the Request.Params("checkboxID") to test for the values of the checkboxes.

(This may not be the exact order things happen but this is most likly what is causing the problem.  Hits me all the time)

Check

0
 
LVL 2

Expert Comment

by:Check
Comment Utility
On the off chance that we're missing something simple, You ARE giving your dynamically created checkboxes unique ID's right?  You can verify what they're called by doing a view source on the page once it makes it to the browser.

Check
0
 
LVL 1

Author Comment

by:monkey3
Comment Utility
I do believe you are correct. I'd actually put this problem on the back burner due to the headache it was causing.

I was giving the check boxes unique ids. like you say, it was just the order in which things were happening.

Thank you very much for your help.

m3.
0
 

Expert Comment

by:fportela
Comment Utility
This suggestion might come a little late as I see that this question was posted quite some time ago.

I suspect that there was a binding being done in the pageload event on EVERY pageload.
Having an if isPostBack statement should resolved the issue and the checkboxes will retain their state as opposed as being reset as part of the binding process.

The code should look something like this:

  If Not (Page.IsPostBack) Then
    loadCollection()
    dg_subsidiaries.DataSource = somethingCollection
    dg_subsidiaries.DataBind()
    Session("subsidiaries") = somethingCollection
  Else
    somethingCollection = Session("subsidiaries")
  End If
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now