Search code examples
asp.netasp.net-controls

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 in page back code.

Thanks


Solution

  • 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.