如何把动态生成控件的值保存??? 在一个动态生成的TextBox中输入值,如何实现在点击保存按钮后把输入到TextBox中的值保存下来??? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1、在CreateChildControls里new TextBox、设置ID,并添加到当前Control(或Page、UserControl)的Controls2、在OnLoad(或Page_Load)的!IsPostBack里初始化TextBox的内容3、在按钮事件处理方法中取值 保存按钮后把输入到TextBox中的值保存下来楼上说的很清楚了,直接取出数据保存就是了 在服務器端怎麼讀出數據?服務器根本就不能捕捉到這一TextBox控件!! 前几天刚做的一个实例,参考一下吧,页面上先拖一个table protected void Page_Load(object sender, EventArgs e) { Table1.CellPadding = 0; Table1.CellSpacing = 0; Table1.Width = Unit.Percentage(50); TableRow trHead = new TableRow(); TableCell tcHeadFoodName = new TableCell(); TableCell tcHeadType = new TableCell();//Header tcHeadFoodName.Text ="类别"; tcHeadType.Text = "值"; trHead.Cells.Add(tcHeadFoodName); trHead.Cells.Add(tcHeadType); trHead.HorizontalAlign = HorizontalAlign.Center; trHead.Height = 30; TableRow tr2 = new TableRow(); TableCell tc21 = new TableCell(); TableCell tc22 = new TableCell(); tc21.Text = "产品名称:"; TextBox tb1 = new TextBox(); tb1.ID = "txtProdName"; tc22.Controls.Add(tb1); tr2.Cells.Add(tc21); tr2.Cells.Add(tc22); Table1.Rows.Add(tr2);//从库里读出 SqlConnection conn = DB.CreateConn(); conn.Open(); int pid=Convert.ToInt32(Request.QueryString["classid"].ToString()); lblClassid.Text = Request.QueryString["classid"].ToString(); string sql = "select t.* from(select * from tb_class where parentid=" +pid+ " and isclass=0 union all select * from tb_class where parentid in(select classID from tb_class where parentid=3 and isclass=0)) t order by dbo.f_getClassmergid(t.classid)"; SqlCommand cmd = new SqlCommand(sql,conn); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) {//开始动态创建控件 if (Convert.ToInt32(dr["parentid"].ToString()) == pid) {//判断是类别,则不创建控件。 TableRow tr1 = new TableRow(); TableCell tc1 = new TableCell(); TableCell tc2 = new TableCell(); tc1.Text = dr["className"].ToString(); tr1.BackColor = Color.LightGray; tr1.Cells.Add(tc1); tr1.Cells.Add(tc2); Table1.Rows.Add(tr1); } else { TableRow tr1 = new TableRow(); TableCell tc1 = new TableCell(); TableCell tc2 = new TableCell(); Label lbl = new Label(); lbl.ID = "lbl" + dr["classid"].ToString(); lbl.Text = dr["className"].ToString(); TextBox tb = new TextBox(); tb.ID = "txt" + dr["classid"].ToString(); //预先设置ID tc1.Controls.Add(lbl); tc2.Controls.Add(tb); //验证控件 RequiredFieldValidator rf = new RequiredFieldValidator(); rf.ID = "rs" + dr["classid"].ToString(); rf.ControlToValidate = "txt" + dr["classid"].ToString(); rf.ErrorMessage = "请输入"+dr["className"].ToString(); tc2.Controls.Add(rf); tr1.Cells.Add(tc1); tr1.Cells.Add(tc2); tr1.HorizontalAlign = HorizontalAlign.Left; tr1.Height = 30; tr1.VerticalAlign = VerticalAlign.Middle; Table1.Rows.Add(tr1); } } dr.Close(); conn.Close(); conn.Dispose(); } protected void Button1_Click(object sender, EventArgs e) ///取值时 { this.Label1.Text = ("名称:" + ((TextBox)(Table1.FindControl("txtProdName"))).Text + "<br>"); SqlConnection conn = DB.CreateConn(); conn.Open(); string sql = "select * from tb_class where parentid in(select classID from tb_class where parentid="+Convert.ToInt32(lblClassid.Text)+" and isclass=0) order by dbo.f_getClassmergid(classid)"; SqlCommand cmd1 = new SqlCommand(sql, conn); SqlDataReader dr = cmd1.ExecuteReader(); while (dr.Read()) { //真正的取值的字段,要FindControl一下. this.Label1.Text += ((Label)Table1.FindControl("lbl"+dr["classid"].ToString())).Text+((TextBox)Table1.FindControl("txt" + dr["classid"].ToString())).Text+"<br>"; } dr.Close(); conn.Close(); conn.Dispose(); } 路过------------------------http://fenglin.xland.cn------------------------ SQL 求救! VB.NET 里做了一个网站,以前ASP是直接目录拷到IIS下,设一下就行,现在该怎么发布这个网站呢? asp.net快捷方式 怎样获取动态生成控件的ID? 简单提问 asp.net用户注册页面注册成功按钮的代码 跪求大侠们一个WEB上的问题,具体请进来看。在线急等 数据库更新语句的执行效率问题,研究一下吧 如果Enableviewstate=false DATAGRID 的自定义分页功能还可以用吗? !!!有什么区别 !!! 程序员们,街头老虎机的算法是什么?工作原理是什么? 我在用VSS2005做使用internet访问时,提示我IIS未安装?
protected void Page_Load(object sender, EventArgs e)
{
Table1.CellPadding = 0;
Table1.CellSpacing = 0;
Table1.Width = Unit.Percentage(50); TableRow trHead = new TableRow(); TableCell tcHeadFoodName = new TableCell();
TableCell tcHeadType = new TableCell();//Header
tcHeadFoodName.Text ="类别";
tcHeadType.Text = "值";
trHead.Cells.Add(tcHeadFoodName);
trHead.Cells.Add(tcHeadType); trHead.HorizontalAlign = HorizontalAlign.Center;
trHead.Height = 30; TableRow tr2 = new TableRow();
TableCell tc21 = new TableCell();
TableCell tc22 = new TableCell();
tc21.Text = "产品名称:";
TextBox tb1 = new TextBox();
tb1.ID = "txtProdName";
tc22.Controls.Add(tb1);
tr2.Cells.Add(tc21);
tr2.Cells.Add(tc22);
Table1.Rows.Add(tr2);
//从库里读出
SqlConnection conn = DB.CreateConn();
conn.Open();
int pid=Convert.ToInt32(Request.QueryString["classid"].ToString());
lblClassid.Text = Request.QueryString["classid"].ToString();
string sql = "select t.* from(select * from tb_class where parentid=" +pid+ " and isclass=0 union all select * from tb_class where parentid in(select classID from tb_class where parentid=3 and isclass=0)) t order by dbo.f_getClassmergid(t.classid)";
SqlCommand cmd = new SqlCommand(sql,conn);
SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read())
{//开始动态创建控件
if (Convert.ToInt32(dr["parentid"].ToString()) == pid)
{//判断是类别,则不创建控件。
TableRow tr1 = new TableRow();
TableCell tc1 = new TableCell();
TableCell tc2 = new TableCell();
tc1.Text = dr["className"].ToString();
tr1.BackColor = Color.LightGray; tr1.Cells.Add(tc1);
tr1.Cells.Add(tc2);
Table1.Rows.Add(tr1);
}
else
{
TableRow tr1 = new TableRow();
TableCell tc1 = new TableCell();
TableCell tc2 = new TableCell(); Label lbl = new Label();
lbl.ID = "lbl" + dr["classid"].ToString();
lbl.Text = dr["className"].ToString(); TextBox tb = new TextBox();
tb.ID = "txt" + dr["classid"].ToString(); //预先设置ID tc1.Controls.Add(lbl);
tc2.Controls.Add(tb);
//验证控件
RequiredFieldValidator rf = new RequiredFieldValidator();
rf.ID = "rs" + dr["classid"].ToString();
rf.ControlToValidate = "txt" + dr["classid"].ToString();
rf.ErrorMessage = "请输入"+dr["className"].ToString(); tc2.Controls.Add(rf); tr1.Cells.Add(tc1);
tr1.Cells.Add(tc2);
tr1.HorizontalAlign = HorizontalAlign.Left;
tr1.Height = 30;
tr1.VerticalAlign = VerticalAlign.Middle; Table1.Rows.Add(tr1);
}
}
dr.Close();
conn.Close();
conn.Dispose();
}
protected void Button1_Click(object sender, EventArgs e) ///取值时
{
this.Label1.Text = ("名称:" + ((TextBox)(Table1.FindControl("txtProdName"))).Text + "<br>");
SqlConnection conn = DB.CreateConn();
conn.Open();
string sql = "select * from tb_class where parentid in(select classID from tb_class where parentid="+Convert.ToInt32(lblClassid.Text)+" and isclass=0) order by dbo.f_getClassmergid(classid)";
SqlCommand cmd1 = new SqlCommand(sql, conn);
SqlDataReader dr = cmd1.ExecuteReader();
while (dr.Read())
{ //真正的取值的字段,要FindControl一下.
this.Label1.Text += ((Label)Table1.FindControl("lbl"+dr["classid"].ToString())).Text+((TextBox)Table1.FindControl("txt" + dr["classid"].ToString())).Text+"<br>";
}
dr.Close();
conn.Close();
conn.Dispose(); }
http://fenglin.xland.cn
------------------------