简体   繁体   中英

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

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.

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.

Here is aspx code:

<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?

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.

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.

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

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

  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.

See this page in MSDN for details.

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

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM