简体   繁体   English

asp.net:如何访问从后面的代码创建的表的单元格内的文本框的内容

[英]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.

相关问题 如何访问文本框,使用asp.net Web表单从代码后面的更新面板内标签 - How to access textbox, label inside update panel from code behind using asp.net web forms 如何在后面的代码中访问DetailsView的ItemTemplate的TextBox? ASP.Net C# - How to access a DetailsView's ItemTemplate's TextBox in code behind? ASP.Net C# Asp.net从aspx的文本框中获取值以进行代码隐藏 - Asp.net get value from Textbox in aspx to code behind 从代码隐藏在 ASP.NET aspx 中绑定 TextBox - Bind TextBox in ASP.NET aspx from Code Behind 无法从asp.net背后的代码访问选项卡面板内的GridView - Not able to access gridview inside tab panel from code behind asp.net c#从asp.net页面访问背后的代码 - c# Code behind access from asp.net page 如何从asp.net中的模式对话框访问click事件代码背后的代码 - How to access the code behind click event code from a modal dialog box in asp.net 如何从asp.net中的服务器端代码向动态创建的文本框控件添加事件处理程序 - How to add event handlers to a dynamically created textbox control from server side code in asp.net asp.net后面的表代码 - asp.net Table code behind 如何仅允许一组IP使用ASP.NET从后面的代码访问站点? - How to allow only a group of IPs access the site from code behind using ASP.NET?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM