tags:

views:

1077

answers:

5

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.

A: 
  • 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.
tsilb
It already has content and the forecolor is black with a white background.
Xaisoft
A: 

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

jmein
but I am setting it the visibility to true if records are found.
Xaisoft
have you debugged it to make sure it is actually setting the value?
jmein
yes, it is setting it to true.
Xaisoft
k I was just wondering why is your dataview getting its data from the GetData function which is the name of your function above. Is this a mistake or just shorthand for example
jmein
It is just a shorthand example. Basically it returns a DataSet.
Xaisoft
also have you tried setting its visibility to true and just setting it to false if there are no rows
jmein
Just tried a panel. It did not work. Could one reason be that I have some UpdatePanels around some other controls. Would that cause issues?
Xaisoft
it could but it is hard to tell without knowing more
jmein
I have done the pretty much the same thing before though and I had an update panel and had no issues
jmein
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.
Xaisoft
that it is why it does need to be in an update panel
jmein
I would still recommend using the panel though instead if doing them both separately
jmein
I know. That makes sense.
Xaisoft
so if you put it in an update panel does it work for you?
jmein
Yes, I just tried and it works when I put the panel in an update panel.
Xaisoft
A: 

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.

Kelly
I know, I was just experimenting to see whether it was an asp.net control or any control. That is why you see the label set to false, but in reality if I have the table visible, I would just have the label's visibility default.
Xaisoft
+2  A: 

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.

GregD
Can you explain. Thanks.
Xaisoft
Your label, outside of an updatepanel, is not asynchronous. In other words you can't update a label that's already displayed on an end users computer, without refreshing the whole page. If the label is on an updatepanel, you can.
GregD
Ok, it seems you are correct. Thanks.
Xaisoft
+1  A: 

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.

M4N