Coding behind Default.aspx page
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="countrypanel" runat="server">
<ContentTemplate >
<asp:DropDownList ID="ddlcountry" AutoPostBack ="true" AppendDataBoundItems
="true" runat="server" Height="20px" Width="156px"
onselectedindexchanged="ddlcountry_SelectedIndexChanged">
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID ="ddlcountry" />
</Triggers>
</asp:UpdatePanel>
<br />
<asp:UpdatePanel ID="statepanel" runat="server">
<ContentTemplate >
<asp:DropDownList ID="ddlstate" AutoPostBack ="true"
AppendDataBoundItems ="true" runat="server" Height="20px"
Width="155px" onselectedindexchanged="ddlstate_SelectedIndexChanged">
</asp:DropDownList>
</ContentTemplate>
<Triggers >
<asp:AsyncPostBackTrigger ControlID ="ddlstate" />
</Triggers>
</asp:UpdatePanel>
<br />
<asp:UpdatePanel ID="citypanel" runat="server">
<ContentTemplate >
<asp:DropDownList ID="ddlcity" AutoPostBack ="true"
AppendDataBoundItems ="true" runat="server" Height="20px" Width="155px">
</asp:DropDownList>
</ContentTemplate>
<Triggers >
<asp:AsyncPostBackTrigger ControlID ="ddlcity" /> </Triggers>
</asp:UpdatePanel>
</div>
</form>
C# Code:
Coding behind Default.aspx.cs page
public partial class _Default : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection("Data Source=YOGENDRA-PC\\SQLEXPRESS;Initial Catalog=register;Integrated Security=True");
public void Bind_ddlCountry()
{
conn.Open();
SqlCommand cmd = new SqlCommand("select CountryID,CountryName from tblCountry",conn );
SqlDataReader dr = cmd.ExecuteReader();
ddlcountry.DataSource = dr;
ddlcountry.Items.Clear();
ddlcountry.Items.Add("--Select--");
ddlcountry.DataTextField = "CountryName";
ddlcountry.DataValueField = "CountryID";
ddlcountry.DataBind();
conn.Close();
}
public void Bind_ddlState()
{
conn.Open();
SqlCommand cmd = new SqlCommand("select StateID,StateName from tblState where Country_ID='"+ddlcountry .SelectedValue +"'",conn );
SqlDataReader dr = cmd.ExecuteReader();
ddlstate.DataSource = dr;
ddlstate.Items.Clear();
ddlstate.Items.Add("--Select--");
ddlstate.DataTextField = "StateName";
ddlstate.DataValueField = "StateID";
ddlstate.DataBind();
conn.Close();
}
public void Bind_ddlCity()
{
conn.Open();
SqlCommand cmd = new SqlCommand("select CityID,CityName from tblCity where State_ID ='"+ddlstate .SelectedValue +"'",conn );
SqlDataReader dr = cmd.ExecuteReader();
ddlcity.DataSource = dr;
ddlcity.Items.Clear();
ddlcity.Items.Add("--Select--");
ddlcity.DataTextField = "CityName";
ddlcity.DataValueField = "CityID";
ddlcity.DataBind();
conn.Close();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind_ddlCountry();
}
}
protected void ddlcountry_SelectedIndexChanged(object sender, EventArgs e)
{
Bind_ddlState();
}
protected void ddlstate_SelectedIndexChanged(object sender, EventArgs e)
{
Bind_ddlCity();
}
}

vishnu vardhanPosted Apr 7, 2014, 2:12 AM
I am working on some project there i have 2 drop down lists and one Textbox in Update panel1. here i am binding the Names of PG from database to dropdownlist1(DDPGNames) and binding the PG rooms to dropdownlist2(DDRoomNumber) based first dropdownlist (it's like Country and state thing) now if i select any Room number in 2nd Dropdownlist the room rent has to dispaly automatically in textbox. for this i am writing this code on DDRoomNumber SelectIndexChanged Event. but my problem is DDRoomNumber is not taking selected room Number (It is taking Only First Value) if i select any other room in dropdown list it's going first one only. see here my code: protected void DDRoomNumber_SelectedIndexChanged(object sender, EventArgs e) { txtRent.Enabled = true; MySqlConnection Con = new MySqlConnection(MyConString); MySqlCommand Cmd = Con.CreateCommand(); string S = "SELECT*FROM pg_room_list WHERE Room_Num ='" DDRoomNumber.SelectedItem.Text "' AND Room_PG_ID ='" DDPGNames.SelectedValue "';"; Cmd = new MySqlCommand(S, Con); MySqlDataReader Dr; Con.Open(); Dr = Cmd.ExecuteReader(); while (Dr.Read()) { string SRoom_Rent = Dr.GetString("Room_Rent"); txtRent.Text = SRoom_Rent; } Con.Close(); } Pls give me some solution. Thanks
Yogendra KumarPosted Apr 4, 2013, 1:00 PM
Thanks Tanuj
Tanuj KhuranaPosted Nov 11, 2012, 12:05 PM
thanks....this post is good...
joPosted Jul 20, 2011, 3:21 AM
please can you help me regarding the sql database and tables you have created for this article? Thanks in advance
Prateek NarangPosted May 12, 2011, 2:57 AM
Hi Guys This is working Fine but i think in this senario there is no need to add Triggers with update panel as below: <Triggers > <asp:AsyncPostBackTrigger ControlID ="ddlcity" /> </Triggers> It doesnt craete any problem but it is not needed here. Try to use without trigger and please reply .