473,406 Members | 2,343 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,406 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 4085
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.