A simple Ajax example in c #. net -- Ajax. dll

Source: Internet
Author: User

C #. net under the simple Ajax example -- Ajax. dll http://www.diybl.com/network CLICK: 1741 [comment]
-
-
Article Search: [Click to package this article]
[Activate online QQ discussion group on this site]

I have studied how to use Ajax. dll to implement the previous example. It is very frustrating because it is simpler, less code, and only a bit of it. Therefore, if Ajax is required for a project, we suggest you study the framework, such as Ext. Its Class Libraries and documents are quite rich and easy to use.

Okay, I will list the implementation process. First, we need an Ajax. dll file. Find it by yourself! Put it in the project and add references. First, you need a class named "AjaxMethod. cs" as follows:

 

Namespace Test. AjaxTest
{
/// <Summary>
/// Summary of AjaxMethod.
/// </Summary>
Public class AjaxMethod
{
Public AjaxMethod (){}

// Database query operation
[Ajax. AjaxMethod (Ajax. HttpSessionStateRequirement. Read)]
Public DataSet GetList (string id)
{
String SQL = "select CityName, CityCode from City where [ID] =" + id;
Return SelectData (SQL); // call the following method
}

//
Private DataSet SelectData (string SQL)
{
// Database link definition:
// <Configuration> <etettings> </appSettings> </configuration> in Web. config
// Add definition: <add key = "ConnectionString" value = "data source = localhost;
// Initial catalog = helpalog 1; user id = sa; password = sa; "> </add>
String cs = System. Configuration. ConfigurationSettings
. Etettings ["ConnectionString"];
// Or define it like this.
// String cs = "Server = (local); UID = sa; PWD = sa; Database = help1_1 ";
SqlDataAdapter sda = new SqlDataAdapter (SQL, cs );

DataSet ds = new DataSet ();
Sda. Fill (ds );
Return ds;
}
}
}
Article Source: http://www.diybl.com/course/4_webprogram/asp.net/asp_netshl/2007125/90636.html

 

The following figure shows the page a. aspx to achieve the effect. The <body> part is simple:

 

<Body>
<Form id = "Form1" method = "post" runat = "server">
<Select id = "AList" onchange = "GetBList ()">
<Option value = "0"> A </option>
<Option value = "1"> B </option>
<Option value = "2"> C </option>
</Select>
<Select id = "BList"> </select>
</Form>
</Body> when the onchange event is triggered on the page, the GetBList () method is entered. The following shows the complete JavaScript code for the page:

<Script language = "javascript">
Function GetBList (){
// Call the Database Operation Method
Var av = document. getElementById ("AList"). value;
// Call the method of the AjaxMethod class. Here there are two parameters. I guess it is a delegate,
// Pass the GetList execution result DataSet to the SetBList method, hiding
AjaxMethod. GetList (av, SetBList );
}

Function SetBList (response ){
If (response! = Null ){
Var ds = response. value; // return set

// This is quite special. At first, I felt something went wrong. Why is the js syntax the same as c #?
// The Regular Expression in Ajax. dll should be strong! However, this is a bit unpleasant.
// During the test, ds. Tables [0]. Rows. lenght is written as ds. Tables [0]. Rows. Count
// Js will not find the object error.
// We still need to get used to its rules. After all, it is not entirely c #, but it just defines a rule similar to c #
// Very similar syntax rules
If (ds! = Null & typeof (ds) = "object" & ds. Tables! = Null ){
Alert (ds. Tables [0]. Rows. length );
For (var I = 0; I <ds. Tables [0]. Rows. length; I ++ ){
Var option = document. createElement ("OPTION ");
Option. value = ds. Tables [0]. Rows [I]. CityCode;
Option. text = ds. Tables [0]. Rows [I]. CityName;
Document. Form1.BList. options. add (option );
}
}
}
}
</Script> This example ends, and many complex operations are encapsulated. What we learned is how to use it. That's all. However, when we catch up with the project, it can bring a lot of convenience.

Article Source: http://www.diybl.com/course/4_webprogram/asp.net/asp_netshl/2007125/90636_2.html

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.