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


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Bind a dropdown according to data in another drop down


Posted Date: 19 Aug 2008    Resource Type: Code Snippets    Category: AJAX

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



Code for client side written within script:


function CallTemplate()
{

//alert(document.getElementById('cmbAssignTo_cmbSimple').value);

var objDd= document.getElementById('ddlmenu')

var strValue = objDd.options[objDd.selectedIndex].value;

//Create XMLHttpRequest object:'request'

CreateXmlHttpRequestObject();

//Build the required Query string
var sPass=1;

var retrieveParam = 'id='+ strValue;

var retrieveURL = "AJAX.aspx";
var url = retrieveURL + '?' + retrieveParam;

request.open('GET',url,true);

request.onreadystatechange = CheckTemplate;

request.send(null);
}

function CheckTemplate()
{
document.getElementById("ddlsubmenu").innerText="";
if(request.readyState == 4 || request.readyState == 'complete')
{
if(request.status == 200)
{
var opt=document.createElement("option");
opt.value="-1";
opt.text="----select subcategory---";
document.getElementById("ddlsubmenu").add(opt);
var DdVisitTemp = document.getElementById("ddlmenu");

var rootFile = request.responseXML.documentElement;

var Id = rootFile.getElementsByTagName('Id');

var Name = rootFile.getElementsByTagName('Name');

if(Id.length > 0 || Name.length > 0)
{

for(var i=0;i {

if(Name.length > 0)
{
var opt=document.createElement("option");

opt.value=Id[i].text;

opt.text=Name[i].text;

document.getElementById("ddlsubmenu").add(opt);

}

}

}

}// inner IF

} // Outer IF

}

function CreateXmlHttpRequestObject()
{
try
{
request = new XMLHttpRequest();
}
catch(nething)
{
try
{
request = new ActiveXObject('Msxml2.XMLHTTP');
}
catch(othr)
{
try
{
request = new ActiveXObject('Microsoft.XMLHTTP');
}
catch(err)
{
request = false;
}
}
}
if(!request)
{
alert("Error initializing XMLHttpRequest object!");
}
}




code for ajax page i.e ajax.aspx




private void Page_Load(object sender, System.EventArgs e)
{

string id=string.Empty ;
System.Text.StringBuilder strXml;
string passedValue = Request.QueryString["id"];

string strFill=" select PK_PRIMARY_KEY,TITLE_SUBMENU from SUBJECT_SUBMENU where FK_SUBJECTMENU_ID = '" + id + "' ";

DataTable dttemp= con.connectiondatatable(strFill);



int j=dttemp.Rows.Count;
strXml = new System.Text.StringBuilder ();
strXml.Append("");
XmlDocument xTemplate = new XmlDocument();
xTemplate.LoadXml(strXml.ToString());

Response.ContentType = "text/xml";
Response.CacheControl = "No-cache";
xTemplate.Save(Response.OutputStream);

// Put user code to initialize the page here
}



There are two tables
1.SUBJECTMENU
2.SUBJECT_SUBMENU

Relation between them

SUBJECT_SUBMENU.FK_SUBJECTMENU_ID=SUBJECTMENU.PK_SUBJECTMENU_ID

Step 1:

create two tables must have relationship between them like above.

step 2:

Bind 1st dropdown on page load.

step 3:

Write code for client side as well as ajax page.

When u change 1st dropdown ,2nd dropdown will change.

How to create a ajax page:

Add a webform and delete all code that will bydefault in client except tag containing page inherit.

Example:

<%@ Page language="c#" Codebehind="AJAX.aspx.cs" AutoEventWireup="false" Inherits="KnowledgeSharing.AJAX" %>

except this one







Responses

Author: Curve Technologies    06 Sep 2008Member Level: Bronze   Points : 2
Its good that you are going through a basic Ajax concept, but i believe with the ajaxtoolkit 2.0 we have the cascading dropdown in which the second dropdown is populated based on hte data selection in the first dropdown and that could be far more easy and also will have an updated knowledge.
Thanks...


Author: abhay    10 Sep 2008Member Level: Gold   Points : 2

hi curve
if you aren't using ajax toolkit but u have to use ajax 1 or 2 places then it isn't necessary to use ajax toolkit .you can use simple ajax like this.


Author: abhay    10 Sep 2008Member Level: Gold   Points : 2

hi curve
if you aren't using ajax toolkit but u have to use ajax 1 or 2 places then it isn't necessary to use ajax toolkit .you can use simple ajax like this.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
This must help u  .  Like cascading dropdown  .  Binding datas  .  Binding datagrid  .  Bindi tables  .  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: File exist or not in harddisk
Previous Resource: AJAX in Pure ASP
Return to Discussion Resource Index
Post New Resource
Category: AJAX


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

accuconference

Contact Us    Privacy Policy    Terms Of Use