Saturday, March 31, 2012

Table Web Control

I'm using a Table web control to dynamically create checkboxes, and check/uncheck/enable/disable them based on values returned from different datasources. In this case, I'm using several DataTables.

I've completed the table on load, and all of the checkbox controls load up as expected, however I now need to submit any changes based on the users values. I've been googling for a good article on the Table web control but haven't found anything that will help. Does anyone know of any articles, or have sample code for maintaining state of these controls and iterating through them so I can create my dynamic SQL based on selections?

Thanks.From the SDK:

"Note: Programmatic additions or modifications to a table row or cell will not persist across posts to the server. Table rows and cells are controls of their own, not properties of the Table control. Changes to table rows or cells must be reconstructed after each post to the server."

Ok, so I think that answers my viewstate problem. I DO recreate this table on page load everytime, so I don't think I need to worry about that.

Any help on muddling through the selected checkboxes of a dynamically created table web control, would still be very beneficial to my cause.
Ugh, I'm fried here...

The viewstate will STILL be a problem, because I wasn't thinking that with each postback, my table would just be re-created, therefore I won't get the modified values on submission.

I keep talking to myself but oh well...

Just to reiterate, I need help on maintaing state for a dynamically created table web control, and how to iterate through the controls that were created in that table on postback.
Dear Forum,

Hi! It's me again!

Ok, so after a little more research I found that people are suggesting the Request.Form() to collect information about the controls. Does that sound right? I guess if you can't take advantage of viewstate in this situation, then Request.Form() is the way to go.

Taking that into consideration, I don't think I can really "iterate" through the controls in my dynamic table, but maybe create some variables that will hold the number of checkbox controls say, when I create the table. So maybe it would look something like this:

pseudocode -
' Creating the table
Dim cb as CheckBox
Dim cbCounter as Int32
'Dynamically create checkbox here and increment the checkbox counter

Then later when I retireve the values within my table...

'Go through each of the checkboxes and determine the value
for x=0 to cbCounter.Length - 1
'check the value of checkbox "x" here

Does this sound like a logical resolution? Am I taking this too far and there is an easier path that I'm missing here? Does anyone really care at this point? :)
Hi FrankWhite,

I'm having the same problem here. I'm fetching values from a MySQL database, I show them in a nice table with checkboxes. But when the user clicks "submit" my checkboxes are lost (since my table won't keep them).
So I'm looking for the same solution: how can I keep all generated checkboxes, so I can check which of them are checked by the user?

If someone has experience with this scenario and knows how it can be done, please post it here for me and FrankWhite ;)

Thanks in advance!
1. Try to stay away from dynamically creating controls that need to keep their values during postbacks.

2. Modify the way you search for information. More than 50% of programming involves just trying to find out how to do stuff. Your problem is not with viewstate, but with the ASP.NET lifecycle.

To answer both your question, you will need some patients.This article will answer your questions, but you have to read every word carefully. DONT skip to the end and read the conclusion as you will miss out on important things that you should learn.

Jagdip
(Haven't read the article yet)

I did come to a solution the other day. Basically on Page_Load my table is recreated everytime. When the submit button is pressed, I simply do Request.Form() on the checkbox controls. I don't have the code in front of me at the moment, but I basically did the following to solve my problem.

1.) Created a variable that would serve as a counter for my checkboxes.
2.) When the checkbox was created, I would give it the id: "chk_" & chkCtrls (variable that was the counter)
3.) In the btn_submit() I created a loop to iterate through each of the checkbox controls (well not really the controls, rather the # of controls). In the loop I would check if the checkbox was checked and do something according to that:

for x = 0 to chkCtrls
if Request.Form("chk_" & x) = "On" Then
'do something
end if
next

There was a lot more to it then this for my situation, but I think this will give you a good idea. Let me know if this helps, or if you figured out a different solution. In the meantime, I'll need to peep this article...
I used the same solution, I recreated my checkbox collection each time in Page_load, and used Request.Form to check wether the "check all" was checked or unchecked to set a default checked value on each checkbox (true/false). Then on the submit button I used Request.Form again to check the status of each generated checkbox by checking for controls like "chk" + intID.
This does the trick I needed very well.

I'll take a look at the article as well, in case this is not the right way of programming this :)

Thanks for the help,
Speerman
Alright, alright, I'll give you the jist of it. I'm not an expert in this (just been reading around a lot) so here goes.
Basically, the ASP.NET life cylce loads your info like this:

Page_Init
Load ViewState
Page_Load
Run any controls functions (e.g. button_click functions, etc.)

From this you can see that if you create controls dynamically, then they will be displayed on the page; but their viewstate will not be 'loaded'. What we need is a way to create the controls before the Load Viewstate state.

Solution: If you are using VS, the you know there is a region called " Web Form Designer Generated Code ". In this you will find:


'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

All you need to do is call the function that creates the dynamic controls from within the Page_Init function. That's it (i think - havent got a chance to try it).

But please, read the article. You will have a better understanding of ASP.NET which means better pages (and maybe a pay rise - you can tell why I read the aticles ;-)

HTH
Jagdip

0 comments:

Post a Comment