WebForm using jquery to implement DropDownList no flush linkage

Source: Internet
Author: User
Tags call back

Directory (?) [-]

    1. 1 JS Code
    2. 2 page-related controls with platform-encapsulated controls the normal DropDownList can also
    3. 3 Background C code
    4. 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 
  1. <span style="FONT-SIZE:18PX;" ><span style="FONT-FAMILY:KAITI_GB2312;FONT-SIZE:14PX;" > <script type="Text/javascript" language="JavaScript" >
  2. $ (function () {
  3. //Take advantage of DropDownList's Change event
  4. $ ("#hbKeyWordsType"). Change (function () {
  5. //Get keyword type
  6. var keyType = $ ("#hbKeyWordsType"). Val ();
  7. $.ajax ({
  8. URL: "wxkeywords_wxmessagefunoperation.aspx/hbkeywordstype_selectedindexchanged", //Call back Method: Page/Method Name
  9. Type: "POST",
  10. Data: "{typecode:" + KeyType + "}", //To the background, the parameter name must be consistent with the parameters of the method
  11. ContentType: "application/json;charset=uft-8",
  12. Success: function (Datajson) {
  13. $ ("#hbKeysName"). empty (); //Clear the original data from the Keyword drop-down box
  14. 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
  15. For (var i = 0; i < strnames.length; i++) {
  16. $ ("#hbKeysName"). Append ($ ("<option value= '" + strnames[i]. Code + ">" + strnames[i]. Keywords + "</option>")); //This add keyword
  17. }
  18. },
  19. Error: function () {
  20. Alert ("Server error, please contact administrator!");
  21. }
  22. });
  23. });
  24. });
  25. </script>
  26. </span></span>

2. Page-related controls (using platform-encapsulated controls, normal DropDownList can also)

[HTML]View PlainCopy 
  1. <span style="FONT-SIZE:18PX;" ><span style="FONT-FAMILY:KAITI_GB2312;FONT-SIZE:14PX;" ><TD Class="Fieldtitle">
  2. Keyword type
  3. </td>
  4. <TD Class="Fieldvalue">
  5. <soa:hbdropdownlist runat="Server" id="Hbkeywordstype" />
  6. </td>
  7. <TD Class="Fieldtitle">
  8. Keyword name
  9. </td>
  10. <TD Class="Fieldvalue">
  11. <soa:hbdropdownlist runat="Server" id="hbkeysname" />
  12. </td></span></span>

3. Background C # code

[CSharp]View PlainCopy 
  1. <span style="FONT-SIZE:18PX;" ><span style="FONT-FAMILY:KAITI_GB2312;FONT-SIZE:14PX;" > [WebMethod] //Call to the front office jquery
  2. public static string hbkeywordstype_selectedindexchanged (string typecode)
  3. {
  4. Find a keyword entity collection based on the selected keyword type and public number encoding
  5. string accountcode = WxMessageAdapter.Instance.GetCurrentWxAccountCode ();
  6. int keywordstype =int.  Parse (TypeCode);
  7. Wxkeywordscollection Wxkeycoll = WxKeywords_WxMessageAdapter.Instance.LoadByAccAndKeyType (AccountCode, Keywordstype);
  8. //Use the encapsulated JSON serialization method to return the JSON string
  9. string reutnjson=jsonserializerexecute.serialize (Wxkeycoll);
  10. return Reutnjson;
  11. }
  12. </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

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.