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

2020-07-18 07:07发布

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.

标签: c# asp.net
6条回答
老娘就宠你
2楼-- · 2020-07-18 07:39

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

查看更多
叼着烟拽天下
3楼-- · 2020-07-18 07:40

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.

查看更多
看我几分像从前
4楼-- · 2020-07-18 07:44

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楼-- · 2020-07-18 07:47

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.

查看更多
Viruses.
6楼-- · 2020-07-18 07:57
  • 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.
查看更多
虎瘦雄心在
7楼-- · 2020-07-18 07:58

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>
查看更多
登录 后发表回答