473,888 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic Checkbox problem when added in render method &checked=tru e

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_Ser verClick method
and then watch the following values:
mtblMain.Rows(0 ).Cells(0).Cont rols(0).ID
ctype(mtblMain. Rows(0).Cells(0 ).Controls(0), HtmlInputCheckB ox).Checked
mtblMain.Rows(1 ).Cells(0).Cont rols(0).ID
ctype(mtblMain. Rows(1).Cells(0 ).Controls(0), HtmlInputCheckB ox).Checked
'CODE BEHIND

Imports System.Web.UI.H tmlControls

Partial Class _Default

Inherits System.Web.UI.P age

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.EventArg s)
Handles Me.Load

Dim ltblMain As New HtmlTable

mobjTest = HttpContext.Cur rent.Session("m objTest")

mbtnRefresh.ID = "btnRefresh "

form1.Controls. Add(mbtnRefresh )

form1.Controls. Add(mtblMain)

ListLayers()

End Sub

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

HttpContext.Cur rent.Session("m objTest") = New Object

mobjTest = HttpContext.Cur rent.Session("m objTest")

'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(w riter)

End Sub

Private Sub ListLayers()

Dim lcbxTemp As HtmlInputCheckB ox

Dim ltcTEmp As HtmlTableCell

Dim ltrTemp As HtmlTableRow

mtblMain.Contro ls.Clear()

If mobjTest IsNot Nothing Then

ltrTemp = New HtmlTableRow

ltcTEmp = New HtmlTableCell

lcbxTemp = New HtmlInputCheckB ox

lcbxTemp.ID = "test"

lcbxTemp.Checke d = True

ltcTEmp.Control s.Add(lcbxTemp)

ltrTemp.Control s.Add(ltcTEmp)

mtblMain.Contro ls.Add(ltrTemp)

ltrTemp = New HtmlTableRow

ltcTEmp = New HtmlTableCell

lcbxTemp = New HtmlInputCheckB ox

lcbxTemp.ID = "test2"

lcbxTemp.Checke d = False

ltcTEmp.Control s.Add(lcbxTemp)

ltrTemp.Control s.Add(ltcTEmp)

mtblMain.Contro ls.Add(ltrTemp)

End If

End Sub

Protected Sub mbtnRefresh_Ser verClick(ByVal sender As Object, ByVal e As
System.EventArg s) Handles mbtnRefresh.Ser verClick

End Sub

End Class

'WEBFORM

<%@ Page Language="VB" AutoEventWireup ="false" CodeFile="Defau lt.aspx.vb"
Inherits="_Defa ult" %>

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

<head runat="server">

<title>Untitl ed Page</title>

</head>

<body>

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

</form>

</body>

</html>

Jul 4 '06 #1
1 4120
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_Ser verClick method
and then watch the following values:
mtblMain.Rows(0 ).Cells(0).Cont rols(0).ID
ctype(mtblMain. Rows(0).Cells(0 ).Controls(0), HtmlInputCheckB ox).Checked
mtblMain.Rows(1 ).Cells(0).Cont rols(0).ID
ctype(mtblMain. Rows(1).Cells(0 ).Controls(0), HtmlInputCheckB ox).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
25777
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 javascript with the onclick method. I need to do or not do something based on the value of the checkbox. To make this a simple as possible I have removed everything I was needing to do, etc. I know the value is getting passed because I can display it....
1
3511
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 ? <li>ABACAVIR SULFATE</li> <INPUT NAME="ingredient0" TYPE=checkbox VALUE="ABACAVIR SULFATE"><br> <input name="ABACAVIR SULFATE AMOUNT" type="radio" value="EQ 300MG BASE">EQ 300MG BASE<br> <input name="ABACAVIR SULFATE AMOUNT" type="radio" value="EQ...
4
11197
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! How do I convert a CheckBox.Checked to an int? Regards Martin Arvidsson
4
9801
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 = Server.CreateObject("ADODB.Connection") etc ... sql="select maa13 from mytable" dim rsArray
3
1211
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 option1. In code behind the form, simply write this line of code (make sure to include the space after the word option1 and the dot: this.option1 .Checked = true;
4
2479
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 up. As long as none of the checkboxes are disabled, viewstate works fine - and client actions are 'remembered' after a postback. but when I have one checkbox and it is disabled, then when I postback, ALL checkboxes get the unchecked value. ...
1
6596
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
7471
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# checkbox. so basically: checkbox.checked =1 else
1
6892
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 tried various things but none seem to work correctly. Any ideas? thanks
0
9961
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9800
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10777
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9597
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7990
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5817
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6014
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4642
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3251
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.