Can't set tag/attribute value using c# codebehind on aspx page
I have a custom .ascx control开发者_如何学Python 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!
In the Page_Load
method, you can do:
bunchOfContent.contentPayload = getRegionID();
The reason why <%# ... %>
did not work is because that's the form you use for databinding. In order for the code you put in there to be executed, you need to call the DataBind()
method somewhere. And as for <%= ... %>
, that's not suitable for setting a server control property, it simply is a short form of <% Response.Write(...) %>
.
精彩评论