HELP!! Dynamically created webcontrol not accessible on postback

Discussion in 'ASP .Net Web Controls' started by Tim Greenwood, May 12, 2005.

  1. 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..
    Tim Greenwood, May 12, 2005
    #1
    1. Advertising

  2. Tim Greenwood

    Josh Guest

    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.
    Josh, May 13, 2005
    #2
    1. Advertising

  3. 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?

    "Josh" <> wrote in message
    news:...
    > 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.
    >
    Fred Hirschfeld, May 14, 2005
    #3
  4. Tim Greenwood

    vuive4ly Guest

    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
    vuive4ly, May 18, 2005
    #4
  5. Tim Greenwood

    vuive4ly Guest

    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
    vuive4ly, May 18, 2005
    #5
  6. Tim Greenwood

    Brock Allen Guest

    > 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
    Brock Allen, May 19, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Greenwood
    Replies:
    1
    Views:
    853
    Teemu Keiski
    May 13, 2005
  2. Mark Rae
    Replies:
    3
    Views:
    560
    =?Utf-8?B?amlt?=
    Sep 24, 2005
  3. Jeff User
    Replies:
    0
    Views:
    331
    Jeff User
    Nov 15, 2006
  4. msimmons
    Replies:
    0
    Views:
    460
    msimmons
    Jul 16, 2009
  5. Cathy

    Dynamically Added User Control not accessible!

    Cathy, Dec 16, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    116
    Cathy
    Dec 16, 2003
Loading...

Share This Page