No interaction between dropdownlist and DataGrid or repeater through Ajax
<% @ Page Language = "C #" codebehind = "example. aspx. cs" autoeventwireup = "false" inherits = "webs. Other. Example" %>
<HTML>
<Head>
<Title> example </title>
<Meta name = "generator" content = "Microsoft Visual Studio. NET 7.1">
<Meta name = "code_language" content = "C #">
<Meta name = "vs_defaultclientscript" content = "JavaScript">
<Meta name = "vs_targetschema" content = "http://schemas.microsoft.com/intellisense/ie5">
<SCRIPT>
Function load (state ){
VaR drp2 = Document. getelementbyid ("dropdownlist2 ");
For (I = drp2.length; I> = 0; I --){
Drp2.options. Remove (I );
}
VaR ohttpreq = new activexobject ("msxml2.xmlhttp ");
VaR odoc = new activexobject ("msxml2.domdocument ");
Ohttpreq. Open ("Post", "getdata. aspx? State = "+ state, false );
Ohttpreq. Send ("");
Result = ohttpreq. responsetext;
Odoc. loadxml (result );
Items1 = odoc. selectnodes ("// city/table/ID ");
Items2 = odoc. selectnodes ("// city/table/shiname ");
VaR itemslength = items1.length;
For (I = 0; I <itemslength; I ++)
// Assign the class name and number of the sub-class to dropdownlist2
{
VaR newoption = Document. createelement ("option ");
Newoption. Text = items2 [I]. text;
Newoption. value = items1 [I]. text;
Drp2.options. Add (newoption );
}
}
Window. onload = function () {load ('1 ');}
</SCRIPT>
</Head>
<Body ms_positioning = "flowlayout">
<Form ID = "form1" method = "Post" runat = "server">
<Asp: dropdownlist id = "dropdownlist1" runat = "server"> </ASP: dropdownlist>
<Asp: dropdownlist id = "dropdownlist2" runat = "server"> </ASP: dropdownlist>
<Asp: textbox id = "th" runat = "server"> </ASP: textbox>
<Asp: button id = "button1" runat = "server" text = "button"> </ASP: button>
</Form>
</Body>
</Html>
Cs source file:
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Namespace webs. Other
{
/// <Summary>
/// Summary of example.
/// </Summary>
Public Class Example: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. dropdownlist dropdownlist1;
Protected system. Web. UI. webcontrols. textbox th;
Protected system. Web. UI. webcontrols. Button button1;
Protected system. Web. UI. webcontrols. dropdownlist dropdownlist2;
Private void page_load (Object sender, system. eventargs E)
{
// Place the user here Code To initialize the page
If (! This. ispostback)
{
// Create a data source to load the first dropdownlist. You can also load the second one by default.
Sqlconnection con = new sqlconnection (system. configuration. configurationsettings. deleettings. Get ("connstr1 "));
Sqldataadapter da = new sqldataadapter ("select ID, shengname from Province", con );
Dataset DS = new dataset ();
Da. Fill (DS );
This. dropdownlist1.datasource = Ds;
This. dropdownlist1.datatextfield = "shengname ";
This. dropdownlist1.datavaluefield = "ID ";
This. dropdownlist1.databind ();
// Here is the binding client event. When the first dropdownlist option changes, the following event onchange is triggered. This event will call a client method load ()
This. dropdownlist1.attributes. Add ("onchange", "load (this. Options [This. selectedindex]. Value )");
}
}
# Region web form designer generated code
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. button1.click + = new system. eventhandler (this. button#click );
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
Private void button#click (Object sender, system. eventargs E)
{
Th. Text = This. Request. Form ["dropdownlist2"]. tostring ();
}
}
}
Getdata source file
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Using system. xml;
Using system. configuration;
Using system. text;
namespace webs. Other
{< br> ///
// abstract description of getdata.
//
public class getdata: system. web. UI. page
{< br> private void page_load (Object sender, system. eventargs e)
{< br> // place the user code here to initialize the page
// response. write (request ["state"]);
int shengno = int. parse (request ["state"]. tostring ();
sqlconnection con = new sqlconnection (system. configuration. configurationsettings. appsettings. get ("connstr1");
sqldataadapter da = new sqldataadapter ("select ID, shiname from city where shengid = '" + shengno + "'", con );
dataset DS = new dataset ("city");
da. fill (DS);
xmltextwriter writer = new xmltextwriter (response. outputstream, encoding. utf8);
writer. formatting = formatting. indented;
writer. indentation = 4;
writer. indentchar = '';
writer. writestartdocument ();
DS. writexml (writer);
writer. flush ();
response. end ();
writer. close ();
}
# region web form designer generated code
override protected void oninit (eventargs e)
{< br> //
// codegen: this call is ASP.. NET web form designer.
//
initializecomponent ();
base. oninit (E );
}< br>
///
// The method required by the designer. Do not use the code editor to modify the method.
/// the content of this method.
//
private void initializecomponent ()
{< br> This. load + = new system. eventhandler (this. page_load);
}< BR ># endregion
}< BR >}