我在按钮点击事件上动态生成多个文本框。而且我限制这些文本框的创建最多5个计数。但是我无法跟踪动态生成的文本框中插入的值以将其插入到数据库表中。 这就是我在做什么 -
<table border="1" width="1000px">
<tr>
<td class="style1">Add Text Box: </td>
<td class="style2">
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Add More"
onclick="Button1_Click" /><br />
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</td>
</tr>
<tr><td colspan="2" align="center">
<asp:Button ID="Button2" runat="server" Text="Submit" onclick="Button2_Click" />
<br />
</td></tr>
</table>
我的cs代码是 -
public partial class DynamicTextbox : System.Web.UI.Page
{
General_Logic g1 = new General_Logic();
DataTable dt = new DataTable();
int rows = 0;
List<string> ControlIdList = new List<string>();
int Counter = 1;
protected override void LoadViewState(object SavedState)
{
base.LoadViewState(SavedState);
ControlIdList = (List<string>)ViewState["ControlIdList"];
foreach (string Id in ControlIdList)
{
Counter++;
TextBox tb = new TextBox();
tb.ID = Id;
LiteralControl linebreak = new LiteralControl();
PlaceHolder1.Controls.Add(tb);
PlaceHolder1.Controls.Add(linebreak);
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Counter <= 4)
{
Counter++;
TextBox tb = new TextBox();
tb.ID = "TextBox" + Counter;
tb.Text = "";
LiteralControl linebreak = new LiteralControl("<br />");
PlaceHolder1.Controls.Add(tb);
PlaceHolder1.Controls.Add(linebreak);
ControlIdList.Add(tb.ID);
ViewState["ControlIdList"] = ControlIdList;
}
else
{
Button1.OnClientClick = null;
Response.Write("<script>alert('Maximum Entry is 5');</script>");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
try
{
string s = null;
for (int parcount = 0; parcount == parcount; parcount++)
{
s = Request.Form["Textbox" + parcount.ToString()];
if (s == null)
{
rows = g1.ExecDB("insert into tbl_products(product1) values('" + txt1.Text.ToString() + "')");
break;
}
else
{
//Label1.Text = s.ToString();
Response.Write(s);
rows = g1.ExecDB("insert into tbl_products(product1,product2,product3,product4,product5) values('" + txt1.Text.ToString() + "','" + s.ToString() + "','" + s.ToString() + "','" + s.ToString() + "','" + s.ToString() + "')");
}
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
}
在编写了这么多代码后,我只能从第一个文本框插入数据,而不能从动态生成的文本框中插入数据。我在动态文本框的数据库表中获取null值。请指导我在我的代码出错的地方?
答案 0 :(得分:1)
将s = Request.Form["Textbox" + parcount.ToString()];
中的文本框更改为 TextBox ,即
s = Request.Form["TextBox" + parcount.ToString()];
<强>原因:强>
请原因您创建“TextBox”并通过“Textbox”检索。
在看到你的逻辑后,我需要建议你遵循复杂的逻辑。如果我理解你的逻辑,下面的代码会帮助你。
protected void Button2_Click(object sender, EventArgs e)
{
try
{
string s = null;
int limit = 4;
string[] DBvalues = new string[5];
for (int parcount = 0; parcount <=limit; parcount++)
{
s = Request.Form["TextBox" + parcount.ToString()];
if (parcount == 0)
{
DBvalues[parcount] = txt1.Text;
}
else
{
DBvalues[parcount] = Request.Form["TextBox" + (parcount + 1).ToString()];
}
//}
}
//converting null values to any default value.i take NULL
for (int i=0; i <= 4;i++ )
{
if (DBvalues[i] == null)
{
DBvalues[i] = "NULL";
}
}
rows = g1.ExecDB("insert into tbl_products(product1,product2,product3,product4,product5) values('" + DBvalues[0].ToString() + "','" + DBvalues[1].ToString() + "','" + DBvalues[2].ToString() + "','" + DBvalues[3].ToString() + "','" + DBvalues[4].ToString() + "')");
Response.Write(DBvalues);
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}