views:

212

answers:

2

(New to ASP.NET here.)

I have a user control which should check for a value in the request query string before deciding what to render:

<%# softLoaded ? "HELLO" : "GOOD BYE" %>

This is the code-behind for the user control:

public bool softLoaded { get; set; }

protected void Page_Load(object sender, EventArgs e)
{
    softLoaded = "apply".Equals(Request.QueryString["apply"]);
}

I've debugged the application and found that softLoaded is true when it should be and false when it should be, but no matter what the value is, the generated HTML says "GOOD BYE" (as if the value was false). This makes me believe that Page_Load is called "too late", causing the .ascx file to use the default value of the bool, which is false.

What am I doing wrong? How do I solve this?

+2  A: 

This is a databinding expression. You need to set the variable value any time before the databinding occurs. Page_Load, OnInit or OnPreInit for example.

Locksfree
A: 

I found the problem. I need to call DataBind() in the Page_Load method of my control to actually make stuff happen. I don't fully grasp how it works, but it sure does!

Deniz Dogan
# is tells ASP.NET to evaluate the expression OnDataBind and you were not DataBinding.
rick schott