WebForm使用JQuery實現DropDownList無重新整理聯動

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   os   使用   sp   java   

    原來用的微軟封裝的Ajax控制項UpdatePannel和ScriptManager去實現無重新整理聯動,但一直出現意料之外的現象導致不正常聯動。單獨的做demo就沒事,放到系統中就出問題,經過調試之後仍然不解。條條大路通羅馬,還不信能被憋死了。眾所周知,JQuery封裝了Ajax,調用起來比原生的Ajax要好用的多,實現非同步重新整理當然也更一些了,雖然功能簡單,直接看代碼:

   1.  JS代碼:

<span style="font-size:18px;"><span style="font-family:KaiTi_GB2312;font-size:14px;"><script type="text/javascript" language="javascript">        $(function () {            //利用dropdownlist的change事件            $("#hbKeyWordsType").change(function () {                //擷取關鍵字類型                var keyType = $("#hbKeyWordsType").val();                $.ajax({                    url: "WxKeywords_WxMessageFunOperation.aspx/hbKeyWordsType_SelectedIndexChanged",    //調用後台方法:頁面/方法名                    type: "post",                                        data: "{typeCode:" + keyType + "}",     //給後台傳遞資料,參數名必須跟方法的參數名一致                    contentType: "application/json;charset=uft-8",                    success: function (dataJson) {                        $("#hbKeysName").empty();           //清除關鍵字下拉框中原來存在的資料                        var strNames = eval(dataJson.d);    //必須加.d,否則無法解析。不太清除原因,通過調試發現存在,但代碼中智能提示中卻沒有d                        for (var i = 0; i < strNames.length; i++) {                            $("#hbKeysName").append($("<option value='" + strNames[i].Code + "'>" + strNames[i].Keywords + "</option>")); //這個添加關鍵字                        }                    },                    Error: function () {                        alert("伺服器錯誤,請聯絡管理員!");                    }                });            });        });           </script></span></span>
   

     2. 頁面相關控制項(用的是平台封裝的控制項,普通DropDownList也可以)    

<span style="font-size:18px;"><span style="font-family:KaiTi_GB2312;font-size:14px;"><td class="fieldtitle">    關鍵字類型</td><td class="fieldvalue">    <SOA:HBDropDownList runat="server" ID="hbKeyWordsType" /> </td><td class="fieldtitle">    關鍵字名稱</td><td class="fieldvalue">    <SOA:HBDropDownList runat="server" ID="hbKeysName" /></td></span></span>

    3. 後台C#代碼 

<span style="font-size:18px;"><span style="font-family:KaiTi_GB2312;font-size:14px;">[WebMethod]  //用於前台Jquery的調用        public static string hbKeyWordsType_SelectedIndexChanged(string typeCode)        {            //根據選取的關鍵字類型和公用號編碼尋找關鍵字實體集合            string accountCode = WxMessageAdapter.Instance.GetCurrentWxAccountCode();            int keyWordsType =int.Parse(typeCode);            WxKeywordsCollection wxKeyColl = WxKeywords_WxMessageAdapter.Instance.LoadByAccAndKeyType(accountCode, keyWordsType);    //使用封裝好的JSON序列化的方法返回Json串            string reutnJson=JSONSerializerExecute.Serialize(wxKeyColl);            return reutnJson;        }</span></span>

    注意事項:

    1. 前台頁面:  ①url:"本頁面名稱/方法名稱";  ②必須添加contentType:"application/json;charset=utf-8";  ③dataJson.d通過.d屬性來取服務端返回的資料
2. 後台方法:  ①方法由[WebMethod]修飾;   ②方法必須是公用且靜態方法;  ③方法裡的參數名稱必須與指令碼裡data的參數名一樣,順序可以不一樣

    之前dataJson沒有添加.d,一直取不到後台返回來的資料,通過調試之後發現其實已經返回來了,只不過是data.d的形式,如:

    

    從可以看出,dataJson是一個對象,而Eval函數是只能解析字串的,導致一直取不到值。做這個功能花費了很長時間,雖然最終做出來了,也沒多少欣慰,工作中的任務可是不等人的。但,萬事開頭難,只要上手了,一切都會好起來。


WebForm使用JQuery實現DropDownList無重新整理聯動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.