Saturday, March 24, 2012

Pass variables to header control (.asxc web user control)?

How can I pass variables to a header control?

Generically, I have a series of pages that all use the same header and footer controls. I want each page to have it's own values for a few variables, and the header page needs to recognize those values and change some html based on those values. The html to be changed is just class names on tds, I'm assuming for it to work that maybe I need to change that to something .net can access easier.

Specifically, I tried just making the variables in the pages's .aspx.vb files and writing them out in the header's .aspx file (right where they'd go in the control) but it's errors and tells me I need to declare the variables in the header control.Try this (in C#)

In your code behind for the Header control:


private string _YourVariable = "";

public string YourVariable
{
set
{
_YourVariable = value.ToLower();
}
}

and in the HTML of the page containing the control:


<uc1:YourHeader id="YourHeader1" runat="server" YourVariable="TheValueOfYourVariableHere"></uc1:YourHeader >

Hope this helps,
Nic
Thank you! I will work on it. I'm using vb, so I'll let you know how it goes.

Question: does the header control line allow multiple variables or just one?

Ie,


<uc1:YourHeader id="YourHeader1" runat="server" YourVariable="TheValueOfYourVariableHere" YourVar1="SomeValueY" YourVar2="SomeValueX" YourVar3="SomeValueZ" ></uc1:YourHeader

As many as you want.
Good to hear I can have several variables there, thanks.

Ok, I've had no luck creating equivalent code in vb, yet it looks so straight forward... make a local variable, set it to "", then give the local variable the value of the 1st one... does that sound about right?

:?
Update: Success!
Used this link (http://www.kamalpatel.net/ConvertCSharp2VB.aspx), followed your suggestions, and it works! Now I understand better how to approach this in the future.

Thanks for your help. :)

0 comments:

Post a Comment