pita
pita

Reputation: 537

pass variable to the user control

I have created an user control for cascading two dropdownlists "province" and "city". But everytime when I run it, the error message said "An unhandled exception of type 'System.StackOverflowException' occurred" Here is my user control code

public partial class cascadingdropdownlist : System.Web.UI.UserControl
{
    public string province_selectedvalue
    {

        set
        {
            string province_selectedvalue = value;
        }
        get
        {
            return city_selectedvalue;
        }
    }

    public string city_selectedvalue
    {
        set
        {
            string city_selectedvalue = value;
        }
        get
        {
            return city_selectedvalue;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        string a = province_selectedvalue;
        ............
    }

}

In the host page

<uc1:cascadingdropdownlist ID="province_city" runat="server" OnPreLoad="province_city_OnPreLoad"/>

The code behind is

protected void province_city_OnPreLoad(object sender, EventArgs e)
    {
        province_city.province_selectedvalue = myReader["Province/State"].ToString();
        province_city.city_selectedvalue = myReader["City"].ToString();
    }

The error happened in calling user control the province_selectedvalue.get method. why I don't understand why? Anyone can help me, thanks very much

Upvotes: 0

Views: 687

Answers (2)

Bruno Costa
Bruno Costa

Reputation: 2720

You are getting that error because the code is in an infinitive loop. When you call province_selectedvalue.get you are calling city_selectedvalue get. And you have the same name for the property in the variable you think you are calling so it is calling the same over and over again.

Change you code to

public string city_selectedvalue { get; set; }

And

public string province_selectedvalue { get; set; }

Upvotes: 0

n8wrl
n8wrl

Reputation: 19765

The problem is in your property:

public string city_selectedvalue 
{ 
    set 
    { 
        string city_selectedvalue = value; 
    } 

Calls the setter (or the getter!) on city_selectedvalue over and over again resulting in a stack overflow.

Replace your property with this

public string city_selectedvalue { get; set; }

I also thought you could do the same with province_selectedvalue but its getter refers to city_selectedvalue - is that correct?

Upvotes: 2

Related Questions