动态将ID分配给asp:Repeater中的文本框(服务器端控件)

普奈特·乔瓦(Puneet Chawla)

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

绑定数据后,隐藏字段将获得用户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;
    }

}

}
神灵

这对我有用。请注意,我从转发器中删除了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;
        }
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在MVC的文本框中从服务器端设置动态文本

关于将模型数据分配给asp.net MVC中的文本框

如何将值分配给在angular js中动态生成的矩阵中的文本框字段?

将CKEditor分配给动态创建的文本框元素

在ASP.NET服务器端还是客户端中的HTML控件?

如何将文本框中的时间值分配给变量?

尝试将文本框中的值分配给变量时出现null异常

将文本框值分配给标签,这是在jquery中编码的正确方法吗?

将值分配给加载页面中的文本框时无法更新数据

将文本框值分配给 HTML Body 中的变量并传递给多个函数

如何将列表框项目保存在asp.net [服务器端]的文本文件中?

将值从服务器分配给目标 -c 中的整数

在ASP动态表中查找文本框控件

将事件侦听器分配给ASP.NET中的动态创建的HTML按钮

如何将文本框值分配给 wpf 中的另一个类

动态将文本框插入到用户控件中

ASP.net Repeater 如何更改标题模板字幕服务器端?

ASP.NET服务器端或客户端的验证控件?

如何在 Asp.Net 的代码隐藏文件中访问服务器端控件?

将文本框值分配给多个表/查询字段值

我如何将文本框值分配给 Oracle SQL 查询 C#

使用jQuery将值分配给GridView文本框

如何将值分配给html单元格内的文本框

如何将值随机分配给文本框加载

以单独的形式将文本框值分配给字段

在ASP.NET/C#中检查复选框服务器控件时隐藏文本框-使用Jquery或Javascript

如何在MVC 2中使用ASP服务器控件,如文本框,按钮,复选框列表网格视图?

转发器:是否在项目控件中将服务器端ID绑定到数据?

代理服务器是否还将内部IP分配给客户端?