William Calleja
William Calleja

Reputation: 4175

Haw can i use a value from a session variable within the attribute of a control?

I have an ASP control custom built and I need to pass a value taken from a session variable to it like so:

<custom:control id='mycontrol' value="+Session['myControlValue']+">
   ...
</custom:control>

the above code obviously doesn't work, I need a way to insert the Session value in the control in this way somehow, can anyone help?

Upvotes: 0

Views: 1067

Answers (2)

user10140942
user10140942

Reputation: 1

Switch the quotes, like so:

<custom:control id="mycontrol" 
            runat="server" 
            value='<%# Session["myControlValue"] %>' />

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039130

If it is a data bound control you may try this:

<custom:control id="mycontrol" 
                runat="server" 
                value='<%# Session["myControlValue"] %>'>
</custom:control>

Personally I would prefer setting this value from the code behind. It seems a little strange to me that a view (aspx) page manipulates the session:

protected void Page_Load(object sender, EventArgs e) 
{
    mycontrol.Value = Session["myControlValue"];
}

Upvotes: 1

Related Questions