for (int i = 1; i <= 5; i++) { string text = string.Empty; string id = "mytextbox" + i; TextBox tb = Page.FindControl(id) as TextBox; if (tb != null && tb is TextBox) text = tb.Text; }
<table runat="server" id="tb"> foreach(Control c in tb.Controls) { }
我那个例子是在td上加runat="server"的,你要在table处加的话就要遍历table中的所有单元格 foreach (HtmlTableRow htr in tb.Rows) { foreach (HtmlTableCell htc in htr.Cells) { foreach (Control c in htc.Controls) { if (c is TextBox) { TextBox tb = c as TextBox; Response.Write(tb.Text + "<br>"); } } } }
Textbox[] t = new Textbox[]{ t1,t2,......};for(int i=0 i < t.Length ; i++)
t[i].Text=i+"";
<table>
<tr>
<td>
<asp:TextBox ID="txt_item1" runat="server" Width="450px"></asp:TextBox>
<asp:TextBox ID="txt_item2" runat="server" Width="450px"></asp:TextBox>
<asp:TextBox ID="txt_item3" runat="server" Width="450px"></asp:TextBox>
<asp:TextBox ID="txt_item4" runat="server" Width="450px"></asp:TextBox>
<asp:TextBox ID="txt_item5" runat="server" Width="450px"></asp:TextBox>
</td>
</tr>
</table>后台如何循环读取textbox的值 for (int i = 1; i <= 5;i++ )
{
TextBox mytextbox = new TextBox();
mytextbox.ID = "txt_item" + i.ToString();
string text = mytextbox.Text;
}为何这种方法取出来的值总是为空
<asp:TextBox ID="txt_item1" runat="server" Width="450px"> </asp:TextBox>
<asp:TextBox ID="txt_item2" runat="server" Width="450px"> </asp:TextBox>
<asp:TextBox ID="txt_item3" runat="server" Width="450px"> </asp:TextBox>
<asp:TextBox ID="txt_item4" runat="server" Width="450px"> </asp:TextBox>
<asp:TextBox ID="txt_item5" runat="server" Width="450px"> </asp:TextBox>
</td>后台:foreach(Control c in td.Controls)
{
if(c is TextBox)
{
TextBox tb = c as TextBox;
//tb.Text
}
}
{
TextBox mytextbox = this.FindControl("txt_item" + i) as TextBox ;
string text = mytextbox.Text;
}
{
Request.Params["txt_item" + i.ToString()].ToString()
}
for (int i = 1; i <= 5; i++)
{
string text = string.Empty;
string id = "mytextbox" + i;
TextBox tb = Page.FindControl(id) as TextBox;
if (tb != null && tb is TextBox)
text = tb.Text;
}
为什么会出现
“td.InnerHtml”引发了“System.NotSupportedException”类型的异常
“td.InnerText”引发了“System.NotSupportedException”类型的异常
<table runat="server" id="tb">
<tr>
<td>
所属问题:
</td>
<td>
<asp:DropDownList ID="ddl1" runat="server">
</asp:DropDownList></td>
</tr>
<tr>
<td>
选项1:
</td>
<td>
<asp:TextBox ID="txt_item1" runat="server" Width="450px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
选项2:
</td>
<td>
<asp:TextBox ID="txt_item2" runat="server" Width="450px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
选项3:
</td>
<td>
<asp:TextBox ID="txt_item3" runat="server" Width="450px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
选项4:
</td>
<td>
<asp:TextBox ID="txt_item4" runat="server" Width="450px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
选项5:
</td>
<td>
<asp:TextBox ID="txt_item5" runat="server" Width="450px"></asp:TextBox>
</td>
</tr>
<tr><td>
<asp:Button ID="btn_items" runat="server" Text="确定" CssClass="button" OnClick="btn_items_Click" /></td></tr>
</table>后台
protected void btn_items_Click(object sender, EventArgs e)
{
string quesid = ddl1.SelectedValue;
foreach(Control c in tb.Controls)
{
if (c is TextBox)
{
TextBox mytextbox = c as TextBox;
if (mytextbox.Text == "")
{
continue;
}
else
{
string sqlstr = "insert into Tb_Answer (AnswCont,QuesID) values ('" + mytextbox.Text + "','" + quesid + "')";
try
{
DBOperator.Exesql(sqlstr);
Response.Write("<script language='javascript'>alert('添加成功!');window.location.href = 'DCWJ.aspx'</script>");
}
catch (Exception)
{
Response.Write("<script language='javascript'>alert('添加失败!');</script>");
}
}
}
}
<table runat="server" id="tb">
foreach(Control c in tb.Controls)
{
}
{
foreach (HtmlTableCell htc in htr.Cells)
{
foreach (Control c in htc.Controls)
{
if (c is TextBox)
{
TextBox tb = c as TextBox;
Response.Write(tb.Text + "<br>");
}
}
}
}
建议你使用FindControl的方式,也就是我上面写那种,那么你现在这个问题应该不存在