Solved

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

Posted on 2010-11-29
8
633 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Nathan08
Comment Utility
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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
Perfect!  Many thanks!
0
 
LVL 74

Expert Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

18 Experts available now in Live!

Get 1:1 Help Now