Solved

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

Posted on 2010-11-29
8
636 Views
Last Modified: 2012-05-10
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!
0
Comment
Question by:Nathan08
  • 4
  • 3
8 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34235737
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

0
 
LVL 13

Expert Comment

by:themrrobert
ID: 34235744
try:


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

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

Open in new window

0
 

Author Comment

by:Nathan08
ID: 34235816
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!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34235838
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Nathan08
ID: 34235896
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!
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 34235920
You're right....  I must be tired. This is what the previous should have been:
// Markup
<uc1:CustomContent ID="bunchOfContent" runat="server" />

// Codebehind
public partial class Region : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Use the control's ID to access the object
        bunchOfContent.contentPayload = getRegionID();
    }

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

Open in new window

0
 

Author Closing Comment

by:Nathan08
ID: 34235946
Perfect!  Many thanks!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34235995
NP. Glad to help  :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now