访问后面代码中的ascx参数

时间:2022-09-02 12:18:37

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.

我想知道是否可以将一个用户控件添加到带有参数的页面中,然后在代码behiind中访问该参数以进行初始化。

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

例如,在我的aspx页面上,我会有一些类似的东西。

<%@ 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.

如何在我的Map.ascx中访问DefaultCountry参数。cs背后的代码文件。

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

如果我偏离了这个基础,正确的实现是什么?

EDIT:

编辑:

Figured it out

搞懂了

in .aspx page

在。aspx页面

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

in .ascx.cs of the user control

在.ascx。用户控件的cs

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 个解决方案

#1


1  

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

首先调用usercontrol,然后调用user控件上的public属性。

myMap1.DefaultCountry = "UnitedStates";

#2


1  

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.

在这种情况下,默认国家应该是您的用户控件的属性。因此,您可以通过使用用户控件实例的这个属性来访问它。

#3


0  

This code is enough

这段代码就足够了

in .aspx page

在。aspx页面

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

in .ascx.cs of the user control

在.ascx。用户控件的cs

public String DefaultCountry { get; set; }

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

属性将自动以值“USA”初始化。

#1


1  

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

首先调用usercontrol,然后调用user控件上的public属性。

myMap1.DefaultCountry = "UnitedStates";

#2


1  

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.

在这种情况下,默认国家应该是您的用户控件的属性。因此,您可以通过使用用户控件实例的这个属性来访问它。

#3


0  

This code is enough

这段代码就足够了

in .aspx page

在。aspx页面

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

in .ascx.cs of the user control

在.ascx。用户控件的cs

public String DefaultCountry { get; set; }

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

属性将自动以值“USA”初始化。