C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Interview   Jobs   Projects   Offshore Development    
Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing | Talk to Us |



My Profile

Gifts

Active Members
TodayLast 7 Days more...







Bind DropDownList Through AJAX


Posted Date: 05 Jul 2008    Resource Type: Code Snippets    Category: ASP.NET WebForms

Posted By: Subhi       Member Level: Gold
Rating:     Points: 10



This code sample consists of the .aspx page and the code-behind .cs file. The example explains how to bind a DropDownList control on a web form with AJAX.

.aspx code
===========



<script type="text/javascript" language="javascript">
function DDLBind()
{
var id= document.getElementById("ddlFirst").options.value;
DropDownBind.bindDDLModel(id,DDL);
}
</script>
<script type="text/javascript" language="javascript">
<!--
function DDL(response)
{
var ds=response.value;
var obj=document.getElementById("ddlSecond");

while(obj.options.length)
{
obj.remove(0);
}

obj.options[0]=new Option("Please wait....","0");

if(ds != null && typeof(ds) == "object" && ds.Tables != null)
{
obj.options[0]=new Option("-- Select --","0");
for(var i=0,j=1;i {
//alert(ds.Tables[0].Rows.length);
obj.options[j]=new Option(ds.Tables[0].Rows[i].Model,ds.Tables[0].Rows[i].ModelId);
}
}
else
{
obj.options[0]=new Option("Please try again");
}
}
-->
</script>



<table width="100%">
<tr>
<td>First DropDownList :</td>
<td><asp:DropDownList ID="ddlFirst" runat="server" onchange="javascript:return DDLBind();" /></td>
</tr>
<tr>
<td>Second DropDownList :</td>
<td><asp:DropDownList ID="ddlSecond" runat="server" ></td>
</tr>
</table>



.cs file
=========



public partial class DropDownBind : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("server=server;database=click2ins;uid=sa;pwd=");
SqlTransaction Tran;
protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(DropDownBind));
try
{
con.Open();
Tran = con.BeginTransaction(IsolationLevel.ReadCommitted);
SqlCommand cmd = new SqlCommand("select MakeId,MakeName from ms_make", con, Tran);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "Make");
ddlFirst.DataSource = ds.Tables["Make"];
ddlFirst.DataValueField = "MakeId";
ddlFirst.DataTextField = "MakeName";
ddlFirst.DataBind();
Tran.Commit();
}
catch
{
Tran.Rollback();
}
finally
{
con.Close();
}
}

[Ajax.AjaxMethod()]
public DataSet bindDDLModel(string id)
{
DataSet ds = new DataSet();
try
{
con.Open();
Tran = con.BeginTransaction(IsolationLevel.ReadCommitted);
string query = "select ModelId,Model from ms_model where MakeId='" + id + "'";
SqlCommand cmd = new SqlCommand(query, con, Tran);
SqlDataAdapter da = new SqlDataAdapter(cmd);

da.Fill(ds, "Model");
Tran.Commit();
}
catch
{
Tran.Rollback();
}
finally
{
con.Close();
}
return ds;
}
}





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
For Fast  .  Execution  .  DropDown Bind  .  AJAX  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Dynamically creating controls(for example checkbox on datagrid) without using Template Column
Previous Resource: Dynamic Table Through AJAX
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET WebForms


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

budget conference call

Contact Us    Privacy Policy    Terms Of Use