C# 当visible 设置为true 时,不显示Asp:Label?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/534045/
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-04 07:08:20  来源:igfitidea点击:

Asp:Label is not shown when visible is set to true?

c#asp.net

提问by Xaisoft

I have a simple web form which has a couple list boxes and a search button. When the button is clicked, it returns a DataSet. If the dataset contains records, I set the asp:label which is initially set to false to true, but this is not happening. If the dataset has records and the visible property is set to true, the label still does not show up.

我有一个简单的 Web 表单,它有几个列表框和一个搜索按钮。单击按钮时,它返回一个数据集。如果数据集包含记录,我将最初设置为 false 的 asp:label 设置为 true,但这并没有发生。如果数据集有记录并且visible 属性设置为true,则标签仍然不显示。

I have also tried putting the label and a couple other controls in an html table and setting a runat="server" attribute on the table and changing the visibility on that, but it does not show either.

我还尝试将标签和其他几个控件放在 html 表中,并在表上设置 runat="server" 属性并更改其可见性,但它也没有显示。

Here is aspx code:

这是aspx代码:

<table>
    <tr>
        <td>
        <asp:Label ID="lblSortBy" runat="server" Text="Sort By:" Visible="false">   
        </asp:Label>
        <asp:DropDownList
                        ID="ddlSortBy" 
                        runat="server" 
                        AutoPostBack="True" 
                        OnSelectedIndexChanged="ddlSortBy_SelectedIndexChanged">
        <asp:ListItem Value="Gross">Gross</asp:ListItem>
        <asp:ListItem Value="Population">Population</asp:ListItem>
        </asp:DropDownList>
        </td>
    </tr>
</table>

Here is simplified code behind when a button is clicked:

这是单击按钮时的简化代码:

public void GetData()
{
    DataView dv = GetReportData().DefaultView;

    if(dv.ToTable().Rows.Count > 0)
     {
        lblSortBy.Visible = true;
     }
     else
     {
        lblSortBy.Visible = false;
     }
  }

I have a couple Update Panels around some ListBoxes and a GridView, but not the Label and Dropdown. Would this cause an issue?

我在一些 ListBoxes 和 GridView 周围有几个更新面板,但没有 Label 和 Dropdown。这会导致问题吗?

I did a test, I set a label that was in an update panel to false if records were found and the label disappeared, so it is working if it is in an update panel.

我做了一个测试,如果找到记录并且标签消失,我将更新面板中的标签设置为 false,所以如果它在更新面板中,它就可以工作。

采纳答案by GregD

If I'm not mistaken, your label should exist on an updatepanel, because as far as the static HTML page is concerned, the one and only time that your current label exists, it's set to be not visible. You would have to reload the whole page to make it visible again.

如果我没记错的话,您的标签应该存在于更新面板上,因为就静态 HTML 页面而言,当您当前的标签存在时,它被设置为不可见。您必须重新加载整个页面才能使其再次可见。

回答by tsilb

  • You just need runat="server" on the label itself; though Visible should default to True.
  • Make sure you add a ForeColor to avoid mixing it in w/ background.
  • Debug to ensure your label has content and it's not in another control whose Visible=False.
  • 您只需要标签本身上的 runat="server" 即可;虽然 Visible 应该默认为 True。
  • 确保添加前景色以避免将其与背景混合。
  • 调试以确保您的标签包含内容并且它不在 Visible=False 的另一个控件中。

回答by Josh Mein

I am assuming that you are gonna hide the ddl as well if there is no data. Have you tried putting a panel around both of them and setting its visibility to true

我假设如果没有数据,您也会隐藏 ddl。您是否尝试过在它们周围放置一个面板并将其可见性设置为 true

if you are returning rows and your button is in an updatepanel, then is your label and ddl in that updatepanel as well

如果您要返回行并且您的按钮在更新面板中,那么您的标签和 ddl 也在该更新面板中

回答by Kelly

If the table is changing visible and is the parent container of the label I don't believe it is necessary to change the label's visibility at all as it should always be set to visible.

如果表正在更改可见并且是标签的父容器,我认为根本没有必要更改标签的可见性,因为它应该始终设置为可见。

回答by M4N

If the button is inside an UpdatePanel, then the Table, Label, etc. also have to be inside an UpdatePanel to get updated. Otherwise only the contents of the UpdatePanel get updated when clicking the button (this is what's called partial page-rendering).

如果按钮位于 UpdatePanel 内,则表、标签等也必须位于 UpdatePanel 内才能更新。否则,只有在单击按钮时 UpdatePanel 的内容才会更新(这就是所谓的部分页面渲染)。

So if the button is in an UpdatePanel, you have two possibilities to solve the problem:

因此,如果按钮在 UpdatePanel 中,您有两种可能来解决问题:

  1. put the table, Label, DropDownList etc. into the same UpdatePanel
  2. or put them in another UpdatePanel and set the UpdateMode of that property to Always, so that it gets updated, even if a Postback was initiated by a control within another UpdatePanel.
  1. 将表、标签、DropDownList 等放入同一个 UpdatePanel
  2. 或者将它们放在另一个 UpdatePanel 中并将该属性的 UpdateMode 设置为 Always,以便它得到更新,即使回发是由另一个 UpdatePanel 中的控件启动的。

See this page in MSDNfor details.

有关详细信息,请参阅MSDN 中的此页面

回答by rekha

thanks its really useful, put Lable in a update panel.

感谢它真的很有用,将 Lable 放在更新面板中。

        <ContentTemplate>
       <table>
           <tr>
                <td>
                     <asp:LinkButton ID="LinkNM" runat="server" Text="Learn>" BackColor="Transparent" style=" color: #6699FF;text-decoration-color:none;border:none;font-size:x-large" OnClick="LinkNM_Click"/>
                    &nbsp;&nbsp;&nbsp;
                                  <asp:Label ID="lblChapterName"  runat="server" BackColor="Transparent" style=" color: #6699FF;text-decoration-color:none;border:none;font-size:x-large" ></asp:Label>

                                </td>
           </tr>
       </table>
             </ContentTemplate>
        <Triggers>
            <asp:PostBackTrigger ControlID="btnFileUpload" />
        </Triggers>

    </asp:UpdatePanel>