How to load Dropdown list in selected event bind the Data from Db.

Jayakumars
Posted by Jayakumars under ASP.NET AJAX category on | Points: 40 | Views : 2178
hi
I have created simple Sample for Dropdownlist load and selected event another dropdownlist loaded data from the fetch db and bind them using
Update Panel

Client Side Coee

<form id="form1" runat="server">
<div>
<asp:UpdatePanel ID="up1" runat="server" >
<ContentTemplate>


From
<asp:DropDownList ID="Drp1" runat="server" AutoPostBack="True"
onselectedindexchanged="Drp1_SelectedIndexChanged">
</asp:DropDownList>

To
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged"
>
</asp:DropDownList>

<asp:LinkButton ID="lnkClear" runat="server" Text ="Clear" onclick="lnkClear_Click"></asp:LinkButton>
</div>

</ContentTemplate>
</asp:UpdatePanel>
</form>


Server Side c# Code


SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConnect"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{

DataTable dtt = new DataTable();
sqlcon.Open();
SqlDataAdapter sqladp = new SqlDataAdapter(" Select * from tbl_Time", sqlcon);
sqladp.Fill(dtt);
DateTime ddd = new DateTime();

Drp1.DataSource = dtt;
Drp1.DataTextField = "Name";
Drp1.DataValueField = "Id";
Drp1.DataBind();
DropDownList1.Items.Insert(0, "Select");
}
}
protected void Drp1_SelectedIndexChanged(object sender, EventArgs e)
{

DataTable dtt = new DataTable();
//sqlcon.Open();
SqlDataAdapter sqladp = new SqlDataAdapter(" Select * from tbl_Time where id="+ Convert.ToInt32(Drp1.SelectedValue) +"", sqlcon);
sqladp.Fill(dtt);

DropDownList1.DataSource = dtt;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "Id";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, "Select");
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dtt = new DataTable();
//sqlcon.Open();
SqlDataAdapter sqladp = new SqlDataAdapter(" Select * from tbl_Time where id=" + Convert.ToInt32(Drp1.SelectedValue) + "", sqlcon);
sqladp.Fill(dtt);

Drp1.DataSource = dtt;
Drp1.DataTextField = "Name";
Drp1.DataValueField = "Id";
Drp1.DataBind();
DropDownList1.Items.Insert(0, "Select");
}
protected void lnkClear_Click(object sender, EventArgs e)
{
DataTable dtt = new DataTable();
//sqlcon.Open();
SqlDataAdapter sqladp = new SqlDataAdapter(" Select * from tbl_Time", sqlcon);
sqladp.Fill(dtt);
DateTime ddd = new DateTime();

Drp1.DataSource = dtt;
Drp1.DataTextField = "Name";
Drp1.DataValueField = "Id";
Drp1.DataBind();
DropDownList1.Items.Insert(0, "Select");
}

Comments or Responses

Posted by: Vinay13mar on: 11/3/2012 Level:Starter | Status: [Member] | Points: 10
Hi this is really nice article . i have found and article this explains how to bind ddl in mvc3

http://www.dotnetpools.com/Article/ArticleDetiail/?articleId=61&title=Binding%20Multiple%20Dropdownlist%20In%20MVC3%20Using%20C#

Login to post response