Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Table Webcontrol and PostBack

Status
Not open for further replies.

ChainsawJoe

Programmer
Dec 5, 2000
154
GB
Hi all,

Need some pointers please - I'm dynamically creating a Table Webcontrol (i.e., *not* a Table HTMLControl) and adding checkboxes into some of the TDs. The "checked" status is being set depending on the current values in certain objects.

I'm then allowing the user to change the status of these checkboxes and click a "Save" button to update the values.

Obviously, since these are dynamically created content then the data is lost in the postback. I can add the table to the viewstate, but I won't even see the "updated" data, since nothing is posted on submission.

How would you suggest I handle this? How can I get the user input (i.e., the user unchecked checkbox for CustomerID #1 and AddressID #15 (not the IDs I'm using, but easier to demonstrate with) and checked the checkboxes for CustomerID #3, AddressIDs #24 and #27)?

Should I be using a little javascript to save a string in a couple of hidden input fields to define which chkbxes are checked and which are not?

Extra info: the table does not map directly to any object structure.

Many thanks,

CJ
 
>>Should I be using a little javascript to save a string in a couple of hidden input fields to define which chkbxes are checked and which are not?

BINGO! but i would suggest that you track just the changed checkboxes. no use in keeping the entire table in viewstate...

Known is handfull, Unknown is worldfull
 
The easiest method is to just create all the controls on each load of the page (changes to control properties will automatically be save in ViewState for you).


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Thanks peeps - vbkris, I went with the hidden field method and this has worked for me.

ca8msm - your method maybe well have worked, so I'll give it a shot next time ;)

Thanks again! :)
 
Mark's suggestion does work.. I have used it many times before, and it is easier since you don't have to worry about setting and getting values from hidden fields.
 
Ok, so just to confirm this method: I have a sub/func called from page_load which creates the dynamic content - perhaps using a parameter to specify whether to instansiate the data from the database or not (such that I'm not overwriting submitted values)?

Is that correct?
 
If you are creating dynamic controls, always put that code, or a call to that code in the Page_Init event. This will prevent possible viewstate problems.
 
>>If you are creating dynamic controls, always put that code, or a call to that code in the Page_Init event

that was exactly why i did not suggest that method. in this case the poster wants to read just the change in the checked statusof the checkbox.

If we go through the control creation routine then:
a. recreate the entire table in init event and then wait till the page_load event
b. read through each control of the table in page_load event to identify the changes
c. tracking changes is very difficult as the previous values are not available.

Known is handfull, Unknown is worldfull
 
You shouldn't rely on a javascript method to perform your logic. By all means use it to enhance your user's experience, but you should always use server-side code as your underlying basis for logic.

Tracking changes in this scenario doesn't have to be difficult. For example, you could write out a hidden input to the page and then compare it against the current selections when the page is posted back.

Other alternatives could be:

1) Loop through each row and run an insert for each customer
2) Reset all the displayed id's to "off" in the database, and then set the ones that are checked to "on".

Personally, I'd go with the tracking changes option by using the hidden element.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Thanks all, but to reiterate my problem (which I've now used javascripting to overcome) and to address Mark's point:
you could write out a hidden input to the page and then compare it against the current selections when the page is posted back.
with my dynamically created content, nothing is available in the postback so I can't compare the changes. This is how I had been trying to do the functionality from the beginning, but when I found the dynamic content "vanished" upon postback I had to come up with another option.
This is also the case for your other point
2) Reset all the displayed id's to "off" in the database, and then set the ones that are checked to "on".
I don't know which ones are checked, since that info is lost!


Thanks again all, but the use of javascript and hidden fields was the solution I went for.

CJ
 
with my dynamically created content, nothing is available in the postback so I can't compare the changes. This is how I had been trying to do the functionality from the beginning, but when I found the dynamic content "vanished" upon postback I had to come up with another option.
This is also the case for your other point
That isn't true. When you create dynamic controls, you need to do so on every load of the page. If you then check/uncheck a control, it's state will be stored in viewstate. If you also wrote out a hidden input at the same time, you could compare this posted value with the current selections.

Go with your javascript solution if you want, but you should be aware that it may not work for all users whereas a server-side method will work 100% of the time.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Also, here's a visual example that will prove my above statementto be correct:
Code:
		<form id="Form1" method="post" runat="server">
			<asp:Panel id="Panel1" runat="server"></asp:Panel>
			<asp:Button ID="Button1" Runat="server" Text="Postback"></asp:Button>			
		</form>
Code:
    [COLOR=blue]Private[/color] [COLOR=blue]Sub[/color] Page_Load([COLOR=blue]ByVal[/color] sender [COLOR=blue]As[/color] [COLOR=blue]Object[/color], [COLOR=blue]ByVal[/color] e [COLOR=blue]As[/color] System.EventArgs) [COLOR=blue]Handles[/color] [COLOR=blue]MyBase[/color].Load
        [COLOR=green]' Declarations[/color]
        [COLOR=blue]Dim[/color] cbPrevious [COLOR=blue]As[/color] [COLOR=blue]New[/color] CheckBox
        [COLOR=blue]Dim[/color] cbCurrent [COLOR=blue]As[/color] [COLOR=blue]New[/color] CheckBox

        [COLOR=green]' Dynamic Controls[/color]
        cbPrevious.ID = "cbPrevious"
        cbPrevious.Text = "Previous Value"
        cbPrevious.Enabled = [COLOR=blue]False[/color]
        cbCurrent.ID = "cbCurrent"
        cbCurrent.Text = "Current Value"

        [COLOR=green]' Add controls[/color]
        Panel1.Controls.Add(cbPrevious)
        Panel1.Controls.Add(cbCurrent)
    [COLOR=blue]End[/color] [COLOR=blue]Sub[/color]

    [COLOR=blue]Private[/color] [COLOR=blue]Sub[/color] Button1_Click([COLOR=blue]ByVal[/color] sender [COLOR=blue]As[/color] [COLOR=blue]Object[/color], [COLOR=blue]ByVal[/color] e [COLOR=blue]As[/color] System.EventArgs) [COLOR=blue]Handles[/color] Button1.Click
        [COLOR=green]' Has the value been changed?[/color]
        [COLOR=blue]If[/color] [COLOR=blue]CType[/color](Panel1.FindControl("cbPrevious"), CheckBox).Checked = [COLOR=blue]CType[/color](Panel1.FindControl("cbCurrent"), CheckBox).Checked [COLOR=blue]Then[/color]
            Response.Write("The value didn't change")
        [COLOR=blue]Else[/color]
            Response.Write("The value has been changed")
        [COLOR=blue]End[/color] [COLOR=blue]If[/color]
        [COLOR=green]' Write out the previous value[/color]
        [COLOR=blue]CType[/color](Panel1.FindControl("cbPrevious"), CheckBox).Checked = [COLOR=blue]CType[/color](Panel1.FindControl("cbCurrent"), CheckBox).Checked
    [COLOR=blue]End[/color] [COLOR=blue]Sub[/color]


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top