No Data on postback

Andy Green
Andy Green used Ask the Experts™
on
This has been confusing me for a couple of days now, can anyone offer any suggestions:

I have an ajax based page with several controls on. One of these controls is a check box list. This is populated with n number of check boxes when the page runs.

This is the code and it works OK, it is called from the page load inside a is.page postback check and only runs on the first time the page loads:
        Dim x As New JobPlan
        Dim NoOfWeeks As DataSet = x.WeekCycleByJobPlan(Session("JobPlan_ID"))

        cblWeek.DataSource = NoOfWeeks
        cblWeek.DataTextField = "Week"
        cblWeek.DataValueField = "Week"
        cblWeek.DataBind()

I check to see  if any check boxes have been check with this serverside code

        'set up a loop to check if one of the weeks has been checked
        For a = 0 To cblWeek.Items.Count - 1
            If cblWeek.Items(a).Selected Then
                flgWeek = True
            End If
        Next a

What is happening is this, and  I'm looking in the page load event so its the first thing that happens - on the initial postback the flgWeek always returns false with one checked, but on the next postback there is data.

Like I said the control is set up on the initial page load, and nothing is running in code that I can see to change the checkboxlist on the firat postbackm with out data to put data in on the second postback.

Any ideas

Andy
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Shaun KlineLead Software Engineer

Commented:
It sounds like you are running into an issue with the page life cycle. See here: http://msdn.microsoft.com/en-us/library/ms178472.aspx.
For checking if values are set, I suggest using the LoadComplete event (with the Is Postback check).

Author

Commented:
Update
This looks like a problem with the Checkboxlist control - I've looked at the HTTP requests with Fiddler, and on the first postback even with  a check box ticked, it is not in the traffic to the server, but on the second postback it does.
Hmmm!!!

Author

Commented:
Same using Load complete I'm afraid - The page needs a postback after load to wire up the checkboxlist, just looking to see why.
Andy
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Author

Commented:
This is getting sillier - I have put an onselectedindexchanged on the control, and it doesn't fire after page load, but it does after the first postback.
So why would the control render but not wire up any events until there is a postback.
Andy
Shaun KlineLead Software Engineer

Commented:
Control events are only fired from the UI when a user causes that event to fire (selecting a checkbox, clicking a button). If you are selecting a value for the checkboxlist in the code-behind page, it's event will not fire, and you would need to call the event directly.

Author

Commented:
I'm checking a box from the UI, but the event is not there on page load, only after a postback. I can confirm this with Fiddler.
Andy
Shaun KlineLead Software Engineer

Commented:
Are you setting the event handler using the ASPX page (setting the OnSelectedIndex property for the control's definition) or are you creating the event handler in the code-behind page (using += new eventhandler())? Also, is the AutoPostBack property set for the control? I believe this property must be set for the checkboxlist to fire events.

Author

Commented:
I'm setting it in the axpx page - here is my mark up.
 <asp:CheckBoxList ID="cblWeek" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow" TextAlign="Left" AutoPostBack="true" OnSelectedIndexChanged="PressSelection" ></asp:CheckBoxList>
I'm about out  strip out all the ajax (quite a job) to see if that is my problem.
Andy
Shaun KlineLead Software Engineer

Commented:
Is your checkboxlist control in an UpdatePanel? If it is, you may need to add the PostBack trigger to it.

Author

Commented:
Yes it is in an Update panel  -I'll do some checking thatnks.
Andy

Author

Commented:
I've removed all the update panels and ajax, but it is the same. I have narrowed down the problem, if I create a fixed checkboxlist with a fixed number of boxes, it works.
Its the database code behind created one that the problem. I've run this with all panels and ajax and get the same result so its the way I'm creating the checkboxlist thats the problem.
Markup is shown in OP.
Andy
Lead Software Engineer
Commented:
I was able to take your markup from comment #26455285 and code from your original post, but using a simple connection object, data adapter and data set, and the postback occurred correctly. I has the checkboxlist in an update panel and the postback occurred both with the trigger on the update panel and without it. Can you do the same...basically start with a simple page and work your way back to what you have?

Author

Commented:
Found it.
Inside the  If Not Page.IsPostBack for the page load event I have
Me.cblWeek.Controls.Clear() This appears before the code that sets up the control, and is to ensure that the contol in empty ???? I know if its just been created it will be.
This code is not run on postback, but removing it worked. Thanks for your help.
Andy

Author

Commented:
Thanks again
Andy

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial