在 asp:Repeater 中动态地将 Id 赋予文本框(服务器端控件)

Dynamically give Id to textbox (Server side control) in asp:Repeater

在我的 <asp:Repeater></asp:Repeater> - 有一个隐藏字段、文本框和按钮。

绑定数据后,隐藏字段会获取User Id,如下代码

<asp:HiddenField ID="hide" Value='<%#Eval("UserId")%>' runat="server"/>

将调用 ItemDataBound 事件,在此函数中,我从隐藏字段获取值并将该值连接为文本框中的 ID。像下面的代码

            TextBox txt = (TextBox)e.Item.FindControl("txtReplyArea");
            HiddenField hf = (HiddenField)e.Item.FindControl("hide");//1
            txt.ID = "txtReplyArea" + hf.Value;//txtReplyArea1

假设只有一条记录来自数据库,并且他们的UserId是1。那么textbox Id应该是"textReplyArea1"。从现在开始,一切都正确了。

我不确定,这是将动态 Id 赋予中继器控件的正确方法,但我认为它是正确的。

问题 -

当我单击按钮时,我通过从中继器中找到控件从中继器和文本框中获取一个项目,然后它显示为空。

        int areaId = int.Parse((sender as Button).CommandArgument);//1
        string id="txtReplyArea"+areaId;//txtReplyArea1

        foreach (RepeaterItem item in repeaterBlog.Items)
        {
            TextBox tb = item.FindControl(id) as TextBox;//tb = null
        }

aspx页面代码

<%@ Page Title="Messages" Language="C#" MasterPageFile="~/Menu.master" AutoEventWireup="true" CodeFile="Messages.aspx.cs" Inherits="Messages" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <div style="width:70%;">
        <asp:Repeater ID="repeaterBlog" runat="server" OnItemDataBound="repeaterBlog_ItemDataBound">
        <HeaderTemplate>
            <table>
            </HeaderTemplate>
                <ItemTemplate>
                    <tr >
                        <td>
                            <asp:HiddenField ID="hide" Value='<%#Eval("UserId")%>' runat="server"/>
                            <asp:TextBox  ID="txtReplyArea" runat="server" TextMode="Multiline" Columns="70" Rows="8" Visible="false"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td style="margin-left:47%;">
                            <asp:Button ID="btnReply" runat="server" Text="Reply" OnClick="btnReplyClicked" AutoPostBack="True" CommandArgument='<%#Eval("UserId")%>'/>
                        </td>
                    </tr>

                </ItemTemplate>
                <FooterTemplate>
            </table>
                </FooterTemplate>
        </asp:Repeater>

    </div>
</asp:Content>

代码aspx.cs

            SqlCommand cmd;
            SqlDataReader sdr;
           protected void Page_Load(object sender, EventArgs e)
           {
              if(!IsPostBack)
              { 
    String cs = ConfigurationManager.ConnectionStrings["myWebsite"].ConnectionString;
    using (SqlConnection con = new SqlConnection(cs))
        {

            cmd = new SqlCommand("select * from ContactMessage", con);
            con.Open();
            sdr = cmd.ExecuteReader();

            repeaterBlog.DataSource = sdr;
            repeaterBlog.DataBind();
            con.Close();
        }
    }

}

public void repeaterBlog_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        TextBox txt = (TextBox)e.Item.FindControl("txtReplyArea");
        HiddenField hf = (HiddenField)e.Item.FindControl("hide");
        txt.ID = "txtReplyArea" + hf.Value;
    }
}


protected void btnReplyClicked(object sender, EventArgs e)
{
    int areaId = int.Parse((sender as Button).CommandArgument);
    string id="txtReplyArea"+areaId;

    foreach (RepeaterItem item in repeaterBlog.Items)
    {
        TextBox tb = item.FindControl(id) as TextBox;
    }

}

}

这对我有用。请注意,我从中继器中删除了 EnableViewState 并添加了 OnItemDatabound 事件。

<asp:Repeater ID="repeaterBlog" runat="server" OnItemDataBound="repeaterBlog_ItemDataBound">
    <HeaderTemplate>
        <table>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <asp:HiddenField ID="hide" Value='<%#Eval("UserId")%>' runat="server" />
                <asp:TextBox ID="txtReplyArea" runat="server" TextMode="Multiline" Columns="70" Rows="8"
                    Visible="false"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="margin-left: 47%;">
                <asp:Button ID="btnReply" runat="server" Text="Reply" OnClick="btnReplyClicked" AutoPostBack="True"
                    CommandArgument='<%#Eval("UserId")%>' />
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>

    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("UserId");
        DataRow dr = dt.NewRow();
        dr[0] = 34;
        dt.Rows.Add(dr);
        repeaterBlog.DataSource = dt;
        repeaterBlog.DataBind();
    }

    public void repeaterBlog_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            TextBox txt = (TextBox)e.Item.FindControl("txtReplyArea");
            HiddenField hf = (HiddenField)e.Item.FindControl("hide");
            txt.ID = "txtReplyArea" + hf.Value;
        }
    }


    protected void btnReplyClicked(object sender, EventArgs e)
    {
        int areaId = int.Parse((sender as Button).CommandArgument);
        string id = "txtReplyArea" + areaId;

        foreach (RepeaterItem item in repeaterBlog.Items)
        {
            TextBox tb = item.FindControl(id) as TextBox;
        }
    }

你可以这样试试

<asp:TextBox ID='<%# "txtReplyArea" + Convert.ToString(Eval("UserId")%>)' runat="server" TextMode="Multiline" Columns="70" Rows="8" Visible="false"></asp:TextBox>