M
Michael Lang
I'm adding checkbox controls to a panel in a post back, I then have a second
post back in which I attempt to process the checkbox controls however they
seem to have disappeared off the panel. The following code demonstrates
what I'm trying to do.
Can anyone explain why there is no checkbox control on the panel when btnTwo
is clicked?
default.aspx:----------------------------------------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<aspanel ID="pnlTest" runat="server"></aspanel>
<asp:Button ID="btnOne" runat="server" Text="Next>"
OnClick="btnOne_Click" />
<asp:Button ID="btnTwo" runat="server" Text="Save" Visible ="false"
OnClick="btnTwo_Click" />
<br />
<asp:Label ID="Label1" runat="server"
Text="Label"></asp:Label></div>
</form>
</body>
</html>
-----------------------------------------------------------------------------------------------------------
default.aspx.cs:--------------------------------------------------------------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnOne_Click(object sender, EventArgs e)
{
btnOne.Visible = false;
CheckBox chkTest = new CheckBox();
chkTest.ID = "m";
chkTest.Text = "CheckMe";
btnTwo.Visible = true;
pnlTest.Controls.Add(chkTest);
}
protected void btnTwo_Click(object sender, EventArgs e)
{
if (pnlTest.Controls.Count < 1)
{
Label1.Text = "Failed";
return;
}
Control control = pnlTest.FindControl("m");
CheckBox chkTest = control as CheckBox;
if (chkTest.Checked)
Label1.Text = "Worked";
}
}
post back in which I attempt to process the checkbox controls however they
seem to have disappeared off the panel. The following code demonstrates
what I'm trying to do.
Can anyone explain why there is no checkbox control on the panel when btnTwo
is clicked?
default.aspx:----------------------------------------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<aspanel ID="pnlTest" runat="server"></aspanel>
<asp:Button ID="btnOne" runat="server" Text="Next>"
OnClick="btnOne_Click" />
<asp:Button ID="btnTwo" runat="server" Text="Save" Visible ="false"
OnClick="btnTwo_Click" />
<br />
<asp:Label ID="Label1" runat="server"
Text="Label"></asp:Label></div>
</form>
</body>
</html>
-----------------------------------------------------------------------------------------------------------
default.aspx.cs:--------------------------------------------------------------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnOne_Click(object sender, EventArgs e)
{
btnOne.Visible = false;
CheckBox chkTest = new CheckBox();
chkTest.ID = "m";
chkTest.Text = "CheckMe";
btnTwo.Visible = true;
pnlTest.Controls.Add(chkTest);
}
protected void btnTwo_Click(object sender, EventArgs e)
{
if (pnlTest.Controls.Count < 1)
{
Label1.Text = "Failed";
return;
}
Control control = pnlTest.FindControl("m");
CheckBox chkTest = control as CheckBox;
if (chkTest.Checked)
Label1.Text = "Worked";
}
}