Adsence750x90

Monday, May 5, 2008

AJAX DropDownList using UpdatePanel in C# and ASP.NET

Simple ASP.NET AJAX DropDownList USING UpdatePanel In C# ASP.NET 2005

Replace all "(" with '<' and also ")" with '>"

You can download AJAX controls in asp.net or From AJAX CONTROLS . Ajax stands for Asynchronous Javascript And XML. Here am going to bind data form database using AJAX UpdatePanel, and Binding city state and country for respected DropDownList.
As usual u can bind country DropDownList

C# code for Country DropDownList


protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
SqlConnection con = new SqlConnection("Data Source=SERVER; Integrated Security=SSPI;persist security info=True;Initial Catalog=DBNAME;");
SqlCommand cmd = new SqlCommand("SELECT CountryName,CountryId FROM CountryTable", con);
DataTable dt = new DataTable();
con.Open();
SqlDataAdapter adptr = new SqlDataAdapter(cmd);
adptr.Fill(dt);
con.Close();
if (dt.Rows.Count > 0)
{
ddlCountry.Items.Clear();
ddlCountry.Items.Add("SELECT");
foreach (DataRow drow in dt.Rows)
{
ListItem lst = new ListItem();
lst.Value = drow[1].ToString();
lst.Text = drow[0].ToString();
ddlCountry.Items.Add(lst);
}
}
}

}

protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlCountry.SelectedValue.ToString() != "SELECT")
{
SqlConnection con = new SqlConnection("Data Source=SERVER; Integrated Security=SSPI;persist security info=True;Initial Catalog=DBNAME;");
SqlCommand cmd = new SqlCommand(string.Format("SELECT StateName,StateId FROM StateTable WHERE CountryId='{0}'", ddlCountry.SelectedValue.ToString()), con);
DataTable dt = new DataTable();
con.Open();
SqlDataAdapter adptr = new SqlDataAdapter(cmd);
adptr.Fill(dt);
con.Close();
if (dt.Rows.Count > 0)
{
ddlState.Items.Clear();
ddlState.Items.Add("SELECT");
foreach (DataRow drow in dt.Rows)
{
ListItem lst = new ListItem();
lst.Value = drow[1].ToString();
lst.Text = drow[0].ToString();
ddlState.Items.Add(lst);
}
}
}
}
HTML code

(asp:dropdownlist id="ddlCountry" runat="server" onselectedindexchanged="ddlCountry_SelectedIndexChanged" autopostback="True")(/asp:dropdownlist)
(br) (asp:updatepanel id="upnl" runat="server")(br) (contenttemplate)(br) (asp:dropdownlist id="ddlState" runat="server")(/asp:dropdownlist)(br) (/contenttemplate)(br) (triggers)(br) (asp:asyncpostbacktrigger controlid="ddlCountry")(br) (/asp:asyncpostbacktrigger)


12 comments:

Rupali Rathi said...

This example is excellent.It helps me
Thanks a lot

Arun said...

This example is excellent.

I have a doubt as well,how can i generate numbers ranging from 0 to 100 in a simple dropdownlist using c#?

Raju.M said...

private void FillDropDown()
{
DropDownList1.Items.Clear(); DropDownList1.Items.Add("Select");
for (int i = 0; i <= 100; i++)
{ DropDownList1.Items.Add(i.ToString());
}
}

Arun said...

Hi Raju..Thanks alot.

I have one more query.
My designer has given me the html format of our proposed design.but how can i integrate that html page to the .net framework?

I have changed the extension .html to .aspx.but still my controlls such as dropdownlist or buttons are not recognized.When ever i am double clickking on these controls instead of taking to the code area of c#,it directs me to the html page again.Please guide me.

Raju.M said...

add new page in your project.then select design that your designer given.take that main table from your design and paste that in your .aspx page, between the form tag.and change html control to asp controls.

Arun said...

I tried in that way.
but couldn't do.

i am adding some portion of the html code here,


input type="radio" name="radio" id="radio" value="radio" Residential

Are you saying this,
input type="radio" name="radio" id="radio"
To be changed something like this?
asp:radiobutton id="radio" runat="server"??

Raju.M said...

yes good,
asp:RadioButton ID="rdbMale" runat="server", like that

Arun said...

Thank you man.Thanx a million.

Arun said...

Hi Raju..
I am facing a problem.see when you register with a site,after completing the registration process,it will take you to the next level with out re-login again.
how can i do this?

Sathish said...

Hi,
I have a doubt , When I click the textbox mean how I can clear the textbox’s text. Like TextBox Text is Name if I click the text box mean it should be clear and I’ll type my name.

Anonymous said...

Its really helpful .Saved my time.Thank you .

Anonymous said...

DropDown List Control