1

(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 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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!

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.