I have a user control (a country selector, select a country and it populates a 2nd DropDownList with the states/regions for that country without reloading the page). The control works fine if there's nothing in the QueryString, but as soon as I add something to the QueryString, such as http://www.example.com/test.aspx?ACC=3 then the control no longer works.
This seems strange to me (and I've not encountered anything like this before). Anyone ran into this before, and if so how did you resolve it. By the way, the page in question is a Web Content Form (Master Page content page) and am using C# 3.5 with SQL 2008.
As requested here is the code (it was written by a co-worker so I'm not resposible for it but am responsible for making it work)
public partial class CountrySelector : System.Web.UI.UserControl
{
private string _connectionString;
private string _selectedCountry;
private string _selectedRegion;
private string _country;
private string _stateOrRegion;
private DataProvider _provider;
public DataProvider Provider
{
get { return _provider; }
set { _provider = value; }
}
public string ConnectionString
{
get { return _connectionString; }
set { _connectionString = value; }
}
public string SelectedCountry
{
get { return _selectedCountry; }
}
public string SelectedRegion
{
get { return _selectedRegion; }
}
public string Country
{
get { return _country; }
set { _country = value; }
}
public string StateOrRegion
{
get { return _stateOrRegion; }
set { _stateOrRegion = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.SelectedIndexChanged += new EventHandler(DropDownList1_SelectedIndexChanged);
DropDownList2.SelectedIndexChanged += new EventHandler(DropDownList2_SelectedIndexChanged);
if (DropDownList1.Items.Count == 0)
{
string connectionString = _connectionString;
SqlDataReader sqlReader;
IClusterisDB mainDatabase = new ClusterisDB(this._provider, this._connectionString);
try
{
mainDatabase.OpenDBConnection();
sqlReader = (SqlDataReader)mainDatabase.ExecuteDataReader(System.Data.CommandType.StoredProcedure, "dbo.uspUserGetCountryList");
DropDownList1.Items.Clear();
while (sqlReader.Read())
{
DropDownList1.Items.Add(new ListItem(sqlReader.GetString(1), sqlReader.GetString(0)));
}
DropDownList1.Items.Insert(0, "[ Select ]");
sqlReader.Close();
if (!string.IsNullOrEmpty(_country))
DropDownList1.SelectedValue = _country;
if (!string.IsNullOrEmpty(_stateOrRegion))
DropDownList2.SelectedValue = _stateOrRegion;
}
catch
{
// Add exception handling here
}
finally
{
mainDatabase.CloseDBConnection();
mainDatabase.Dispose();
}
}
_selectedCountry = DropDownList1.SelectedItem.Value;
}
void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
_selectedRegion = DropDownList2.Text;
}
void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList2.Items.Clear();
DropDownList2.Enabled = false;
SqlDataReader sqlReader;
IClusterisDB mainDatabase = new ClusterisDB(this._provider, this._connectionString);
try
{
mainDatabase.OpenDBConnection();
mainDatabase.CreateDataParameters(1);
mainDatabase.AddDataParameters(0, "@CountryCode", DropDownList1.SelectedItem.Value);
sqlReader = (SqlDataReader)mainDatabase.ExecuteDataReader(System.Data.CommandType.StoredProcedure, "dbo.uspUserGetStateOrRegionByCode");
if (sqlReader.HasRows)
{
while (sqlReader.Read())
{
DropDownList2.Enabled = true;
DropDownList2.Items.Add(new ListItem(sqlReader.GetString(1), sqlReader.GetString(0)));
}
DropDownList2.Items.Insert(0, "[ Select ]");
}
else
{
DropDownList2.Items.Clear();
DropDownList2.Enabled = false;
}
sqlReader.Close();
if (DropDownList2.Text != "[ Select ]")
_selectedRegion = DropDownList2.SelectedItem.Value;
_selectedCountry = DropDownList1.SelectedItem.Value; ;
}
catch(Exception ex)
{
Response.Write(ex.Message);
//Add exception handling
}
finally
{
mainDatabase.CloseDBConnection();
mainDatabase.Dispose();
}
}
}