I tried this, but could not get through:-
code behind
protected HtmlTableRow trComment;
protected void 开发者_开发知识库Page_Load(object sender, EventArgs e)
{
//Show/Hide table rows (TR)
trComment.Visible = ConfigUtil.DisplaySummaryComment;
}
.ascx page
<tr id="trComment" runat="server">
<td style="vertical-align:top; text-align:left;">
<%#ConfigUtil.FieldLabels["PIComments"]%>
:
</td>
<td>
<%= Test.Comment %>
</td>
</tr>
Your original code doesn't work, not because it's incorrect, but because you probably have more places with trComment
(in which case it should error) or because your current code is inside a template of some sort (in a GridView
, a Repeater
). The latter is most likely, because you use a data-statement (<%#
), which is commonly placed in a databound control template (but not necessarily).
One way to solve this uniformly and easily (many ways exist and it's probably best not to use literal tables anyway) is to use an asp:PlaceHolder
, which does not leave HTML "traces", but can be used to toggle any block of HTML / ASP.NET code:
<!-- toggle through OnLoad (can use ID as well) -->
<asp:PlaceHolder runat="server" OnLoad="MakeVisibleOrNot">
<tr>
...
</
</asp:PlaceHolder>
in the code behind
protected void MakeVisibleOrNot(object sender, EventArgs e)
{
((Control) sender).Visible = ConfigUtil.DisplaySummaryComment;
}
<tr id="trComment" runat="server">
<td>
</td>
</tr>
Then in your Page_Load() method find your element and set visibility true or false like below
protected void Page_Load(object sender, EventArgs e)
{
trComment.Visible = false; //or trComment.Visible = true; as you wish
}
Hope this helps you
Try
trComment.Style.Add("display", "none");
This also works with no code behind
<asp:PlaceHolder runat="server" Visible ='<%# Convert.ToBoolean(Session["sess_isArtist"].ToString() == "1" || Session["sess_isBeneficiary"].ToString() == "1" ? "true": "false") %>'>
<tr>
...
</
</asp:PlaceHolder>
精彩评论