文件名为test.aspx HTML代码<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %><!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>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:Panel ID="plControls" runat="server">
</asp:Panel>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button"
Width="80px" /></div>
</form>
</body>
</html>后置文件代码using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
plcontrolsBind();
}
}
private void plcontrolsBind()
{
Label lb = new Label();
lb.Text = "<br> :";
plControls.Controls.Add(lb);
TextBox tb = new TextBox();
tb.Text ="文本框的值";
plControls.Controls.Add(tb);
RadioButton rb = new RadioButton();
rb.Text ="rb1的值";
rb.GroupName ="group";
plControls.Controls.Add(rb);
RadioButton rb2 = new RadioButton();
rb2.Text = "rb2的值";
rb2.GroupName = "group";
plControls.Controls.Add(rb2);
CheckBox ck = new CheckBox();
ck.Text = "复选框ck";
plControls.Controls.Add(ck);
CheckBox ck1= new CheckBox();
ck1.Text = "复选框ck1";
plControls.Controls.Add(ck1);
CheckBox ck2 = new CheckBox();
ck2.Text = "复选框2ck";
plControls.Controls.Add(ck2);
} protected void Button1_Click(object sender, EventArgs e)
{
foreach (Control ct in plControls.Controls)
{
string result = "";
if (ct is TextBox)
{
result = ((TextBox)ct).Text;
}
if (ct is RadioButton)
{
foreach (ListItem lt in ((RadioButtonList)ct).Items)
{
if (lt.Selected == true)
{
result = ((RadioButton)ct).Text+"被选中了";
}
}
} if (ct is CheckBoxList)
{
string s = "";
foreach (ListItem lt in ((CheckBoxList)ct).Items)
{
if (lt.Selected == true)
{
result = ((CheckBox)ct).Text+"被选中了";
}
}
}
Page.RegisterStartupScript("aaa", "<script>alert('"+result+"')</script>");
}
}
}问题:在Button点击事件Button1_Click为什么取不到控件的值,理论上应该没问题
哪位能告诉下忽视了什么地方
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:Panel ID="plControls" runat="server">
</asp:Panel>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button"
Width="80px" /></div>
</form>
</body>
</html>后置文件代码using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
plcontrolsBind();
}
}
private void plcontrolsBind()
{
Label lb = new Label();
lb.Text = "<br> :";
plControls.Controls.Add(lb);
TextBox tb = new TextBox();
tb.Text ="文本框的值";
plControls.Controls.Add(tb);
RadioButton rb = new RadioButton();
rb.Text ="rb1的值";
rb.GroupName ="group";
plControls.Controls.Add(rb);
RadioButton rb2 = new RadioButton();
rb2.Text = "rb2的值";
rb2.GroupName = "group";
plControls.Controls.Add(rb2);
CheckBox ck = new CheckBox();
ck.Text = "复选框ck";
plControls.Controls.Add(ck);
CheckBox ck1= new CheckBox();
ck1.Text = "复选框ck1";
plControls.Controls.Add(ck1);
CheckBox ck2 = new CheckBox();
ck2.Text = "复选框2ck";
plControls.Controls.Add(ck2);
} protected void Button1_Click(object sender, EventArgs e)
{
foreach (Control ct in plControls.Controls)
{
string result = "";
if (ct is TextBox)
{
result = ((TextBox)ct).Text;
}
if (ct is RadioButton)
{
foreach (ListItem lt in ((RadioButtonList)ct).Items)
{
if (lt.Selected == true)
{
result = ((RadioButton)ct).Text+"被选中了";
}
}
} if (ct is CheckBoxList)
{
string s = "";
foreach (ListItem lt in ((CheckBoxList)ct).Items)
{
if (lt.Selected == true)
{
result = ((CheckBox)ct).Text+"被选中了";
}
}
}
Page.RegisterStartupScript("aaa", "<script>alert('"+result+"')</script>");
}
}
}问题:在Button点击事件Button1_Click为什么取不到控件的值,理论上应该没问题
哪位能告诉下忽视了什么地方
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货