动态地给Id文本框(服务器端控制)在asp:Repeater

本文关键字:控制 asp Repeater 服务器端 Id 文本 动态 | 更新日期: 2023-09-27 18:09:07

在我的<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。然后文本框Id应该是"textReplyArea1"。从现在开始,都是正确的。

我不确定,这是正确的方式给动态Id中继器控制,但我认为,这是正确的。

问题-

当我点击按钮,我得到一个项目从重复器和文本框从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;
    }
}
}

动态地给Id文本框(服务器端控制)在asp:Repeater

这对我很有效。请注意,我从重复器中删除了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>