G
Guest
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>
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>