HELP!! Dynamically created webcontrol not accessible on postback

T

Tim Greenwood

I am using the calendar control provided for ASP.NET. I am adding a
checkbox during the DayRender event as follows:

System.Web.UI.WebControls.CheckBox cb = new
System.Web.UI.WebControls.CheckBox();

cb = new System.Web.UI.WebControls.CheckBox();

cb.EnableViewState = true;

cb.ID="p" + e.Day.Date.ToShortDateString().Replace("/","_");

cb.CheckedChanged += new System.EventHandler(this.CheckBox_CheckedChanged);

cb.Text = "Play";

e.Cell.Controls.Add(cb);



I have a command button for submitting the changes but cannot figure out how
to access the dynamically created checkboxes. The WebCalendar does not
expose the table object it creates so how would I be able to access the
values of these checkboxes??

I've kind of reached my end here...any help would be most appreciated..
 
J

Josh

On Each and every post back you need to reload dynamic controls. The
viewstate is posted back to the server but the interpreter cant put it
anywhere because the controls dont exist.

In the Stateless environs those controls dont persist, you have to recreate
them. The data in the controls does persist. its in the Viewstate.
 
F

Fred Hirschfeld

I am aware that that technique will get you the viewstate back but how does
that get the value entered by the user? ViewState is the state that was sent
to the user not what is recieved.

Does the framework also take the values of the post and load it into the
dynamic controls?
 
V

vuive4ly

Please let me know if you found the solution, I'm facing the same
problem... don't know how to retrieve checked value.
 
V

vuive4ly

This works for me, I was able to store Date in my Array if the checkbox
is checked

'--------------------------------------
Dim Arr(31) as string
Sub calendarCS_DayRender(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DayRenderEventArgs) Handles
calendarCS.DayRender
If e.Day.IsOtherMonth = False Then
Dim MyCheckbox As New CheckBox
Dim i As Integer = e.Day.DayNumberText
Dim chkID As String = "D" & i
MyCheckbox.ID = chkID
If arr(i) = e.Day.DayNumberText Then
MyCheckbox.Checked = True
Else
MyCheckbox.Checked = False
End If
MyCheckbox.EnableViewState = True
e.Cell.Controls.Add(MyCheckbox)
End If
End Sub

'-----------------------------------
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSubmit.Click
Dim c As Control
Dim i As Integer = 31
For i = 1 To 31
Dim D As New CheckBox
Dim strID As String = "D" & i
Dim strIDFound As String = Request.Form(strID)
If strIDFound = "on" Then
arr(i) = i
lblError.Text += arr(i) & " "
End If
Next
End Sub
'------------------------------------

Hope it works for you guys too
 
B

Brock Allen

I am aware that that technique will get you the viewstate back but how
does that get the value entered by the user? ViewState is the state
that was sent to the user not what is recieved.

Does the framework also take the values of the post and load it into
the dynamic controls?

When you dynamically recreate the controls and add them back into the server
control tree, they will load their viewstate and the post data the user had
entered. So the answer to your question is "yes" :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top