Solved

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

Posted on 2010-11-29
8
637 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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
 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp day pilot 3 18
Reference issues in Visual Studio 3 29
Where did System.Data.Objects go? 2 23
Error handling in asp.net site 5 24
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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

808 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