tags:

views:

34

answers:

2

I was wondering if I could add a user control to a page with a parameter and then access that parameter in the code behiind for initiallization.

For example on my aspx page i would have somethign like.

<%@ Register TagPrefix="uc1" TagName="myMap" Src="~/Map.ascx" %>
blah 
blah 
blah
<uc1:myMap ID="myMap1" runat="server" DefaultCountry="UnitedStates"/>

How would I access the DefaultCountry parameter in my Map.ascx.cs code behind file.

If I am off base on this what is the correct implementation?

EDIT:

Figured it out

in .aspx page

<uc1:myPartnerMap ID="MyPartnerMap1" runat="server" defaultCountry="USA"/>

in .ascx.cs of the user control

private string defaultCountry;   

    public String DefaultCountry
    {
        get { return defaultCountry; }
        set { defaultCountry = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CountrySelector.SelectedValue = defaultCountry;
        }
    }
+1  A: 

You would call the usercontrol first, and then the public property on the user control.

myMap1.DefaultCountry = "UnitedStates";
Brandon
+1  A: 

In this case DefaultCountry sould be a property of your user control. So you can simply access it by using this property of the user control's instance.

Canavar
What if I had to use this to set a default on a dropdown such as this, this is different i take it.<asp:DropDownList ID="CountrySelector" runat="server" onselectedindexchanged="CountrySelector_SelectedIndexChanged" AutoPostBack="True" />
theDawckta