Lea*_*ing 5 c# asp.net webforms asp.net-ajax code-behind
我在更新面板中定义了一些绑定到转发器控件的控件.我需要隐藏并显示基于匿名字段的用户名和国家/地区,但问题是我无法以编程方式访问更新面板中定义的控件.
我怎样才能访问这些控件,我也在网上查看但找不到很多引用
下面是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>
Run Code Online (Sandbox Code Playgroud)
代码背后
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;
}
Run Code Online (Sandbox Code Playgroud)
功能使用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");
}
Run Code Online (Sandbox Code Playgroud)
问题不在于UpdatePanel,而在于Repeater。更新面板内的控件可以在页面范围内直接访问(例如转发器本身),而转发器内的控件必须在绑定期间或之后“找到”。转发器中的代码是许多项目的模板,而不是特定于任何一项。
我建议ItemDataBound向中继器添加一个事件,并在每个项目绑定到中继器时执行该事件内的逻辑。
<asp:Repeater ID="rptCommentList" runat="server" EnableViewState="false"
OnItemDataBound="rptCommentList_ItemDataBound">
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
显然,请替换MyObject为您首先绑定到转发器的集合/列表/表的对象类型。
希望有帮助。