高手帮忙下。我的要求是按增加按纽动态生成textbox控件,然后往textbox控件输入数据,按提交按纽把数据提交到一个string [] a 里面保存,一个text对应string[]a的一个元素,如果text为空就不保存。其中动态生成textbox控件的代码已经实现,数据保存望高手解答。
代码如下:
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
ViewState["Count"]=0;
else
for(int i=1;i<=(int)ViewState["Count"];i++)
{
addproduct(i.ToString());
}
}
void addproduct(string num)
{
LiteralControl mylabel;
TextBox mybox = new TextBox();
mylabel = new LiteralControl();
mylabel.Text = "<br>";
PlaceHolder1.Controls.Add(mylabel);
mybox = new TextBox();
mybox.ID = "txtProduct" + num;
PlaceHolder1.Controls.Add(mybox);
}
protected void add_btn(object sender, EventArgs e)
{
int count =(int)ViewState["Count"];
count++;
ViewState["Count"] = count;
addproduct(ViewState["Count"].ToString());
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>动态添加控件</title>
</head>
<body style="font-size: 12px;">
<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:Button ID="Button1" runat="server" Text="增加" OnClick="add_btn" /><br />
<br />
<br />
<asp:Button ID="Button2" runat="server" Text="提交" /></div>
</form>
</body>
</html>
代码如下:
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
ViewState["Count"]=0;
else
for(int i=1;i<=(int)ViewState["Count"];i++)
{
addproduct(i.ToString());
}
}
void addproduct(string num)
{
LiteralControl mylabel;
TextBox mybox = new TextBox();
mylabel = new LiteralControl();
mylabel.Text = "<br>";
PlaceHolder1.Controls.Add(mylabel);
mybox = new TextBox();
mybox.ID = "txtProduct" + num;
PlaceHolder1.Controls.Add(mybox);
}
protected void add_btn(object sender, EventArgs e)
{
int count =(int)ViewState["Count"];
count++;
ViewState["Count"] = count;
addproduct(ViewState["Count"].ToString());
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>动态添加控件</title>
</head>
<body style="font-size: 12px;">
<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:Button ID="Button1" runat="server" Text="增加" OnClick="add_btn" /><br />
<br />
<br />
<asp:Button ID="Button2" runat="server" Text="提交" /></div>
</form>
</body>
</html>
for(i=0;i<lary.length;i++)
{
if(lary[i].type=="text"&&!isNaN(lary[i].value)&&lary[i].value!="")
{
useCont += parseInt(lary[i].value,10);
}
}我用js写的一部分
仿照csdn结帖时动态显示分数
先为提交按钮定义一事件save_btn
<asp:Button ID="Button2" runat="server" Text="提交" OnClick="save_btn"/>
protected void save_btn(object sender, EventArgs e)
{
ArrayList al = new ArrayList(); int num = Convert.ToInt32(ViewState["Count"]);
for (int i = 1; i <= num; i++)
{
if (((TextBox)this.FindControl("txtProduct" + i.ToString())).Text.Trim() != "")
{
al.Add(((TextBox)this.FindControl("txtProduct" + i.ToString())).Text.Trim());
}
}
//将输入的非空值保存到string[] a
string[] a = new string[al.Count];
al.CopyTo(a,0);
Response.Write(a.Length.ToString());
}
{
if (ViewState["Count"] != null)
{
for (int i = 0; i < Convert.ToInt16(ViewState["Count"]); i++)
{
AddTextBox();
}
} }动态增加:
#region 动态文本框
//向页面中添加文本框 public void AddTextBox()
{
TableRow tr = new TableRow();
TableCell tc1 = new TableCell();
TextBox txt = new TextBox();
txt.ID = "tb" + Table1.Rows.Count;
txt.Font.Size = FontUnit.Point(9);
TableCell tc2 = new TableCell();
Label lab = new Label();
lab.ID = "lab" + Table1.Rows.Count;
lab.Width = 50;
txt.Width = 200;
lab.Text = "文件" + (Table1.Rows.Count + 1);
tc2.Controls.Add(lab);
tc1.Controls.Add(txt);
tr.Cells.Add(tc2);
tr.Cells.Add(tc1);
Table1.Rows.Add(tr);
}添加事件:
//添加动态文本框
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
AddTextBox();
ViewState["Count"] = Convert.ToInt16(ViewState["Count"]) + 1;
}提交:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < Table1.Rows.Count; i++)
{
if (i > 0)
{
sb.Append("|" + ((TextBox)Table1.Rows[i].FindControl("tb" + i)).Text);
}
else
{
sb.Append(((TextBox)Table1.Rows[i].FindControl("tb" + i)).Text);
}
}
sb.ToString() 就是动态生成控件后所有信息内容,我把内容用“|” 分割。如果编辑时进行的绑定:
string txtFileNames = dt.Rows[0]["thFileName"].ToString(); //先前保存的内容
if (txtFileNames != "" && txtFileNames != null)
{
string[] txtFileName = txtFileNames.Split('|');
for (int i = 0; i < txtFileName.Length; i++)
{
TableRow tr = new TableRow();
TableCell tc1 = new TableCell();
TextBox txt = new TextBox();
txt.ID = "tb" + i;
txt.Font.Size = FontUnit.Point(9);
TableCell tc2 = new TableCell();
Label lab = new Label();
lab.ID = "lab" + i;
lab.Width = 50;
txt.Width = 200;
lab.Text = "文件" + (i + 1);
tc2.Controls.Add(lab);
tc1.Controls.Add(txt);
tr.Cells.Add(tc2);
tr.Cells.Add(tc1);
Table1.Rows.Add(tr);
if (!txtFileName[i].Equals(string.Empty))
{
((TextBox)Table1.Rows[i].FindControl("tb" + i)).Text = txtFileName[i];
}
}
}
前一段时间刚做了一个,你看看能用不
{ Int32 num = Convert.ToInt32(ViewState["Count"]);
for (int i = 1; i <= num; i++)
{
if (((TextBox)Page.FindControl("txtProduct" + i.ToString())).Text.Trim() != ""||
((TextBox)Page.FindControl("txtProduct" + i.ToString())).Text.Trim()!=null)
{
ArrLabel.Add(((TextBox)Page.FindControl("txtProduct" + i.ToString())).Text.Trim());
}
}
}
private void Button2_Click(object sender, System.EventArgs e)
{
ArrayList al = new ArrayList();
int num = Convert.ToInt32(ViewState["Count"]);
for (int i = 1; i <= num; i++)
{
if (((TextBox)this.FindControl("txtProduct" + i.ToString())).Text.Trim() != "")
{
al.Add(((TextBox)this.FindControl("txtProduct" + i.ToString())).Text.Trim());
}
}
//将输入的非空值保存到string[] a
string[] a = new string[al.Count];
al.CopyTo(a,0);
for(int j=0;j<a.Length;j++)
{
Response.Write(a[j].ToString());
Response.Write("<br>");
}
}