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
The problem is in your property:
Calls the setter (or the getter!) on city_selectedvalue over and over again resulting in a stack overflow.
Replace your property with this
I also thought you could do the same with province_selectedvalue but its getter refers to city_selectedvalue – is that correct?