3 Replies - 3437 Views - Last Post: 25 November 2009 - 03:01 PM Rate Topic: -----

#1 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

User control not working when values in QueryString

Posted 25 November 2009 - 09:11 AM

I have a user control (country selector) that consists of 2 DropDownLists and an UpdatePanel. DropDownList1 is populated with country names, and when you select a country it enables and populates DropDownList2 with the states/regions for the selected country (without refreshing the page of course).

In a page that has no QueryString values the control works as expected, but as soon as values are added to the QueryString it stops working. DropDownList1 gets populated with the countries as it should, but selecting a country does nothing, DropDownList2 is never enabled and does not populate with the states/regions for the selected country.

If someone feels it's a code issue I can post the code (but since it works without QS values I'm not sure how it can be a code issue). I've not ran into this issue before and so far have found nothing to help me figure out what's going wrong here. Anyone got any ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: User control not working when values in QueryString

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: User control not working when values in QueryString

Posted 25 November 2009 - 10:16 AM

To prevent someone from asking, here's some code

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



CountrySelector.ascx.cs
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();
		}

	}
}



In my page's Load even I have this

private string connStr = ConfigurationManager.ConnectionStrings["ClusterisConn"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
		CountrySelector1.ConnectionString = ConfigurationManager.ConnectionStrings["ClusterisConn"].ConnectionString;
		CountrySelector1.Provider = Clusteris.Core.DAL.DataProvider.SqlServer;
}



As I stated if the QueryString is empty (http://www.example.com/default.aspx) it works fine, but as soon as there's something in the QueryString (http://www.example.com/default.aspx?ACC=3) all that happens is the country DropDownList populates but nothing happens when you select a country
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: User control not working when values in QueryString

Posted 25 November 2009 - 10:44 AM

@Jayman The user control is being loaded in the markup

<%@ Register src="~/Controls/CountrySelector.ascx" tagname="CountrySelector" tagprefix="ccp" %>

//then
<tr>
	<td colspan="3">						   
		<ccp:CountrySelector ID="CountrySelector1" runat="server" />
	</td>
</tr>



I created a test page with the Master page, added values to the QueryString (manually once the page loaded) and it worked fine. Can this be caused since I'm loading the page with Thickbox (same as Lightbox)?
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: User control not working when values in QueryString

Posted 25 November 2009 - 03:01 PM

Have you tried debugging the javascript to see whats happening when the Ajax fires?

More than likely the interaction between Ajax Extensions and Thickbox are in conflict and Thickbox is winning.

Sometimes they don't play well together. :(
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1