[英]asp.net: how to access contents of a textbox inside the cell of a table that was created from the code behind
I'm having trouble accessing the contents of a textbox inside the cell of a table that was created from the code behind. 我无法访问从后面的代码创建的表的单元格内的文本框的内容。 In my small program, the table is pre-populated with a student's name (cell A1).
在我的小程序中,表中预先填充了学生的姓名(单元格A1)。 A textbox is added to the cell below it (cell A2).
将一个文本框添加到其下方的单元格(单元格A2)。 The user enters Pass or Fail into the textbox and clicks submit.
用户在文本框中输入“通过”或“失败”,然后单击“提交”。 At that point a message should read, "The students status was changed to (whatever the user entered)".
那时应该显示一条消息,“学生状态已更改为(无论用户输入了什么)”。 This is the problem -- since the textbox ID (student1) is assigned in the code behind, the textbox ID does not yet exist in the context.
这就是问题所在-由于文本框ID(student1)是在后面的代码中分配的,因此文本框ID在上下文中尚不存在。
//Code Behind
protected void Page_Load(object sender, EventArgs e)
{
Label nameStudent = new Label()
{
Text = "Annie McDonald"
};
TableCell nameCell = new TableCell();
nameCell.Controls.Add(nameStudent);
NameRow.Cells.Add(nameCell);
TextBox status = new TextBox()
{
ID = "student1",
Text = "Pass or Fail"
};
TableCell statusCell = new TableCell();
statusCell.Controls.Add(status);
StatusRow.Cells.Add(statusCell);
}
protected void sumbitChange_Click(object sender, EventArgs e)
{
Confirm.InnerText = "The students status was changed to " + student1.Text;
}
<body>
<form id="form1" runat="server">
<asp:Table ID="StudentRoster" runat="server">
<asp:TableRow ID="NameRow" runat="server" />
<asp:TableRow ID="StatusRow" runat="server" />
</asp:Table>
<asp:Button ID="sumbitChange" text="Submit" runat="server" OnClick="sumbitChange_Click"/>
<p id="Confirm" runat="server"></p>
</form>
</body>
You need to find the TextBox inside the TableRow. 您需要在TableRow中找到TextBox。 Try the code below:
请尝试以下代码:
protected void sumbitChange_Click(object sender, EventArgs e)
{
TextBox student1 = StatusRow.FindControl("student1") as TextBox;
Confirm.InnerText = "The students status was changed to " + student1.Text;
}
I have tested and it is working. 我已经测试过,并且可以正常工作。
In my opinion you misunderstand the way ASP.NET WebForms works. 我认为您会误解ASP.NET WebForms的工作方式。 You usually declare the controls in the
*.aspx
or *.ascx
file, assign them an ID and access them via code behind ( *.aspx.cs
or *.ascx.cs
). 通常,您可以在
*.aspx
或*.ascx
文件中声明控件,为它们分配一个ID,并通过后面的代码( *.aspx.cs
或*.ascx.cs
)访问它们。
In your case this would look like this: 您的情况如下所示:
* .aspx file * .aspx文件
<form id="form1" runat="server">
<p class="someFancyInformationStyle"><asp:Label ID="statusLabel" runat="server"></asp:Label></p>
<table>
<tr>
<th>Name</th>
<th>Status</th>
</tr>
<tr>
<td><asp:Label ID="nameLabel" runat="server"></asp:Label></td>
<%-- Or even better, use a drop down --%>
<td><asp:TextBox ID="statusTextBox" runat="server"></asp:TextBox></td>
</tr>
...
</table>
</form>
Code behind file: 文件后面的代码:
protected void sumbitChange_Click(object sender, EventArgs e)
{
statusLabel.Text = "The students status was changed to " + statusTextBox.Text;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.