views:

104

answers:

1

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();
        }

    }




}
A: 

o.k.w and the others who have looked at this question, here's the Makup for the user control in question

<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Always" runat="server">
<Triggers>
    <asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<p style="padding-bottom:6px;">Country:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:DropDownList ID="DropDownList1" runat="server" Width="146px" AutoPostBack="true">
            </asp:DropDownList></p>
<p style="padding-bottom:6px;">State/Region:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:DropDownList ID="DropDownList2" runat="server" Width="146px" AutoPostBack="false" Enabled="False">
            </asp:DropDownList></p>
</ContentTemplate>

And here is where I'm setting the properties needed to populate it in the page it's being used in

protected void Page_Load(object sender, EventArgs e)

{ CountrySelector1.ConnectionString = ConfigurationManager.ConnectionStrings["ClusterisConn"].ConnectionString; CountrySelector1.Provider = Clusteris.Core.DAL.DataProvider.SqlServer; }

When the page loads if there's nothing in the QueryString (Just the address and page name) the control does what it's supposed to do, however I need to pass values in the QueryString and when that happens only the first DropDownList loads (Countries), when I select a country the 2nd one never populates or emables.

PsychoCoder