本文介绍了ASP.NET 2.0 - 在回发中添加了丢失的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在向帖子后面的面板添加复选框控件,然后我有第二张 回复,其中我尝试处理复选框控件然后他们 似乎已从专题小组中消失。下面的代码演示了 我正在尝试做什么。 任何人都可以解释为什么btnTwo时没有复选框控制面板>点击? default.aspx:-------------------------- -------------------------------------------------- ------------------ <%@ Page Language =" C#" AutoEventWireup = QUOT;真" 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> < asp:Panel ID =" pnlTest" runat =" server">< / asp:Panel> < asp:Button ID =" btnOne" RUNAT = QUOT;服务器" Text =" Next>" OnClick =" btnOne_Click" /> < asp:Button ID =" btnTwo" RUNAT = QUOT;服务器"文本= [储存" Visible =" false" OnClick =" btnTwo_Click" /> < br /> < asp:Label ID =" Label1" runat =" server" Text =" Label">< / asp:Label>< / div> < / form> < / body> < / html> --------------- -------------------------------------------------- ------------------------------------------ default.aspx.cs:---------------------------------------- -------------------------------------------------- - 使用System; 使用System.Data; 使用System.Configuration; 使用System.Web; 使用System.Web.Security; 使用System.Web.UI; 使用System.Web.UI .WebControls; 使用System.Web.UI.WebControls.WebParts; 使用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,Even tArgs 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; } 控制控制= pnlTest.FindControl(" m"); CheckBox chkTest =控制为CheckBox; if(chkTest.Checked ) Label1.Text =" Worked"; } } - -------------------------------------------------- -------------------------------------------------- ----- 提前致谢... MichaelI''m adding checkbox controls to a panel in a post back, I then have a secondpost back in which I attempt to process the checkbox controls however theyseem to have disappeared off the panel. The following code demonstrateswhat I''m trying to do.Can anyone explain why there is no checkbox control on the panel when btnTwois 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><asp:Panel ID="pnlTest" runat="server"></asp:Panel><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";}}-----------------------------------------------------------------------------------------------------------Thanks in advance...Michael推荐答案 Michael, 你必须在每次回发时重新创建动态创建的控件。 在这里查找更多: http://aspnet.4guysfromrolla.com/articles /092904-1.aspx - Eliyahu Goldin, 软件开发人员&顾问 Microsoft MVP [ASP.NET] http: //msmvps.com/blogs/egoldin " Michael Lang" < micklang at gmail.comwrote in message news:es ************** @ TK2MSFTNGP02.phx.gbl ...Michael,You have to re-create dynamically-created controls on every postback.Find more here: http://aspnet.4guysfromrolla.com/articles/092904-1.aspx--Eliyahu Goldin,Software Developer & ConsultantMicrosoft MVP [ASP.NET] http://msmvps.com/blogs/egoldin"Michael Lang" <micklang at gmail.comwrote in messagenews:es**************@TK2MSFTNGP02.phx.gbl... 我正在向帖子后面的面板添加复选框控件,然后我有一个 的第二篇帖子,我试图处理复选框控件 演示了我想要做的事情。 任何人都可以解释为什么面板上没有复选框控件 btn点击两次? default.aspx:-------------------------- -------------------------------------------------- ------------------ <%@ Page Language =" C#" AutoEventWireup = QUOT;真" 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> < asp:Panel ID =" pnlTest" runat =" server">< / asp:Panel> < asp:Button ID =" btnOne" RUNAT = QUOT;服务器" Text =" Next>" OnClick =" btnOne_Click" /> < asp:Button ID =" btnTwo" RUNAT = QUOT;服务器"文本= [储存"可见 =" false"的OnClick = QUOT; btnTwo_Click" /> < br /> < asp:Label ID =" Label1" runat =" server" Text =" Label">< / asp:Label>< / div> < / form> < / body> < / html> --------------- -------------------------------------------------- ------------------------------------------ default.aspx.cs:---------------------------------------- -------------------------------------------------- - 使用System; 使用System.Data; 使用System.Configuration; 使用System.Web; 使用System.Web.Security; 使用System.Web.UI; 使用System.Web.UI .WebControls; 使用System.Web.UI.WebControls.WebParts; 使用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 se nder,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; } 控制控制= pnlTest.FindControl(" m"); CheckBox chkTest =控制为CheckBox; if(chkTest.Checked ) Label1.Text =" Worked"; } } - -------------------------------------------------- -------------------------------------------------- ----- 提前致谢... MichaelI''m adding checkbox controls to a panel in a post back, I then have asecond post back in which I attempt to process the checkbox controlshowever they seem to have disappeared off the panel. The following codedemonstrates what I''m trying to do.Can anyone explain why there is no checkbox control on the panel whenbtnTwo 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> <asp:Panel ID="pnlTest" runat="server"></asp:Panel> <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"; }}-----------------------------------------------------------------------------------------------------------Thanks in advance...Michael谢谢... 非常正确。 我想也许是通过重新添加控件他们的状态会丢失,但通过测试看起来不是这样的。所以对于任何感兴趣的人来说, 以下代码可以工作...... 使用System; 使用System.Data; 使用System.Configuration; 使用System.Web; 使用System.Web.Security; 使用System.Web .UI; 使用System.Web.UI.WebControls; 使用System.Web.UI.WebControls.WebParts; 使用System。 Web.UI.HtmlControls; public partial class _Default:System.Web.UI.Page { protected void Page_Load (对象发送者,EventArgs e) { if(btnTwo.Visible) { CheckBox chkTest = new CheckBox(); chkTest.ID =" m"; chkTest.Text =" CheckMe"; pnlTest.Controls .Add(chkTest); } } protected void btnOne_Click(object sender,EventArgs e) { btnOne.Visible = false; btnTwo.Visible = true; CheckBox chkTest = new CheckBox(); chkTest.ID =" m"; chkTest.Text =" CheckMe"; pnlTest.Controls.Add(chkTest); } protected void btnTwo_Click(object sender,EventArgs e) { if(pnlTest.Controls.Count< 1) { Label1.Text =" Failed"; return; } 控制控制= pnlTest.FindControl(" m"); CheckBox chkTest =控制为CheckBox; if(chkTest.Checked ) Label1.Text =" Is Checked"; else Label1.Text =" Not Checked"; } } " Eliyahu Goldin" < RE ************************** @ mMvVpPsS.org写在 消息新闻:Oa **** ********** @ TK2MSFTNGP04.phx.gbl ...Thanks...Quite right.I thought perhaps by re-adding the controls their state would be lost butthrough testing this looks not to be the case. So for anyone interested thefollowing code works...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){if (btnTwo.Visible){CheckBox chkTest = new CheckBox();chkTest.ID = "m";chkTest.Text = "CheckMe";pnlTest.Controls.Add(chkTest);}}protected void btnOne_Click(object sender, EventArgs e){btnOne.Visible = false;btnTwo.Visible = true;CheckBox chkTest = new CheckBox();chkTest.ID = "m";chkTest.Text = "CheckMe";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 = "Is Checked";elseLabel1.Text = "Not Checked";}}"Eliyahu Goldin" <RE**************************@mMvVpPsS.orgwrote inmessage news:Oa**************@TK2MSFTNGP04.phx.gbl... Michael, 你必须在每次回发时重新创建动态创建的控件。 在这里找到更多: http://aspnet.4guysfromrolla.com/articles/092904-1.aspx - Eliyahu Goldin, 软件开发人员&顾问 Microsoft MVP [ASP.NET] http: //msmvps.com/blogs/egoldin " Michael Lang" < micklang at gmail.comwrote in message news:es ************** @ TK2MSFTNGP02.phx.gbl ...Michael,You have to re-create dynamically-created controls on every postback.Find more here: http://aspnet.4guysfromrolla.com/articles/092904-1.aspx--Eliyahu Goldin,Software Developer & ConsultantMicrosoft MVP [ASP.NET] http://msmvps.com/blogs/egoldin"Michael Lang" <micklang at gmail.comwrote in messagenews:es**************@TK2MSFTNGP02.phx.gbl... >我正在向帖子后面的面板添加复选框控件,然后我有一个第二个帖子,我试图处理复选框控件然而他们似乎已经从专家组中消失了。以下代码演示了我正在尝试做什么。 任何人都可以解释为什么单击 btnTwo时面板上没有复选框控件? default.aspx:------------------------------------------ -------------------------------------------------- - <%@ Page Language =" C#" AutoEventWireup = QUOT;真" 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> < asp:Panel ID =" pnlTest" runat =" server">< / asp:Panel> < asp:Button ID =" btnOne" RUNAT = QUOT;服务器" Text =" Next>" OnClick =" btnOne_Click" /> < asp:Button ID =" btnTwo" RUNAT = QUOT;服务器"文本= [储存"可见 =" false"的OnClick = QUOT; btnTwo_Click" /> < br /> < asp:Label ID =" Label1" runat =" server" Text =" Label">< / asp:Label>< / div> < / form> < / body> < / html> ----------------------------------- -------------------------------------------------- ---------------------- default.aspx.cs:-------------- -------------------------------------------------- ---------------------------- 使用System; 使用System.Data; 使用System.Configuration; 使用System.Web; 使用System.Web.Security; 使用System.Web.UI; 使用System.Web.UI.WebControls ; 使用System.Web.UI.WebControls.WebParts; 使用System.Web.UI.HtmlControls; 公共部分类_Default:System.Web.UI.Page { protected void Page_Load(object sender,EventArgs e) 受保护的void btnOne_Click(对象发送者,EventArgs e) { btnOne.Visible = false; CheckBox chkTest = new CheckBox(); ch kTest.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 ="失败" ;; 返回; 控制控制= pnlTest.FindControl(" m" }; CheckBox chkTest =控制为CheckBox; if(chkTest.Checked) Label1.Text =" Worked"; } } ---------------------------------------------- -------------------------------------------------- ----------- 提前致谢... Michael>I''m adding checkbox controls to a panel in a post back, I then have asecond post back in which I attempt to process the checkbox controlshowever they seem to have disappeared off the panel. The following codedemonstrates what I''m trying to do.Can anyone explain why there is no checkbox control on the panel whenbtnTwo 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> <asp:Panel ID="pnlTest" runat="server"></asp:Panel> <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"; }}-----------------------------------------------------------------------------------------------------------Thanks in advance...Michael" Michael Lang" < micklang at gmail.comwrote in message news:Ou ************** @ TK2MSFTNGP02.phx.gbl ..."Michael Lang" <micklang at gmail.comwrote in messagenews:Ou**************@TK2MSFTNGP02.phx.gbl... 以下代码有效...following code works... protected void Page_Load(object sender,EventArgs e) protected void Page_Load(object sender, EventArgs e) 在这种情况下,你很幸运... 一般来说,为了几乎保证这样的代码会 工作,你需要在Page_Init中创建动态控件,而不是Page_Load ...In which case, you''re quite fortunate...Generally speaking, in order to pretty much guarantee that such code willwork, you need to create the dynamic controls in Page_Init, not Page_Load... 这篇关于ASP.NET 2.0 - 在回发中添加了丢失的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-18 00:54
查看更多