TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Virus Kiraak
NA
14
28.2k
I have two dropdowns, when I select one dropdown value. I need to bind the other dropdown without postback. Help me with the code.
Sep 14 2012 6:39 AM
I have two dropdowns, when I select one dropdown value. I need to bind the other dropdown without postback. Help me with the code.
Regards,
MZee.
Reply
Answers (
2
)
0
Satyapriya Nayak
0
39.3k
13.3m
Sep 14 2012 11:57 AM
Hi Virus,
Try this...
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Country_state_city._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="countrypanel" runat="server">
<ContentTemplate >
<asp:Label ID="Label1" runat="server" Text="Choose Country" BackColor="#FFFF99"
ForeColor="Red" Width="100px"></asp:Label>
<asp:DropDownList ID="ddlcountry" AutoPostBack ="true"
AppendDataBoundItems="true" runat="server" Height="20px" Width="156px"
onselectedindexchanged="ddlcountry_SelectedIndexChanged" Font-Bold="True"
ForeColor="#FF9900">
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID ="ddlcountry" />
</Triggers>
</asp:UpdatePanel>
<br />
<asp:UpdatePanel ID="statepanel" runat="server">
<ContentTemplate >
<asp:Label ID="Label2" runat="server" Text="Choose State" BackColor="#FFFF66"
ForeColor="Red" Width="100px"></asp:Label>
<asp:DropDownList ID="ddlstate" AutoPostBack ="true"
AppendDataBoundItems ="true" runat="server" Height="20px"
Width="155px" onselectedindexchanged="ddlstate_SelectedIndexChanged" Font-Bold="True"
ForeColor="#993300">
</asp:DropDownList>
</ContentTemplate>
<Triggers >
<asp:AsyncPostBackTrigger ControlID ="ddlstate" />
</Triggers>
</asp:UpdatePanel>
<br />
<asp:UpdatePanel ID="citypanel" runat="server">
<ContentTemplate >
<asp:Label ID="Label3" runat="server" Text="Choose City" BackColor="#FFFF99"
ForeColor="Red" Width="100px"></asp:Label>
<asp:DropDownList ID="ddlcity" AutoPostBack ="true" AppendDataBoundItems ="true"
runat="server" Height="20px" Width="155px" Font-Bold="True" ForeColor="#CCCC00">
</asp:DropDownList>
</ContentTemplate>
<Triggers >
<asp:AsyncPostBackTrigger ControlID ="ddlcity" /></Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
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;
using System.Data.SqlClient;
namespace Country_state_city
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindcountry();
}
}
protected void ddlcountry_SelectedIndexChanged(object sender, EventArgs e)
{
bindstate();
}
protected void ddlstate_SelectedIndexChanged(object sender, EventArgs e)
{
bindcity();
}
public void bindcountry()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select CountryId,CountryName from Country";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "Country");
ddlcountry.Items.Clear();
ddlcountry.Items.Add("--Select--");
ddlcountry.DataValueField = "CountryId";
ddlcountry.DataTextField = "CountryName";
ddlcountry.DataSource = ds;
ddlcountry.DataMember = "Country";
ddlcountry.DataBind();
con.Close();
}
public void bindstate()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select StateId,StateName from State where Country_Id='" + ddlcountry.SelectedValue + "'";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "State");
ddlstate.Items.Clear();
ddlstate.Items.Add("--Select--");
ddlstate.DataValueField = "StateId";
ddlstate.DataTextField = "StateName";
ddlstate.DataSource = ds;
ddlstate.DataMember = "State";
ddlstate.DataBind();
con.Close();
}
public void bindcity()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select CityId,CityName from City where State_Id ='" + ddlstate.SelectedValue + "'";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "City");
ddlcity.Items.Clear();
ddlcity.Items.Add("--Select--");
ddlcity.DataValueField = "CityId";
ddlcity.DataTextField = "CityName";
ddlcity.DataSource = ds;
ddlcity.DataMember = "City";
ddlcity.DataBind();
con.Close();
}
}
}
Thanks
If this post helps you mark it as answer
0
RanjithKumar chiguruvada
0
240
107.4k
Sep 14 2012 10:27 AM
Hi,
Take two dropdown lists with following as
<asp:ScriptManager ID="sc1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="up1" runat="server"><ContentTemplate>
<div>
<asp:DropDownList ID="ddlFirst" runat="server" Height="20" Width="90"
AutoPostBack="true"
onselectedindexchanged="ddlFirst_SelectedIndexChanged" >
<asp:ListItem Value=""></asp:ListItem>
<asp:ListItem Value="1"></asp:ListItem>
<asp:ListItem Value="2"></asp:ListItem>
<asp:ListItem Value="3"></asp:ListItem>
<asp:ListItem Value="4"></asp:ListItem>
</asp:DropDownList>
</div>
<div>
<asp:DropDownList ID="ddlSecond" runat="server" Height="20" Width="90"
onselectedindexchanged="ddlSecond_SelectedIndexChanged" AutoPostBack="true" >
<asp:ListItem Value=""></asp:ListItem>
<asp:ListItem Value="1"></asp:ListItem>
<asp:ListItem Value="2"></asp:ListItem>
<asp:ListItem Value="3"></asp:ListItem>
<asp:ListItem Value="4"></asp:ListItem>
</asp:DropDownList>
</div>
</ContentTemplate>
</asp:UpdatePanel>
and write the events in code behind file as
protected void ddlFirst_SelectedIndexChanged(object sender, EventArgs e)
{
var first = ddlFirst.SelectedValue.ToString();
ddlSecond.SelectedValue = first.ToString();
}
protected void ddlSecond_SelectedIndexChanged(object sender, EventArgs e)
{
var second = ddlSecond.SelectedValue.ToString();
ddlFirst.SelectedValue = second.ToString();
}
That's it.
if any queries please let me ask.
Avoid postback when binding gridview
Difference between break and continue statement