Employers - Post Free Jobs
Forum guidelines
AuthorQuestion
how to deal with SelectedIndexChange of dropdownlist in Usercontrol Added dynamicaly
Posted on: 31 Jan 2011

hello everyone,
I have three DropDown list of Country,State,City in a User Control. I have populated the country DropDownList on page load of .ascx page ie. on UserControl. Now i want to populate the state ddl on selectedindexchange event of Countryddl bt finding difficulty to add that event on parent page.
plz can anyone help me to sortout this problem.

Anusha...

AuthorReply
Re: how to deal with SelectedIndexChange of dropdownlist in Usercontrol Added dynamicaly
Posted on: 31 Jan 2011  
use switch-case in the country drop down list's SelectedIndexChanged event n maps its SelectedIndex property. On Case 0 write the code to add the items in the state ddl int indx = (int)country.SelectedIndex; switch(indx) { case 0://let the countrty be India state.Items.Add("Maharashtra"); // and so on.... } similarly do for the city ddl
Re: how to deal with SelectedIndexChange of dropdownlist in Usercontrol Added dynamicaly
Posted on: 01 Feb 2011  

I have done this coding on the User Control.
using
System;
using
System.Collections;
using
System.Configuration;
using
System.Data;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
public
partial class userControll_AddressUC : System.Web.UI.UserControl
{
/// <summary>
/// Method for Page_Load
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
///
#region
"Page_Load"
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
CountoryMaster oCountoryMaster = new CountoryMaster();
CountryDB oCountryDB = new CountryDB();
DataTable dtBindListCountry = oCountryDB.GetCountryName(oCountoryMaster);
if (dtBindListCountry.Rows.Count > 0)
{
ddlAddCountry.DataSource = dtBindListCountry;
ddlAddCountry.DataTextField = dtBindListCountry.Columns[
"country_name"].ToString();
ddlAddCountry.DataValueField = dtBindListCountry.Columns[
"country_id"].ToString();
ddlAddCountry.DataBind();
}
dtBindListCountry.Dispose();
}
}
catch (Exception ex)
{
throw ex;
}
}

#endregion

/// <summary>
/// Method for ddlAddState_SelectedIndexChanged
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
///
#region
"BindCity"
protected void BindCity(object sender, EventArgs e)
{
try
{
CityMaster oCityMaster = new CityMaster();
CityDB oCityDB = new CityDB();
oCityMaster.StateId = ddlAddState.SelectedValue;
oCityMaster.CountryId =
Convert.ToInt32(ddlAddCountry.SelectedValue);
DataTable dtCity = oCityDB.getSelectCityByStateId(oCityMaster);
if (dtCity.Rows.Count > 0)
{
ddlAddCity.DataSource = dtCity;
ddlAddCity.DataTextField = dtCity.Columns[
"city_name"].ToString();
ddlAddCity.DataValueField = dtCity.Columns[
"city_id"].ToString();
ddlAddCity.DataBind();
}
ddlAddCity.Items.Insert(0,
new ListItem("Please Select...", "-1", true));
dtCity.Dispose();
}
catch (Exception ex)
{
throw ex;
}
}

#endregion

/// <summary>
/// Method for ddlAddCountry_SelectedIndexChanged
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
///
#region
"BindState"
protected void BindState(object sender, EventArgs e)
{
try
{
State oState = new State();
StateDb oStateDb = new StateDb();
oState.CountryId =
Convert.ToInt32(ddlAddCountry.SelectedValue);
DataTable dtState = oStateDb.getStateByCountry(oState);
if (dtState.Rows.Count > 0)
{
ddlAddState.DataSource = dtState;
ddlAddState.DataTextField = dtState.Columns[
"state_name"].ToString();
ddlAddState.DataValueField = dtState.Columns[
"state_id"].ToString();
ddlAddState.DataBind();
}
ddlAddState.Items.Insert(0,
new ListItem("Please Select...", "-1", true));
dtState.Dispose();

}
catch (Exception ex)
{
throw ex;
}
}

