RealityDysfunction
RealityDysfunction

Reputation: 2639

Element.Visible returning false, but it is visible on page

I have the following if-statement, that occurs on Page.IsPostBack, why is FirstName.Visible, LastName.Visible, EmailAddress.Visible all false?:

protected void Page_Load(object sender, EventArgs e)
{
    MainLogo.InnerHtml = ModuleEval("HeadlineLandingPage").ToString();
    FillLabelsAndImages();

    if (!Page.IsPostBack) //Page load
    {
        //Some random code here
    }
    else //Submit button load
    {
        MainLogo.InnerHtml = ModuleEval("HeadlineLandingMasterPage").ToString();
            if (FirstName.Visible && LastName.Visible && EmailAddress.Visible) 
            {
                if (detailsResponse.ResponseStatus.Success && detailsResponse.MemberInfo != null)
                {
                     //Random code here
                }
                else
                    HandleWebServiceError(detailsResponse.ResponseStatus);
            }
            else 
            {
            }
    }
}

The elements are invisible by default:

<input type="text" id="FirstName" runat="server" visible="False" />

However they are set to visible when some user actions occur:

    FirstName.Visible = true;

The elements are all clearly visible when I submit, I don't understand why asp.net still thinks they are invisible.

Upvotes: 0

Views: 2430

Answers (3)

Tim
Tim

Reputation: 181

How are you changing the control?

If you've got something to the effect of

protected void Page_Load(object sender, EventArgs e)
{
    if(FirstName.Visible && LastName.Visible && EmailAddress.Visible) 
    {
        //Code
    }
}

protected void UpdateFirstName(object sender, EventArgs e)
{
    FirstName.Visible = true;
}

And you call the update by using an ASP.NET event, such as

<asp:button text="Button!" onclick="UpdateFirstName" runat="server" />

When you click on the button Page_Load is called first. While Page_Load is running,

FirstName.Visible

Would be whatever the default value is - in this case, false.

Unfortunately there isn't really a way to call UpdateFirstName before Page_Load.

The best solution is to move your

if(FirstName.Visible && LastName.Visible && EmailAddress.Visible) 

logic into a different function which can be called after UpdateFirstName runs.

Upvotes: 2

TechneWare
TechneWare

Reputation: 263

Elements rendered via asp.net controls will not render in the page at all if they are set to visible=false. IE: The element will not be in the output.

Regular html elements do not understand this and therefore you must set style="display:none" to hide them while still making them available in the DOM/rendered output.

So if what you are using is not an asp.net control setting the value visible="false" on the element, as you show for your input element:

<input type="text" id="FirstName" runat="server" visible="False" />

Really does nothing except placing an attribute on the element called visible which has a value of False.

For example:

If I put the following in an asp.net page.

<input id="test" runat="server" />
<asp:TextBox ID="test1" runat="server"></asp:TextBox>

And then on the server side I run the following:

protected void Page_Load(object sender, EventArgs e)
{
    test.Style.Add("display", "none");
    test1.Visible = false;
}

The rendered output will be something similar to this:

<input name="ctl00$FeaturedContent$test" type="text" id="FeaturedContent_test" style="display:none;" />

With no sign of the element test1 in the rendered output. While the element test is in the output except that it does not appear as a visible element on the page.

Hope that helps.

Upvotes: 1

Trucktech
Trucktech

Reputation: 358

Have you tried?

if (FirstName.Visible == true && LastName.Visible == true && EmailAddress.Visible ==  true)
{
    //your code here
}

Your testing against a boolean and it really shouldn't matter but it's worth a try. According to MSDN the .Visible property "Gets or sets a value indicating whether the control and all its child controls are displayed.". So your getting the value but you still need to test against that value.

Upvotes: 0

Related Questions