后台生成控件后,取不到值

vivian_lanlan 2010-05-12 01:43:18
----生成控件
TableRow row = new TableRow();
TableCell cell = new TableCell();


TextBox tebox = new TextBox();
tebox.ID = "txtid001";
tebox.Text = "已找到值";
cell.Controls.Add(tebox);
row.Cells.Add(cell);
this.Table1.Rows.Add(row);
--取值
TextBox te = (TextBox)this.FindControl("txtid001");

Response.Write(te.Text);--这里发生错误,空异常
...全文
129 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
vivian_lanlan 2010-05-12
  • 打赏
  • 举报
回复
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Table ID="Table1" runat="server">
</asp:Table>
<asp:Button runat="server" ID="btn1" Text="测试" OnClick="btn1_Click" />
</div>
</form>
</body>
</html>










protected void Page_Load(object sender, EventArgs e)
{
//if (!IsPostBack)
//{
TableRow row = new TableRow();
TableCell cell = new TableCell();

TextBox tebox = new TextBox();
tebox.ID = "txtid001";
tebox.Text = "已找到值";

DropDownList dl = new DropDownList();
dl.Items.Add(new ListItem("1", "1"));
dl.Items.Add(new ListItem("2", "2"));
dl.Items.Add(new ListItem("3", "3"));
cell.Controls.Add(dl);
cell.Controls.Add(tebox);
row.Cells.Add(cell);

this.Table1.Rows.Add(row);
//}
}
protected void btn1_Click(object sender, EventArgs e)
{
TextBox te = (TextBox)this.FindControl("txtid001");
DropDownList dl = (DropDownList)this.FindControl("dl");
Response.Write(dl.SelectedValue);
//Response.Write("取值:" + Request["txtid001"].ToString());
//Response.Write("下拉框:" + Request["dl"].ToString());
//Response.Write(te.Text);
}
jzcldy 2010-05-12
  • 打赏
  • 举报
回复
lz 是你自己哪里写错了吧 你的代码没错啊
<body>
<form id="form1" runat="server">
<div>
<asp:Table ID="Table1" runat="server" ></asp:Table>
</div>
<asp:Button ID="bt" runat="server" Text="sumbit" onclick="bt_Click" />
</form>
</body>


public partial class sounds_Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TableRow row = new TableRow();
TableCell cell = new TableCell();


TextBox tebox = new TextBox();
tebox.ID = "txtid001";
tebox.Text = "已找到值";
cell.Controls.Add(tebox);
row.Cells.Add(cell);
this.Table1.Rows.Add(row);


}
protected void bt_Click(object sender, EventArgs e)
{
TextBox te = (TextBox)this.FindControl("txtid001");

Response.Write(te.Text);
}
}


我运行了下 可以正常显示
你是不是 前面 没加 form啊
你吧你前面 的代码贴上来
vivian_lanlan 2010-05-12
  • 打赏
  • 举报
回复
上面的都不对
mngzilin 2010-05-12
  • 打赏
  • 举报
回复
Response.Write(Request.Form["txtid001"].ToString());
q107770540 2010-05-12
  • 打赏
  • 举报
回复
mark

try;

TextBox te = (TextBox)Table1.Rows[0].FindControl("txtid001");

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