Solved

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

Posted on 2010-11-29
8
639 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Generate Unique ID in VB.NET 21 104
Dictionary and array of [N] size - performance tuned 12 41
Converting PHP triple des decrypt to c# 9 70
Selenium and Xpath 4 31
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…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 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