Ajaxpro implements data transmission at the front-end

Source: Internet
Author: User
Ajaxpro implements data transmission at the front-end

Keywords: Ajaxpro passing Parameters

1. Create the. net2005 website test.

2. Reference ajaxpro. dll in the bin folder, or directly place the bin directory under the test website.

3. Add the following in webconfig:Code

<System. Web>
<Httphandlers>
<Add verb = "post, get" Path = "ajaxpro/*. ashx" type = "ajaxpro. ajaxhandlerfactory, ajaxpro.2"/>
</Httphandlers>

4. Write the code in the backend Cs:

Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Ajaxpro. Utility. registertypeforajax (typeof (_ default ));
}

[Ajaxpro. ajaxmethod]
Public String senddata (string rq)
{
RQ = "2006 ";
String STR;
STR = "sdfsdfsdfsdfsfdsffdfs" + RQ;
Return STR;
}
}

5. Write the code at the front-end:

<SCRIPT>
Function diaoyong ()
{
VaR C;
C = Hello. _ default. senddata ("2006", handle );

// Note: the parameter is passed. "2006" after senddata is passed to the background as the Foreground Data. Handle Function
// It is the processing function. The default value is this. In fact, it is best not to write it like this, but there is a problem with debugging and printing,
// Doubt ing.
}

Function handle (RES)
{
Alert (res. value );
}

</SCRIPT>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Input id = "button1" type = "button" value = "Search" onclick = "diaoyong ();"/>
<Input id = "text1" type = "text"/> </div>
</Form>
</Body>

// Question 1: The handle function must be written; otherwise, the output is an object. I don't know why.
// Question 2: If the namespace is specified in. CS: [ajaxpro. ajaxnamespace ("hello")]

// Write "Hello. _ default. senddata ()" In JavaScript to indicate that the request is empty or not an object.

 

 

Using ajaxpro. dll,
For. NET 2.0, use ajaxpor.2.dll

Notes
1. Reference using ajaxpro;
2. Add </system. Web> in the web. config file
<Httphandlers>
<Add verb = "post, get" Path = "ajaxpro/*. ashx"
Type = "ajaxpro. ajaxhandlerfactory, ajaxpro"/>
</Httphandlers>
3. The form at the front-end must be runat = server. Otherwise, an error is returned ************

4. *. aspx. CS File
Add the last row of the class definition to the next row of the namespace
[[Ajaxpro. ajaxnamespace ("current class name")]
Note: This version is applicable to version 6.10.6.1. Different versions may vary.

Adding [ajaxpro. ajaxmethod] to the previous line of the defined method indicates that this method is an Ajax method.

Add the registration class to the page_load () method:
Ajaxpro. Utility. registertypeforajax (typeof (class name that defines the Ajax method ));

5. Use the Ajax method in javascript:

Ajax method (parameter, callback function name)

 

Example:

Foreground ---- JavaScript -----
Function delrec (){
VaR n = 1;
If (n <1) return;
VaR SSID = Document. getelementbyid ('dgrid'). Rows [N]. cells [0]. innerhtml;

If (confirm ("are you sure delete current select record, id =" + SSID) = false) return;
VaR xmlstr = "<? XML version = '1. 0' encoding = 'utf-8'?> <Rec> <Sid> "+ SSID +" </Sid> </REC> ";
Webajaxact. delrec (xmlstr, deltablerow );
}

Function deltablerow (result ){
VaR xmldoc = new activexobject ("Microsoft. xmldom ");
// Alert ("Return Value:" + result. value );
Xmldoc. loadxml (result. value );
VaR n1_xmldoc.doc umentelement. childnodes [0]. childnodes [0]. nodevalue;
// Delete a table row
}

* **. Aspx. CS file (pay attention to the code of ********* and compare it with the above description)
-------- C #------

Using ajaxpro;

Namespace wtest {

[Ajaxpro. ajaxnamespace ("webajaxact")] **************
Public class webajaxact: system. Web. UI. Page
{

Private void page_load (Object sender, system. eventargs e ){
Ajaxpro. Utility. registertypeforajax (typeof (webajaxact ));***********

}

[Ajaxpro. ajaxmethod] *************
Public String updaterec (string szxml ){
Other Processing

String xmlstr = "";
// Assign values to xmlstr
Return xmlstr;
}
}
}

 

Ajaxpor + XML makes it easy to implement no refreshing pages
Principle: The background method of ajaxpro is used to return paging XML, and xml2table is used for foreground display.

<Input onclick = "showpage ('1')" type = "button" value = "first">
<Input onclick = "showpage ('2')" type = "button" value = "Prev">
<Input onclick = "showpage ('3')" type = "button" value = "Next">
<Input onclick = "showpage ('4')" type = "button" value = "last">
<Asp: DataGrid id = "dgrid" runat = "server"> </ASP: DataGrid>

Function showpage (I)
{
If (I = "1") curpage = 1;
If (I = "2") curpage --;
If (I = "3") curpage ++;
If (I = "4") curpage = npage;

If (curpage <= 1) curpage = 1;
If (curpage> npage) curpage = npage;

Ajaxpro class. Method (curpage, showtable = function (result ){
If (result. value! = NULL) {xml2table ('dge', result. value );
}
);
}

Function xml2table (TB, szxml ){
If (szxml = NULL | szxml = "") return;
Varxmldoc = new activexobject ("Microsoft. xmldom ");
Xmldoc. async = false;
Xmldoc. loadxml (szxml );

If (xmldoc. parseerror. errorcode! = 0 ){
// Var TXT = "return data format error:" R "N" R "N ";
// TXT = TXT + "error code:" + xmldoc. parseerror. errorcode + "" N ";
// TXT = TXT + "error reason:" + xmldoc. parseerror. reason;
// TXT = TXT + "error line:" + xmldoc. parseerror. line;
// Alert (txt );
Return;
}

Varx=xmldoc.doc umentelement;
Varm = x. childnodes. length;
If (M = 0) return;
Varn = x. childnodes [0]. childnodes. length;

Vart = Document. getelementbyid (TB );
For (vari = T. Rows. Length-1; I> 0; I --) T. deleterow (I );

Vartr;
Vartd;

For (var j = 0; j <m; j ++ ){
Tr = T. insertrow (-1 );
// Tr. Height = 16;
For (I = 0; I <n; I ++ ){
TD = tr. insertcell (-1 );
Try {
TD. innerhtml = x. childnodes [J]. childnodes [I]. childnodes [0]. nodevalue;
}
Catch (ERR) {TD. innerhtml = "";}
}
}
}

 

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.