cs文件: 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 WebApplication1 { /// <summary> /// WebForm5 的摘要说明。 /// </summary> public class WebForm5 : System.Web.UI.Page { protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.HtmlControls.HtmlTableCell TD1; protected System.Web.UI.WebControls.Button Button1;
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 WebApplication1
{
/// <summary>
/// WebForm5 的摘要说明。
/// </summary>
public class WebForm5 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.HtmlControls.HtmlTableCell TD1;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
TextBox tb=new TextBox();
tb.ID="TB";
TD1.Controls.Add(tb);
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
Label1.Text=((TextBox)TD1.Controls[0].FindControl("TB")).Text;
}
}
}
aspx文件:
<%@ Page language="c#" Codebehind="WebForm5.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm5" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm5</title>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body>
<form id="WebForm5" method="post" runat="server">
<FONT face="宋体">
<P>
<TABLE id="Table1" cellSpacing="0" cellPadding="0" width="100%" border="0">
<TR>
<TD id="TD1" runat="server"></TD>
</TR>
</TABLE>
</P>
<P>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button></P>
<P>
<asp:Label id="Label1" runat="server">Label</asp:Label></P>
</FONT>
</form>
</body>
</HTML>For VS.NET 中文简体正式版
看不懂,VB的有吗
to all
当点击按钮,动态创建的textbox就没了。。
动态生成的textbox由于页面重载的时候丢失了,所以取不到Text内容。
你可以做一个标志,用viewstate或者cache都可。
在page_init或者page_load中判断是否要动态生成textbox
比如
if(ViewState["flag"]=1)
{
TextBox myTextbox=new TextBox();
Page.controls[1].controls.Add(myTextbox);}
访问myTextbox.Text
Button1_Click(....)
{
string my=myTextbox.Text;(注意这里不是textbox的id,而是直接引用该对象)
.......
......
handle...
}
double x1,x2;
string dgTop; for(int i=0;i<15;i++) {
box[i] = new TextBox();
box[i].ID = "box_"+colinfo[i];
box[i].Text = "";
box[i].MaxLength = 0;
box[i].Visible = true;
box[i].Style["POSITION"] = "absolute";
box[i].Style["Top"] = Convert.ToString(30*i+30)+"px"; dgTop = BgDiv.Style["Width"];
box[i].Style["Left"]= "600px";
box[i].Style["Z-INDEX"] = "102";
this.Controls[1].Controls.Add(box[i]); Label label = new Label();
label.ID = "label_"+colinfo[i];
label.Text = colname[i];
label.Style["POSITION"] = "absolute";
dgTop = DgCustomer.Style["Top"];
label.Style["Top"] = Convert.ToString(30*i+30)+"px";
x1 = DgCustomer.Width.Value;
x2 = Convert.ToDouble(DgCustomer.Style["Left"]);
label.Style["Left"] = Convert.ToString(430)+"px"; label.Visible = true;
this.Controls[1].Controls.Add(label);
}
ViewState["OldNumber"] = "";
OldNumber = "";
}
override protected void OnInit(EventArgs e)
InitializeComponent();
base.OnInit(e);
AddNewRecord();
ViewState["CurrentPage"] = 0;
}private void TbNavigate_ButtonClick(object sender, System.EventArgs e) {
ToolbarItem item = (ToolbarItem)sender;
int page; switch(item.ID.ToUpper()){
case "ADD":
CreateNewRecord();
break;
case "SAVE":
SaveRecord();
break;
default:
break;
}
}
你新建一个项目将我的cs文件与aspx文件的代码copy到相应的文件中,就一目了然了。
最主要的代码是:
cs文件中在页面初始化时:
override protected void OnInit(EventArgs e)
{
TextBox tb=new TextBox();//构造函数
tb.ID="TB";//给生成的TextBox实例ID赋值
TD1.Controls.Add(tb);//将TextBox添加到单元格TD1中
InitializeComponent();
base.OnInit(e);
}按钮事件:
private void Button1_Click(object sender, System.EventArgs e)
{
Label1.Text=((TextBox)TD1.Controls[0].FindControl("TB")).Text;//通过FindControl方法下塑造型一个TextBox控件,并得到它的Text属性值(也就是在页面初始化中动态生成的控件)
}
}
我习惯
在oninit事件中加载
for (int i=1;i<3;i++){
TextBox mTextbox=new TextBox();
mTextbox.ID=i.ToString();
Page.Controls[1].Controls.Add(mTextbox);
}
然后在其他应用事件中通过id来确定每一个自己加载的textbox习惯而已。
}
我试验一把先。to hgknight(江雨)
怎么把用户输入的text保存到VIEWSTATE里?
关键是在什么时候做这个动作?
然后再重建的时候读取回去是吗。