动态加控件的问题
我已经能实现动态加控件和删除控件了,可就是不能找到控件的id 我觉得好好像是 和ViewState 有关系,可我有不知道怎么用 希望大家能帮帮忙 下面是代码 我分不是很多了 就这些了,不过这是我全部的分了。前台
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<%-- <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder> --%>
<asp:Table ID="Table1" runat="server" CellPadding="0" CellSpacing="0" Height="56px"
Width="142px" BorderWidth="1px">
</asp:Table>
<br />
<asp:Button ID="btnAdd" runat="server" Text="添加" OnClick="btnAdd_Click" />
<asp:Button ID="btnclear" runat="server" Text="删除" OnClick="btnclear_Click"/>
<asp:HiddenField ID="hfCount" runat="server" Value="0" />
</div>
<div>
<input type="button" id="btnSu" runat="server" value="确定" onclick="getid()" />
<input type="hidden" id="hidde" value="0" runat="server"/>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form> --------------------编程问答-------------------- 后台:
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 Opertor_WorkFlow : System.Web.UI.Page
{
private int _count = 0;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
// 取得以前已创建控件的个数
if (!String.IsNullOrEmpty(this.Request["hfCount"]))
{
_count = Convert.ToInt32(this.Request["hfCount"]);
// hidde.value = Convert.ToString(_count);
}
// 假如按下“Add”按钮,那么count加一
string target = this.Request["btnAdd"];
string strclear = this.Request["btnclear"];
string strCont=this.Request["btnSure"];
if (strCont == "确定")
{
string str = string.Empty;
{
foreach (Control c in Table1.Controls)
{
for (int i = 1; i <= _count; i++)
{
// Label1.Text = TextAuditing + i.ToString();
str += ((TextBox)Table1.FindControl("TextAuditing" + i.ToString())).Text + "<br>";//获取控件的值
}
}
}
Label1.Text = str;
}
if (strclear == "删除")
{
_count--;
for (int i = 1; i <= _count; i++)
{ // 这里以TextBox为例,实际上需要创建的是WebPartZone
//TextBox newTextbox = new TextBox();
//newTextbox.ID = "TXT" + i.ToString();
//this.PlaceHolder1.Controls.Add(newTextbox);
TableRow row = new TableRow();
TableCell cellHead = new TableCell();
TableCell cellContent = new TableCell();
Label LabelAuditing = new Label();
TextBox TextAuditing = new TextBox();
LabelAuditing.ID = "LabelAuditing" + i.ToString();
LabelAuditing.Text = "工作流" + i.ToString();
TextAuditing.ID = "TextAuditing" + i.ToString();
TextAuditing.Height = 18;
cellHead.Controls.Add(LabelAuditing);
cellContent.Controls.Add(TextAuditing);
cellHead.BackColor = System.Drawing.Color.FromName("#EFEFEF");
cellHead.BorderWidth = 1;
cellHead.BorderColor = System.Drawing.Color.FromName("#CCCCCC");
cellHead.HorizontalAlign = HorizontalAlign.Left;
cellHead.Width = 60;
cellContent.BorderColor = System.Drawing.Color.FromName("#CCCCCC");
cellContent.BackColor = System.Drawing.Color.FromName("#EFEFEF");
cellContent.BorderWidth = 1;
cellContent.HorizontalAlign = HorizontalAlign.Left;
cellContent.Width = 100;
row.Cells.Add(cellHead);
row.Cells.Add(cellContent);
row.Height = 22;
this.Table1.BorderColor = System.Drawing.Color.FromName("#CCCCCC");
this.Table1.Width = 400;
this.Table1.Rows.Add(row);
}
}
if (target == "添加")
{
if (ViewState["CreateControl"] == null)
{
ViewState["CreateControl"] = true;
_count++;
// 动态创建控件
for (int i = 1; i <= _count; i++)
{ // 这里以TextBox为例,实际上需要创建的是WebPartZone
//TextBox newTextbox = new TextBox();
//newTextbox.ID = "TXT" + i.ToString();
//this.PlaceHolder1.Controls.Add(newTextbox);
TableRow row = new TableRow();
TableCell cellHead = new TableCell();
TableCell cellContent = new TableCell();
Label LabelAuditing = new Label();
TextBox TextAuditing = new TextBox();
LabelAuditing.ID = "LabelAuditing" + i.ToString();
LabelAuditing.Text = "工作流" + i.ToString();
TextAuditing.ID = "TextAuditing" + i.ToString();
// ViewState["TextAuditing" + i.ToString()] = TextAuditing.ID;
TextAuditing.Height = 18;
cellHead.Controls.Add(LabelAuditing);
cellContent.Controls.Add(TextAuditing);
cellHead.BackColor = System.Drawing.Color.FromName("#EFEFEF");
cellHead.BorderWidth = 1;
cellHead.BorderColor = System.Drawing.Color.FromName("#CCCCCC");
cellHead.HorizontalAlign = HorizontalAlign.Left;
cellHead.Width = 60;
cellContent.BorderColor = System.Drawing.Color.FromName("#CCCCCC");
cellContent.BackColor = System.Drawing.Color.FromName("#EFEFEF");
cellContent.BorderWidth = 1;
cellContent.HorizontalAlign = HorizontalAlign.Left;
cellContent.Width = 100;
row.Cells.Add(cellHead);
row.Cells.Add(cellContent);
row.Height = 22;
this.Table1.BorderColor = System.Drawing.Color.FromName("#CCCCCC");
this.Table1.Width = 400;
this.Table1.Rows.Add(row);
}
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
hfCount.Value = _count.ToString();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
// 不能在此添加WebPartZone控件,只能在OnInit或之前,否则报异常
}
protected void btnclear_Click(object sender, EventArgs e)
{
// 不能在此添加WebPartZone控件,只能在OnInit或之前,否则报异常
}
protected void btnSure_click(object sender, EventArgs e)
{
}
}
--------------------编程问答-------------------- 为什么没人回复我啊
--------------------编程问答-------------------- [img=http://topic.csdn.net/u/20091128/15/c9717ca5-bf35-44e3-8039-ea51cead23de.html?84175][/img]冲着你头像
ding --------------------编程问答-------------------- --------------------编程问答-------------------- 帮定! --------------------编程问答-------------------- 不知道你想问什么。
可能你上次创建的控件,asp.net没有为你保存状态
所以就找不到吧。 --------------------编程问答-------------------- 我就是问这么能找到控件的ID
补充:.NET技术 , ASP.NET