如何进入放置在中继器内部的文本框

本文关键字:内部 文本 中继器 何进入 | 更新日期: 2023-09-27 18:37:18

所以,我有一个中继器,可以从数据库中打印出类别名称,每个类别名称下面都有TextBox和按钮,以便我可以编辑它的名称如果我愿意,但是......是的,总有一个但是。我只是无法达到重复TextBox,它告诉我TextBox_Update在当前上下文中不存在。

    protected void Button1_Click(object sender, EventArgs e)
{
    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
    {
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandText = @"UPDATE [categories] SET [category_name] = @category_name WHERE [category_id] = @category_id";
        cmd.Parameters.AddWithValue("@category_name", TextBox1.Text);
        cmd.Parameters.AddWithValue("@category_id", Request["id"]);
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }
    Response.Redirect("default.aspx");
}

这是我尝试更新它的方式,但是...告诉我textbox根本不存在。

这是它在 html 中的样子

<asp:Repeater ID="Repeater_Categories" runat="server" >
    <HeaderTemplate>
       <div class="insertPost">
            <h1>Categories</h1>
           <hr />
    </HeaderTemplate>
    <ItemTemplate>
           <div class="category">
                <h3><%# Eval("category_name") %></h3>
                <div class="categoryEdit">
                    <asp:TextBox ID="TextBox1" runat="server"     ValidationGroup="3"></asp:TextBox>
                    <br />
                    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" ValidationGroup="3" />
                    <a href="?action=delete&id=<%# Eval("category_id") %>">Del</a>
                    <br />
                </div>
           </div>
    </ItemTemplate>
    <FooterTemplate>
</div>
    </FooterTemplate>
</asp:Repeater>

可能做了一些非常错误的事情,我迫切需要帮助;<</p>

@Edit

修复了文本框名称。

如何进入放置在中继器内部的文本框

应搜索文本框以获取引用,然后使用该引用更新其属性。

TextBox txt1 = Repeater_Categories.FindControls("TextBox1") as TextBox;