Access ascx parameter in code

I was wondering if I can add a user control to a page with a parameter, and then access this parameter in the code for initialization.

For example, on my aspx page, I would have something similar.

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

How to access the DefaultCountry parameter in my Map.ascx.cs file behind the file.

If I do not agree with this, then what is the correct implementation?

EDIT:

It revealed

on the .aspx page

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

in .ascx.cs 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;
        }
    }
+3
source share
3 answers

First, you call usercontrol, and then the public property in the user control.

myMap1.DefaultCountry = "UnitedStates";
+1
source

DefaultCountry . , .

+1

This code is enough

on the .aspx page

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

in .ascx.cs user control

public String DefaultCountry { get; set; }

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

0
source

Source: https://habr.com/ru/post/1771806/


All Articles