简体   繁体   English

如何访问文本框,使用asp.net Web表单从代码后面的更新面板内标签

[英]How to access textbox, label inside update panel from code behind using asp.net web forms

I have a few controls defined inside an update panel which are bound to repeater control. 我在更新面板中定义了一些绑定到转发器控件的控件。 I need to hide and show the user name and country based on anonymous fields, but the problem is that I'm not able to access the controls defined inside update panel programmatically. 我需要隐藏并显示基于匿名字段的用户名和国家/地区,但问题是我无法以编程方式访问更新面板中定义的控件。

How can I access these controls, I looked on the net also but couldn't find many references 我怎样才能访问这些控件,我也在网上查看但找不到很多引用

Below is the code from aspx page and .cs page 下面是aspx页面和.cs页面的代码

<asp:UpdatePanel ID="updPnlComments" runat="server">
<ContentTemplate>
<table border="0" width="100%" ><tr><td valign="top">
    <asp:Repeater ID="rptCommentList" runat="server" EnableViewState="false">
       <ItemTemplate>
            <div id="divComPostDate" class="ArticlePubDate">
                <asp:Label ID="lblComDateAdded" runat="server" Text="Added"></asp:Label> 
                <asp:Label ID="lblComPostDate" runat="server" Text='<%# FormatCommentDate(Eval("comPostDate")) %>'></asp:Label>
            </div>
            <div id="divComMSGDetail" class="PostCommentMSG">
                <asp:Label ID="lblComMSGDetails"  runat="server" Text='<%# Eval("comMessage") %>'></asp:Label> 
            </div>
            <div id="divComUserName" class="ComUserName">
                <asp:Label ID="lblComUserName" runat="server" Text='<%# Eval("comFullName") %>'></asp:Label>,
                <asp:Label ID="lblComCountry" runat="server" Text='<%# Eval("comCountry") %>'></asp:Label>
                <asp:Label ID="lblUserAnonymous" runat="server" Text='<%# showUserName(Eval("comAnonymous")) %>' Visible=false></asp:Label>
            </div>
            <div id="divThinlLine" class="ThinLine" ></div>
        </ItemTemplate>
     </asp:Repeater>
  </td></tr><tr><td>
    <table border="0" width="90%" ><tr><td align="center" valign="bottom" height="50px">
      <table border="0"><tr><td align="center" >
             <uc1:PagerControl ID="PagerControl1" runat="server"  CssClass="gold-pager"  PageMode="LinkButton"  />
      </td></tr></table>
    </td></tr></table>
</td></tr></table>
</ContentTemplate>
</asp:UpdatePanel>

Code Behind 代码背后

protected string FormatCommentDate(object dt)
{
    string date;
    date  = String.Format("{0:hh:mm, MMMM dd, yyyy}", dt);
    return date;
}

protected string showUserName(object userName)
{
    String str=null;
    try
    {
        Boolean isUserAnonymous = Convert.ToBoolean(userName);

        if (isUserAnonymous == true)
        {
            // Not able to access lblComUserName CONTROL here

        }

    }
    catch (Exception ex)
    {

    }

    return str;

}

Function To bind repeater control with Pager and Repeater control 功能使用Pager和Repeater控件绑定转发器控件

protected void getCommentsList(int iArticleID)
{
    try
    {

        PagerControl1.PageSize = 4;
        //PagerControl1.TotalItems = 1;
        //PagerControl1.PageMode = 4;
        PagerControl1.DisplayEntriesCount = 5;
        //Will show 2 links after ...
        PagerControl1.EdgeEntriesCount = 0;
        DataSet ds = DataProvider.GetCommentList(iArticleID);
        DataView dv = ds.Tables[0].DefaultView;

        //pass the datatable and control to bind
        PagerControl1.BindDataWithPaging(rptCommentList, dv.Table);

    }
    catch (Exception ex)
    {
        HttpContext.Current.Response.Redirect("Message.aspx?msg=Invalid Request");
    }

The problem isn't with UpdatePanel , but with the Repeater . 问题不在于UpdatePanel ,而在于Repeater Controls inside the update panel can be directly accessed within the scope of the page (such as the repeater itself), whilst controls within the repeater have to be "found" during or after they have been bound. 更新面板内的控件可以在页面范围内直接访问(例如转发器本身),而转发器中的控件必须在绑定期间或之后“找到”。 Your code within the repeater is a template for many items and not specific to any one. 转发器中的代码是许多项目的模板,并不是特定于任何项目的模板。

I'd suggest adding an ItemDataBound event to your repeater and perform the logic inside this event as each item is being bound to the repeater. 我建议将一个ItemDataBound事件添加到转发器中,并在每个项目绑定到转发器时执行此事件中的逻辑。

<asp:Repeater ID="rptCommentList" runat="server" EnableViewState="false"
     OnItemDataBound="rptCommentList_ItemDataBound">

protected void rptCommentList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
    {
        // get the data item
        MyObject myObject = (MyObject)e.Item.DataItem;

        // find the label
        Label lblComUserName = (Label)e.Item.FindControl("lblComUserName");

        // do the magic!
        if (myObject.comAnonymous)
             lblComUserName.Visible = false;
    }
}

Obviously, replace MyObject with the object type of the collection/list/table you are binding to the repeater in the first place. 显然,首先用您绑定到转发器的collection / list / table的对象类型替换MyObject

Hope that helps. 希望有所帮助。

If the control is inside the repeater 如果控件在转发器内

 Label lbl=(Label)Repeater1.FindControl("lbl1");

should do the job. 应该做的工作。 Otherwise you can directly access the controls. 否则,您可以直接访问控件。 If not the page has errors and not compiling 如果不是页面有错误而没有编译

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asp.net:如何访问从后面的代码创建的表的单元格内的文本框的内容 - asp.net: how to access contents of a textbox inside the cell of a table that was created from the code behind 无法从asp.net背后的代码访问选项卡面板内的GridView - Not able to access gridview inside tab panel from code behind asp.net 使用 ajax 调用后面的 asp.net web 表单代码 - call asp.net web forms code behind using ajax 如何在线程(ASP.NET Web窗体)中访问页面控件(标签,计时器等) - How access page controls (label, timer, etc) inside a thread (ASP.NET Web Forms) 如何从背后的代码绑定ASP.NET Web Forms C#应用程序中下拉列表中的“ SelectedValue”? - How to bind the 'SelectedValue' in dropdown in ASP.NET Web Forms C# application from code behind? 如何从代码后面动态添加 ASP.NET Web Forms 中的怪癖模式元标记? - How to dynamically add quirks mode meta-tags in ASP.NET Web Forms from code behind? Asp.net-将Textbox字段更改为gridview中位于后面代码中的标签 - Asp.net - Changing Textbox field into label in gridview at code behind ASP.Net更新面板不读取背后的代码中的全局变量 - ASP.Net Update Panel not reading Global Variables in code behind 当用户使用ASP.NET从Web窗体的DropDownList中选择其他控件时,如何显示TextBox - How TextBox is shown when user select other from DropDownList in web forms using ASP.NET 在 asp.net web 表单上更新或显示进度标签 - Update or show progress label on asp.net web forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM