c# - Asp.Net/C# - 如何获取嵌套在 Repeater 中的 Label 控件的文本?

标签 c# asp.net repeater

我正在尝试获取中继器内的标签文本,但我一直收到 NullPointerException

所有数据都来自数据库,而且是正确的。

当我点击 LinkBut​​ton 时,我想为下一位代码使用标签文本。

Aspx 页面:

   <asp:Repeater ID="RepeaterDepartmentParent" runat="server">
       <ItemTemplate>
                <div id="outerDiv" class="col-lg-3 col-xs-6" runat="server">
                        <!-- small box -->
                        <div>
                            <div class="inner">

                                <p>
                                   <%# DataBinder.Eval(Container.DataItem, "Department_Namestr")%>
                                </p>
                            </div>


                            <asp:Label ID="lblDepartmentId" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Department_Idint")%>' Visible="true"></asp:Label>

                            <asp:LinkButton ID="linkChildDepartment" CommandName="Click" runat="server" CssClass="small-box-footer" OnClick="linkChildDepartment_Click">More info<i class="fa fa-arrow-circle-right"></i></asp:LinkButton>

                        </div>
                </div><%--<%-- ./col -->--%>
       </ItemTemplate>
       </asp:Repeater>

代码隐藏:

  protected void Page_Load(object sender, EventArgs e)
    {
        DataSet dsParentDepartment = null;
        dsParentDepartment = objDepartmentBL.viewDepartmentparent();
         RepeaterDepartmentParent.DataSource = dsParentDepartment.Tables[0];
        RepeaterDepartmentParent.DataBind();

    }

  protected void linkChildDepartment_Click(Object sender, EventArgs e)
    {
        //what to write here??
        //i have tried the bellow code but it gives me every data in that loop but i
        //want the single data for a link button click.
        //foreach (RepeaterItem item in RepeaterDepartmentParent.Items)
       // {
          //  Label myLabel = (Label)item.FindControl("lblDepartmentId");
         //   myLabel.Text = Id;
        //}
       //edited code that works properly
        LinkButton linkChildDepartment = (LinkButton)sender;
        RepeaterItem item = (RepeaterItem)linkChildDepartment.NamingContainer;
        Label myLabel = (Label)item.FindControl("lblDepartmentId");

    }

如何正确引用链接按钮标签文本?

最佳答案

您可以使用 NamingContainer 属性获取 RepeaterItem 的引用。从那里到您的标签很近:

protected void linkChildDepartment_Click(Object sender, EventArgs e)
{
    LinkButton linkChildDepartment = (LinkButton) sender;
    RepeaterItem item = (RepeaterItem) linkChildDepartment.NamingContainer;
    Label myLabel = (Label)item.FindControl("lblDepartmentId");
     // ...
}

关于c# - Asp.Net/C# - 如何获取嵌套在 Repeater 中的 Label 控件的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25159613/

相关文章:

c# - 如何在 C# Windows 窗体中隐藏关闭按钮

c# - 从字节值返回特定位作为 bool 值

c# - 如果是持久(保持事件)连接,为什么我会在 Web 请求中到达 endOfStream?

asp.net - 中继器中只有一个单选按钮选择

c# - 其他控件内的中继器

java - RabbitMQ 从 C# 解析 "client_properties" header

c# - 如何让子面板填充父面板中的剩余空间?

asp.net - 指定多行文本框的最大长度

c# - UseSubmitBehavior ="false"时停止 asp.net 回发

c# - 将通用列表绑定(bind)到转发器 - ASP.NET