Directory (?) [-]
- 1 JS Code
- 2 page-related controls with platform-encapsulated controls the normal DropDownList can also
- 3 Background C code
- Precautions
The original Microsoft encapsulated AJAX control Updatepannel and ScriptManager to achieve no-flush linkage, but the unexpected phenomenon has been caused by abnormal linkage. Do the demo alone is nothing, put in the system on the problem, after debugging still puzzled. All roads lead to Rome, still not believe can be suppressed to death. As we all know, jquery encapsulates Ajax, calling it more useful than native Ajax, implementing asynchronous refreshes of course, although the function is simple, look directly at the code:
1. JS Code:
[JavaScript]View PlainCopy
- <span style="FONT-SIZE:18PX;" ><span style="FONT-FAMILY:KAITI_GB2312;FONT-SIZE:14PX;" > <script type="Text/javascript" language="JavaScript" >
- $ (function () {
- //Take advantage of DropDownList's Change event
- $ ("#hbKeyWordsType"). Change (function () {
- //Get keyword type
- var keyType = $ ("#hbKeyWordsType"). Val ();
- $.ajax ({
- URL: "wxkeywords_wxmessagefunoperation.aspx/hbkeywordstype_selectedindexchanged", //Call back Method: Page/Method Name
- Type: "POST",
- Data: "{typecode:" + KeyType + "}", //To the background, the parameter name must be consistent with the parameters of the method
- ContentType: "application/json;charset=uft-8",
- Success: function (Datajson) {
- $ ("#hbKeysName"). empty (); //Clear the original data from the Keyword drop-down box
- var strnames = eval (DATAJSON.D); //Must be added. D, otherwise it cannot be resolved. Does not clear the reason, through debugging discovery exists, but the code in the smart hint does not have D
- For (var i = 0; i < strnames.length; i++) {
- $ ("#hbKeysName"). Append ($ ("<option value= '" + strnames[i]. Code + ">" + strnames[i]. Keywords + "</option>")); //This add keyword
- }
- },
- Error: function () {
- Alert ("Server error, please contact administrator!");
- }
- });
- });
- });
- </script>
- </span></span>
2. Page-related controls (using platform-encapsulated controls, normal DropDownList can also)
[HTML]View PlainCopy
- <span style="FONT-SIZE:18PX;" ><span style="FONT-FAMILY:KAITI_GB2312;FONT-SIZE:14PX;" ><TD Class="Fieldtitle">
- Keyword type
- </td>
- <TD Class="Fieldvalue">
- <soa:hbdropdownlist runat="Server" id="Hbkeywordstype" />
- </td>
- <TD Class="Fieldtitle">
- Keyword name
- </td>
- <TD Class="Fieldvalue">
- <soa:hbdropdownlist runat="Server" id="hbkeysname" />
- </td></span></span>
3. Background C # code
[CSharp]View PlainCopy
- <span style="FONT-SIZE:18PX;" ><span style="FONT-FAMILY:KAITI_GB2312;FONT-SIZE:14PX;" > [WebMethod] //Call to the front office jquery
- public static string hbkeywordstype_selectedindexchanged (string typecode)
- {
- Find a keyword entity collection based on the selected keyword type and public number encoding
- string accountcode = WxMessageAdapter.Instance.GetCurrentWxAccountCode ();
- int keywordstype =int. Parse (TypeCode);
- Wxkeywordscollection Wxkeycoll = WxKeywords_WxMessageAdapter.Instance.LoadByAccAndKeyType (AccountCode, Keywordstype);
- //Use the encapsulated JSON serialization method to return the JSON string
- string reutnjson=jsonserializerexecute.serialize (Wxkeycoll);
- return Reutnjson;
- }
- </span></span>
Precautions:
1. Front page: ①url: "This page name/method name"; ② must add ContentType: "Application/json;charset=utf-8"; ③DATAJSON.D through the. D property to fetch the data returned by the server
2. Backstage method: ① method is modified by [WebMethod]; The ② method must be a public and static method; The parameter names in the ③ method must be the same as the parameter name of data in the script, and the order can be different
Previously Datajson did not add. D, has not been taken back from the background data, through debugging found actually returned, but is the form of DATA.D, such as:
As you can see, Datajson is an object, and the Eval function can only parse the string, resulting in a value not being taken. It took a long time to do this, and although it was done in the end, it was not much consolation, but the task at work didn't wait for anyone. But, everything is difficult at the beginning, as long as you get started, all will be well.
WebForm using jquery to implement DropDownList no flush linkage