Home All Groups Group Topic Archive Search About

HELP!! Dynamically created webcontrol not accessible on postback

Author
12 May 2005 4:26 PM
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..

Author
13 May 2005 8:36 AM
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.
Author
14 May 2005 7:46 AM
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?

Show quoteHide quote
"Josh" <s@a.com> wrote in message
news:e2ckec5VFHA.2984@tk2msftngp13.phx.gbl...
> 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.
>
Author
18 May 2005 3:11 PM
vuive4ly
Please let me know if you found the solution, I'm facing the same problem... don't know how to retrieve checked value. -- vuive4ly ------------------------------------------------------------------------ Posted via http://www.mcse.ms ------------------------------------------------------------------------ View this thread: http://www.mcse.ms/message1610540.html
Author
18 May 2005 4:28 PM
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 -- vuive4ly ------------------------------------------------------------------------ Posted via http://www.mcse.ms ------------------------------------------------------------------------ View this thread: http://www.mcse.ms/message1610540.html
Author
18 May 2005 11:24 PM
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" :)

-Brock
DevelopMentor
http://staff.develop.com/ballen