动态创建控件后取值问题,高手帮忙啊,急死我也~~~~~~~ 你可以把动态产生的控件放到复合控件PlaceHolder里面,他就不会消失了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 页面回发后在OnInit事件里面再执行一次动态创建控件的代码 在你点击处理按钮的时候,在前端用javascript把你控件中的值转移到一些textbox,hidden表单里面再在后台处理,如何?这个方法的效率较低,可以考虑一下。 1.每回都要建立控件一次。2.如要保持状态,请把控件放在OnInit事件里创建。 质疑henryfan1(每天好心情(*_*)) :我的测试代码:用户点击添加按钮,在后台添加一个textbox,用户在里面填写一些值,用户再单击确定,在page_load时添加同样的按钮,取textbox里的值,经测试为空字符传。前台:<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="test.WebForm1" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ><HTML> <HEAD> <title>WebForm1</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="FlowLayOut"> <form id="Form1" method="post" runat="server"> <FONT face="宋体"> <asp:Button id="Button1" runat="server" Text="添加表单"></asp:Button> <asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder> <asp:Button id="btnOK" runat="server" Text="确定"></asp:Button></FONT> </form> </body></HTML>后台:using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Drawing;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;namespace test{ /// <summary> /// WebForm1 的摘要说明。 /// </summary> public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1; protected System.Web.UI.WebControls.Button btnOK; protected System.Web.UI.WebControls.Button Button1; private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 if ( !IsPostBack ) { ViewState["flag"] = false; } if ( Convert.ToBoolean(ViewState["flag"]) == true ) { AddTxtBox1(); } } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.Button1.Click += new System.EventHandler(this.Button1_Click); this.btnOK.Click += new System.EventHandler(this.btnOK_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void Button1_Click(object sender, System.EventArgs e) { AddTxtBox1(); ViewState["flag"] = true; } private void AddTxtBox1() { TextBox txtBox1 = new TextBox(); txtBox1.ID = "txt1"; PlaceHolder1.Controls.Add( txtBox1 ); } private void btnOK_Click(object sender, System.EventArgs e) { string a = (( TextBox )PlaceHolder1.FindControl("txtBox1")).Text; Response.Write(a); Response.End(); } }} 找到个解决方法,动态创建控件不在点击按钮时,在Page_Load事件中,点击按钮时通过一个参数调用Page_Load事件创建控件代码.可以取到值!!!各位还有好方法吗?讨论一下~~~ 把页面所在的类串行化!!!然后保存在Session中!!以后随便你怎么写! 在page_load中创建控件就可以。 不好意思,我的代码有点问题:string a = (( TextBox )PlaceHolder1.FindControl("txtBox1")).Text;改为string a = (( TextBox )PlaceHolder1.Controls[0]).Text;就可以了。 sfar(正则表达式学习中) 这位朋友,看来你最执着了,谢谢你啊,交个朋友吧,呵!! 求助Access连接方法!!!!!! C#如何读取excel表中某个单元格的数据,急急急急! (急救)关于CrystalReport显示不出数据问题。。。。。 js焦点问题 DataGrid中加入控件 更新数据的问题,在线结贴 如何在.net页面中动态添加柱状图饼状图 asp.net 放51job动态读取数据问题 如何让代码有颜色区分?对象后面加"点"后怎样出现对象的属性和方法? 急急急 谁有mvc5的安装包 ☆★★★怎样让用户控件托到Web窗体上时有自己的界面??? 请问有什么方法可以对浏览端上的文件操作,如复制、删除等。
2.如要保持状态,请把控件放在OnInit事件里创建。
我的测试代码:
用户点击添加按钮,在后台添加一个textbox,用户在里面填写一些值,用户再单击确定,在page_load时添加同样的按钮,取textbox里的值,经测试为空字符传。
前台:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="test.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="FlowLayOut">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:Button id="Button1" runat="server" Text="添加表单"></asp:Button>
<asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:Button id="btnOK" runat="server" Text="确定"></asp:Button></FONT>
</form>
</body>
</HTML>后台:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace test
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;
protected System.Web.UI.WebControls.Button btnOK;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if ( !IsPostBack )
{
ViewState["flag"] = false;
}
if ( Convert.ToBoolean(ViewState["flag"]) == true )
{
AddTxtBox1();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
AddTxtBox1();
ViewState["flag"] = true;
}
private void AddTxtBox1()
{
TextBox txtBox1 = new TextBox();
txtBox1.ID = "txt1";
PlaceHolder1.Controls.Add( txtBox1 );
} private void btnOK_Click(object sender, System.EventArgs e)
{
string a = (( TextBox )PlaceHolder1.FindControl("txtBox1")).Text;
Response.Write(a);
Response.End();
}
}
}
然后保存在Session中!!以后随便你怎么写!
string a = (( TextBox )PlaceHolder1.FindControl("txtBox1")).Text;
改为string a = (( TextBox )PlaceHolder1.Controls[0]).Text;就可以了。