By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,933 Members | 1,243 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,933 IT Pros & Developers. It's quick & easy.

Dynamic Checkbox problem when added in render method &checked=true

P: n/a
This is one of the weirdest problems I have ever run into. I have had to
trim down a bunch of code to give a sample that is more easily readable by
those who will view this. Here is the problem:

I dynamically add an htmlcheckbox to a webform in the pages render and set
the checked value to true. When the page loads, if I remove the check from
the checkbox and then submit it, in the submit event the checkbox' checked
value is still true. However if I orignally set the checked value to false in
the render event, load the webform, check the checkbox and submit the page,
in the submit event, the checkbox's value is set to true which is how I would
expect it to work.

In the example I am including, you will see 2 checkboxes are added in an if
statement. This statement is so when the form loads for the first time, I
can control having the checkboxes being added in the render event first
instead of in the load and then the render (this is to emulate my situation
and show the problem). The first checkbox is set to true and doesn't work
properly, while the second is originally set to false and does work properly.

This seems extremely odd that the one originally set to false works properly
yet the one originally set to true does not. Is this a problem and is there
a way to fix it.

This problem only appears when the page is loaded the first time and
submitted the first time. After this it works properly and I would guess
this has something to do with the second time through loading in the
page_load first instead of the render method first.

For debugging purposes, set a break on the mbtnRefresh_ServerClick method
and then watch the following values:
mtblMain.Rows(0).Cells(0).Controls(0).ID
ctype(mtblMain.Rows(0).Cells(0).Controls(0), HtmlInputCheckBox).Checked
mtblMain.Rows(1).Cells(0).Controls(0).ID
ctype(mtblMain.Rows(1).Cells(0).Controls(0), HtmlInputCheckBox).Checked
'CODE BEHIND

Imports System.Web.UI.HtmlControls

Partial Class _Default

Inherits System.Web.UI.Page

Private WithEvents mbtnRefresh As New HtmlInputSubmit

Private mobjTest As Object

Private mtblMain As New HtmlTable

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

Dim ltblMain As New HtmlTable

mobjTest = HttpContext.Current.Session("mobjTest")

mbtnRefresh.ID = "btnRefresh"

form1.Controls.Add(mbtnRefresh)

form1.Controls.Add(mtblMain)

ListLayers()

End Sub

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)

HttpContext.Current.Session("mobjTest") = New Object

mobjTest = HttpContext.Current.Session("mobjTest")

'Had to add this to the render section so we can obtain the new layers from

'the MapViewer which is obtained during prerender

ListLayers()

MyBase.Render(writer)

End Sub

Private Sub ListLayers()

Dim lcbxTemp As HtmlInputCheckBox

Dim ltcTEmp As HtmlTableCell

Dim ltrTemp As HtmlTableRow

mtblMain.Controls.Clear()

If mobjTest IsNot Nothing Then

ltrTemp = New HtmlTableRow

ltcTEmp = New HtmlTableCell

lcbxTemp = New HtmlInputCheckBox

lcbxTemp.ID = "test"

lcbxTemp.Checked = True

ltcTEmp.Controls.Add(lcbxTemp)

ltrTemp.Controls.Add(ltcTEmp)

mtblMain.Controls.Add(ltrTemp)

ltrTemp = New HtmlTableRow

ltcTEmp = New HtmlTableCell

lcbxTemp = New HtmlInputCheckBox

lcbxTemp.ID = "test2"

lcbxTemp.Checked = False

ltcTEmp.Controls.Add(lcbxTemp)

ltrTemp.Controls.Add(ltcTEmp)

mtblMain.Controls.Add(ltrTemp)

End If

End Sub

Protected Sub mbtnRefresh_ServerClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles mbtnRefresh.ServerClick

End Sub

End Class

'WEBFORM

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
Inherits="_Default" %>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

</form>

</body>

</html>

Jul 4 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
A bit more info:

What am I trying to do:
I am working on a mapping solution that has two controls. One is a map
control and the other is a layers control. When the form first loads, I
don''t know which layers the map control has available to it (I have
simplified this above in the code that checks to see if mobjTemp isNot
nothing) thus I cannot create the check boxes. When I get to the render of
the layer control the map control now knows the layers that are available and
thus I can create the checkboxes to turn layers on and off.

The steps to follow:

1. Add a breakpoint on the serverclick event for the button

2. Load the form (this runs the form load which does not create the text
boxes the first time through because mobjTest is nothing. The render method
will then run which sets the mobjTest value thus creating the 2 checkboxes
where the firstone is checked true and the second is checked false)

3. Click on the first checkbox to remove the checkmark and click on the
second checkbox to add the checkmark.

4. Click submit. This will end up running the load event again. This time
however, mobjTest is valid thus the checkboxes render. If I fail to create
the checkboxes here, I am unable to obtain the value submitted from the form
when the click event fires.

5. In the serverclick event check the values of the checkboxes. You will
see that the checkbox originally set to false is now true, but the checkbox
originally set to true is still true. This is incorrect.

This problem only appears when the page is loaded the first time and
submitted the first time. After this it works properly.

For debugging purposes, set a break on the mbtnRefresh_ServerClick method
and then watch the following values:
mtblMain.Rows(0).Cells(0).Controls(0).ID
ctype(mtblMain.Rows(0).Cells(0).Controls(0), HtmlInputCheckBox).Checked
mtblMain.Rows(1).Cells(0).Controls(0).ID
ctype(mtblMain.Rows(1).Cells(0).Controls(0), HtmlInputCheckBox).Checked

When I want to layers on and off, i do so and click a refresh button. In
the load event I must recreate my checkboxes so that I may later obtain the
submitted values in the serverclick event. When the serverclick event fires
I take the layer values and tell the map which layers have been turned on and
off. Here lies my problem. The first time a checkbox is dynamically added
to the screen, its value is set to true and not originally created in the
load event, setting the value to false will not be the submitted value.
However if set to false when rendered it works fine.

After the first load it works fine because after the first submit, you have
now created that control in the load event.


Jul 5 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.