theDawckta
theDawckta

Reputation: 874

access a ascx parameter in code behind

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;
        }
    }

Upvotes: 3

Views: 3302

Answers (3)

Cyril
Cyril

Reputation: 144

This code is enough

in .aspx page

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

in .ascx.cs of the user control

public String DefaultCountry { get; set; }

The property will be initialized with the value "USA" automatically.

Upvotes: 0

Canavar
Canavar

Reputation: 48088

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.

Upvotes: 1

Brandon
Brandon

Reputation: 70012

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

myMap1.DefaultCountry = "UnitedStates";

Upvotes: 1

Related Questions