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!
<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!
try:
<uc1:CustomContent ID="bunchOfContent" runat="server" contentPayload="<%# getRegionID(); %>" />
or
<uc1:CustomContent ID="bunchOfContent" runat="server" contentPayload="<%= getRegionID(); %>" />
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!
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.
ASKER
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!
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect! Many thanks!
NP. Glad to help :)
Open in new window