Asp.net implements the linkage control of Brushless newest dropdownlist

Source: Internet
Author: User
ASP. net brings us the programming mechanism of the event model, which enables us to put all the tasks on the server and execute even a small change. In fact, this is not a problem, however, we can't bear it. If we need to refresh the content page in an input box, we need to update another dropdownlist to change the selection item of dropdownlist, which is really depressing. Next I will describe a primitive method. The reason for this is that this method is stored in ASP. net already exists before. I don't need to describe the relationship between the two. What we want to talk about today is how to update dropdownlist without refreshing a new page. This method is designed to attract others, in fact, this method can be used to implement a lot of applications that interact with the background without refreshing the web page. Let's just stop talking nonsense. Let's look at our example. First, we need to place two dropdownlist pages, the page code is as follows:
<% @ 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>

The above page contains two dropdownlist and a JS script, which can be directly written on the page or written in the background on the regeist page (the latter is more flexible) the background code of this page is as follows,
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 user code here 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 ();
}
}
} In the above Code, we did two things: 1. help to define one dropdownlist (you can also bind two at the same time ). 2. Specify the client script of the control. Next we will introduce the above JS Code in detail. First, we will get the associated dorpdownlist object on the page, clear its options, and then create two client objects, ohttpreq and odoc, one of them is responsible for sending the request and the other is responsible for obtaining the response result. We will send the user-selected state to the page named webform6.aspx, which will process the request and return a response, the response is an XML file. I will introduce the code in webform6.aspx later. We load the returned results to the odoc object using the loadxml method, and then we can use the selectnodes method to get all the city nodes, and then cycle these nodes to create the option object on the client, add these option objects to dropdwonlist2. 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
{
/// <Summary>
/// Summary of getdata.
/// </Summary>
Public class getdata: system. Web. UI. Page
{
Private void page_load (Object sender, system. eventargs E)
{
// Place 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. deleettings. 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)
{
//
// 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. Load + = new system. eventhandler (this. page_load );
}
# Endregion
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.