473,238 Members | 1,591 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,238 software developers and data experts.

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

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
1 4078
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Cary | last post by:
Hi all, I seem to be losing my mind here. I can't get this to work and I know it must be something stupid. I have an asp page with a html checkbox on it. I have a button that calls a...
1
by: John Mullen | last post by:
I want to take the following HTLM and use javascript to turn on radio buttons if checkbox is checked, can I do this with javascript (maybe onClick or an array) or do i need a server side script ?...
4
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! How do I convert a CheckBox.Checked to an int? Regards Martin Arvidsson
4
by: Kevin | last post by:
Hi, I have a input with type checkbox. I want it to be automatically checked if the value from the corresponding field in the database is also checked. I tried this: <% set objdc =...
3
by: Johann Blake | last post by:
Is this a bug in the C# compiler when it parses code, or is there a legitimate reason why the following code is legal. If so, explain why. On a form, add an option button (radio button). Call it...
4
by: RodBillett | last post by:
I have a situation where I have 3 checkboxes - and at least 1 needs to be selected at all times... I have implemented the code that allows this behavior to happen, BUT Viewstate gets all messed...
1
by: Eric via .NET 247 | last post by:
Hello, I'm dynamicly filling a table with checkboxes, doing this: ... CheckBox chkBox = new CheckBox(); chkBox.ID = "chk" + i.ToString(); chkBox.AutoPostBack=true; ...
3
by: nologo | last post by:
All, Have a problem i just cant find the answer for. I'm trying to populate a colum in a SQL database. Basically the column requires a 1 or 0. I'm trying to populate this col via a c#...
1
by: =?Utf-8?B?amV6MTIzNDU2?= | last post by:
Hi Experts I have a windows form with a TreeView control with the CheckBoxes property set to True. When I check a node, I want that node and all it's child nodes to be checked and expanded. I've...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.