#endregion
 
 
<
asp:UpdatePanel ID="UpnlAddDetails" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<table border="0" cellpadding="1" cellspacing="3" width="100%">
<tr>
<td align="left" width="35%">
Country
</td>
<td align="left" width="65%">
<asp:DropDownList ID="ddlAddCountry" runat="server" AutoPostBack="true" CssClass="fields"
OnSelectedIndexChanged="BindState">
</asp:DropDownList>
</td>
</tr>
<tr>
<td align="left" width="35%">
State
</td>
<td align="left" width="65%">
<asp:DropDownList ID="ddlAddState" runat="server" CssClass="fields" AutoPostBack="true"
OnSelectedIndexChanged="BindCity">
</asp:DropDownList>
</td>
</tr>
<tr>
<td align="left" width="35%">
</td>
<td align="left" valign="middle" width="65%">
Other :
<asp:TextBox ID="txtOtherState" runat="server" CssClass="INPUT-BOX"></asp:TextBox>
</td>
</tr>
<tr>
<td align="left" width="35%">
City
</td>
<td align="left" width="65%">
<asp:DropDownList ID="ddlAddCity" runat="server" CssClass="fields">
</asp:DropDownList>
</td>
</tr>
<tr>
<td align="left" width="35%">
</td>
<td align="left" width="65%">
Other :
<asp:TextBox ID="txtOtherCity" runat="server" CssClass="INPUT-BOX"></asp:TextBox>
</td>
</tr>
</table>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlAddCountry" EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlID="ddlAddState" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
And the Followingn on the parent page
<
asp:DropDownList ID="ddlAddressType" runat="server" AutoPostBack="true" CssClass="fields"
OnSelectedIndexChanged="ddlAddressType_SelectedIndexChanged">
<asp:ListItem Text="...Select..." Value="" Selected="True"></asp:ListItem>
<asp:ListItem Text="Home" Value="Home"></asp:ListItem>
<asp:ListItem Text="Business" Value="Business"></asp:ListItem>
</asp:DropDownList>
if (ddlAddressType.SelectedValue == "Home")
{
Control AddressHome = LoadControl("~/userControll/AddressUC.ascx");
pnlUCPermanent.Visible =
true;
pnlUCPermanent.Controls.Add(AddressHome);
pnlUCBussisns.Visible =
false;
}
if (ddlAddressType.SelectedValue == "Home")
{
Control AddressBuss = LoadControl("~/userControll/AddressUC.ascx");
pnlUCBussisns.Visible =
true;
pnlUCBussisns.Controls.Add(AddressBuss);
pnlUCPermanent.Visible =
true;
}
But the selected index change for the country ddl is not fired.

Anusha...

Re: how to deal with SelectedIndexChange of dropdownlist in Usercontrol Added dynamicaly
Posted on: 01 Feb 2011  
the asp code for the country ddl. 

<asp:DropDownList ID="country" runat="server" AutoPostBack="True" Height="29px" 
            onselectedindexchanged="country_SelectedIndexChanged1" 
            ontextchanged="country_TextChanged" Width="176px">
        </asp:DropDownList>

One of the property of ddl is "AutoPostBack", which is to be set true...
this allows to load the next ddl on its selection. 

Now the SelectedIndexChanged event of the country ddl 

 protected void country_SelectedIndexChanged1(object sender, EventArgs e)
        {
            int ch = (int)country.SelectedIndex;
            switch (ch)
            {
                case 0:
                    state.Items.Clear();
                    state.Items.Add("Delhi");
                    state.Items.Add("Punjab");
                    state.Items.Add("U.P.");
                    state.Items.Add("Maharashtra");
                    break;

                case 1:
                    state.Items.Clear();
                    state.Items.Add("Islamabad");
                    state.Items.Add("Punjab");
                    state.Items.Add("Karachi");
                    state.Items.Add("Lahor");
                    break;

                default:
                    Label1.Text = "Select a country first";
                    break;

            }
        }


The switch-case will check the index of the country ddl n will load the states. If u r loading the ddls from a database, then u have to fire a query as,
 "select state from country where country '" + country.Text + "'"; 
and then u will get a data set taht u can bound to state ddl...

SPONSORED BY

Offshore Software and Database Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.