Link to home
Start Free TrialLog in
Avatar of Nathan08
Nathan08

asked on

Can't set tag value using c# codebehind on aspx page

Hi, I have a custom .ascx control and would like to set one of it's properties using code.  In the .aspx I have this:

<uc1:CustomContent ID="bunchOfContent" runat="server" contentPayload='<%# getRegionID() %>' />

In the codebehind I have:

    public partial class Region : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
... things
        }

        public string getRegionID()
        {
            //return "region_" + Request["region"];
            return "thevalueIwant";
        }

However, the value I want is not populated and the code is not invoked (breakpoints are not triggered).

What am I doing wrong?  I've tried various changes like changing the quotes from " to ' to no quotes at all.  Also I've used <%= instead of <%# but no luck.  Thanks!
Avatar of kaufmed
kaufmed
Flag of United States of America image

How about this:
// Markup
<uc1:CustomContent ID="bunchOfContent" runat="server" />

// Codebehind
public partial class Region : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        contentPayload = getRegionID();
    }

    public string getRegionID()
    {
        //return "region_" + Request["region"];
        return "thevalueIwant";
    }

Open in new window

try:


<uc1:CustomContent ID="bunchOfContent" runat="server" contentPayload="<%# getRegionID(); %>" />

or 
<uc1:CustomContent ID="bunchOfContent" runat="server" contentPayload="<%= getRegionID(); %>" />

Open in new window

Avatar of Nathan08
Nathan08

ASKER

Thanks for responses:

kaufmed -> 'contentPayload' is not a known construct in Page_Load (nor is uc1).  So this doesn't seem to work, am I missing a step?  I don't see where this assigns the property to the control?

themrrobert -> Unfortunately this doesn't invoke the code still

Any further help appreciated!
Did you drag the user control onto the markup surface or the designer surface (the rendered preview)? If you drag it onto the markup surface, it does not add the appropriate control references for some reason. You can manually add them, but I find it simpler to just delete the control and then drag a new copy of the control onto the designer surface.
Thanks.  Yes I did, the control operates correctly, it's just that I can't get that tag value with code.  Basically, its a simple CMS type thing where the ID of the control is used to determine the bit of content that is rendered.  So 'startpage' or 'aboutpage' gets the appropriate item based on that ID.

However, ID can't be changed on the fly so I'm trying to create an extra attribute that can be.  Proving harder than I expected!
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect!  Many thanks!
NP. Glad to help  :)