Lets Have a fun with Technology.

BTemplates.com

Follow by Email

Saturday, September 6, 2008

Coding to populate state and city Acording to country Without Post back In Asp.net


Here I Show how to Fill Country,State and city.State Depend On Country So
if Country changed then State Dynamically Changed Without Postback.Similar City Depend on state So if We Change State then need to city also changed this can be done in asp.net without postback with
following methods :
In Asp.net We Need to Register Script so later we can call this Function
Write down Following code in .Cs
protected void country_fill()
{
DataSet Search_dataset = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter("(select state_code,state_name,country_code from state_master) (select city_code,city_name,state_code from city_master)", con);
sda.Fill(Search_dataset);
string str = "";
str = " < script language=" + '"' + "javascript" + '"' + " > \n";
str += "var state = Array(" + (Search_dataset.Tables[0].Rows.Count + 1) + ");\n";
str += "var city = Array(" + (Search_dataset.Tables[1].Rows.Count + 1) + ");\n";
str += "function createarray(){\n";
str += "var i;\n";
str += "//create item array \n";
str += "for(i=0;i<>
str += "var a = new Array(3);\n";
str += "state[i] = a; }\n";
int i = 0;
foreach (DataRow Objdrow in Search_dataset.Tables[0].Rows)
{
str += "state[" + i + "][0]='" + Objdrow["state_code"] + "';\n";
str += "state[" + i + "][1]='" + Objdrow["state_name"] + "';\n";
str += "state[" + i + "][2]='" + Objdrow["country_code"] + "';\n";
i += 1;
}
str += "var j;\n";
str += "//create item array \n";
str += "for(j=0;j<>
str += "var b = new Array(3);\n";
str += "city[j] = b; }\n";
i = 0;
foreach (DataRow Objdrow1 in Search_dataset.Tables[1].Rows)
{
str += "city[" + i + "][0]='" + Objdrow1["city_code"] + "';\n";
str += "city[" + i + "][1]='" + Objdrow1["city_name"] + "';\n";
str += "city[" + i + "][2]='" + Objdrow1["state_code"] + "';\n";
i += 1;
}
str += "}\n"; //end of function
str += " < /script > \n";
//Page.RegisterClientScriptBlock("fill_item", str);
Page.RegisterClientScriptBlock("fill_item", str);
}
Now in ASpx Page On combo Box Changed Write Down following Code.
function fill_spec() //handle keyup on model_name
{
var selectcmb_func,spec_array_length,selectfunc_id,selectcmb_spec,selecttxt_spec_id;
spec_array_length = state.length;
selectcmb_func = document.forms.DropDownList1;
selectcmb_func=document.getElementById("");
selectcmb_spec = document.getElementById("");
selectfunc_id = selectcmb_func.options[selectcmb_func.selectedIndex].value;
numShown=0;
selectcmb_spec.length=0;
selectcmb_spec[numShown] = new Option("-- Select State -- ","0");
numShown++;
for(i = 0; i <>
{
if( (state[i][2]==selectfunc_id) || (state[i][2]==0))
{
selectcmb_spec[numShown] = new Option(state[i][1],state[i][0]);
numShown++;
}
}
}
function fill_city() //handle keyup on model_name
{
var city_length,ddl_state,ddl_city,id,numShown;
city_length = city.length;
ddl_state=document.getElementById("");
ddl_city = document.getElementById("");
id = ddl_state.options[ddl_state.selectedIndex].value;
numShown=0;
ddl_city.length=0;
ddl_city[numShown] = new Option("-- Select City -- ","0");
numShown++;
for(i = 0; i <>
{
if( (city[i][2]==id) || (city[i][2]==0))
{
ddl_city[numShown] = new Option(city[i][1],city[i][0]);
numShown++;
}
}
}
hear you need to call function createarray .You can call this function in page load.


Prev Post                                                      All Post                                                           Next Post


0 comments:

Post a Comment