Asp:当visible设置为true时,不显示标签?

时间:2021-03-16 22:50:53

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表单,它有几个列表框和一个搜索按钮。单击该按钮时,它将返回一个DataSet。如果数据集包含记录,我将最初设置为false的asp:标签设置为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周围有一些更新面板,但没有标签和下拉列表。这会导致问题吗?

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,因此如果它在更新面板中,它就可以正常工作。

6 个解决方案

#1


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页面而言,当前标签存在的唯一时间,它被设置为不可见。您必须重新加载整个页面才能再次显示它。

#2


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. 将表,Label,DropDownList等放入同一个UpdatePanel中

  3. 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.
  4. 或者将它们放在另一个UpdatePanel中,并将该属性的UpdateMode设置为Always,以便更新,即使其他UpdatePanel中的控件启动了回发。

See this page in MSDN for details.

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

#3


  • You just need runat="server" on the label itself; though Visible should default to True.
  • 你只需要标签上的runat =“server”;虽然Visible应该默认为True。

  • Make sure you add a ForeColor to avoid mixing it in w/ background.
  • 确保添加ForeColor以避免在背景中混合。

  • Debug to ensure your label has content and it's not in another control whose Visible=False.
  • 调试以确保您的标签具有内容,并且它不在Visible = False的另一个控件中。

#4


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.

如果表格可见并且是标签的父容器,我认为根本不需要更改标签的可见性,因为它应始终设置为可见。

#5


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也是如此

#6


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>

#1


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页面而言,当前标签存在的唯一时间,它被设置为不可见。您必须重新加载整个页面才能再次显示它。

#2


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. 将表,Label,DropDownList等放入同一个UpdatePanel中

  3. 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.
  4. 或者将它们放在另一个UpdatePanel中,并将该属性的UpdateMode设置为Always,以便更新,即使其他UpdatePanel中的控件启动了回发。

See this page in MSDN for details.

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

#3


  • You just need runat="server" on the label itself; though Visible should default to True.
  • 你只需要标签上的runat =“server”;虽然Visible应该默认为True。

  • Make sure you add a ForeColor to avoid mixing it in w/ background.
  • 确保添加ForeColor以避免在背景中混合。

  • Debug to ensure your label has content and it's not in another control whose Visible=False.
  • 调试以确保您的标签具有内容,并且它不在Visible = False的另一个控件中。

#4


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.

如果表格可见并且是标签的父容器,我认为根本不需要更改标签的可见性,因为它应始终设置为可见。

#5


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也是如此

#6


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>