C# 如何在 .ascx 页面中显示/隐藏表格行 <tr>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1701949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:06:15  来源:igfitidea点击:

How to show/hide table row <tr> in .ascx page

c#asp.netjavascripthtml

提问by Jango

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

.ascx 页面

<tr id="trComment" runat="server">
    <td style="vertical-align:top; text-align:left;">
        <%#ConfigUtil.FieldLabels["PIComments"]%>
        :
    </td>
    <td>
        <%= Test.Comment %>
    </td>
</tr>

采纳答案by Abel

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).

您的原始代码不起作用,不是因为它不正确,而是因为您可能有更多的地方trComment(在这种情况下它应该出错),或者因为您当前的代码在某种模板中(在 a GridView、 a 中Repeater)。后者最有可能,因为您使用数据语句 ( <%#),它通常放置在数据绑定控件模板中(但不一定)。

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:

统一而轻松地解决这个问题的一种方法(存在多种方法,无论如何最好不要使用文字表)是使用asp:PlaceHolder,它不会留下 HTML“痕迹”,但可用于切换任何 HTML/ASP 块。网络代码:

<!-- 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;
}

回答by Bob

Try

尝试

trComment.Style.Add("display", "none");

回答by mut tony

<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

然后在您的 Page_Load() 方法中找到您的元素并将可见性设置为 true 或 false,如下所示

protected void Page_Load(object sender, EventArgs e)
{
   trComment.Visible = false; //or trComment.Visible = true; as you wish
}

Hope this helps you

希望这对你有帮助

回答by drinky

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>