how to assign a control property value from a global variable in page code?
Greetings,
I have a control and list of variables and I want in the control property to be assigned to the variable value directly in the page not from the back code, something like this
My global variables
public string Banana = "banana_pie";
public string Apple = "apple_pie";
in my custom control instead of:
<uc:LoadPie id="pieBanana" type="banana_pie" />
To this
<uc:LoadPie id="pieBanana" type="<%=Banana %>" />
so is there a way or just assign the property开发者_StackOverflow中文版 in page back code.
Thanks
You can do it like this using data binding syntax.
<uc:LoadPie id="pieBanana" type='<%#Banana%>' runat="server"></uc:LoadPie>
But then in your code behind you have to call
pieBanana.DataBind();
in the page load in order for the databinding expression to be evaulated.
But if you are going to do this then you might as well assign the property in the page load.
I think you should go with a property (protected should be enought, but I'll say public in the following snippet) in your code behind:
Public Property myBanana() As String
Get
Return Pies.Banana;
End Get
End Property
Then you can use it in your controls, for example:
<uc:LoadPie id="pieBanana" type="<%= myBanana%>" />
Not quite what you want, but how about:
<% pieBanana["type"] = this.Banana %>
精彩评论