2017-08-09 61 views
0

當我回到頁面點擊後退按鈕:如何通過JQuery/Javascript優化Listbox綁定C#代碼?

protected void TsrProc_BackButton_Click(object sender, EventArgs e) 
{ 
    Response.Redirect("~/TSP/TSR_SearchList.aspx?rtn=1&bl=1"); 
} 

我收到以下錯誤:

long running script error after sometime in IE 11

,並在Chrome:

following page has become unresponsive

回家後我我綁定5-6下拉列表(當我選擇下拉列表中的所有建築物時出現此錯誤)。

一個下拉列表中的C#代碼是這樣的:

if (Request.QueryString("rtn").Equals("1")) { 
    filterValue = Session["SEARCHWORKREQUEST"]; 
    string buildingsFilter = filterArray(1); 
    List<string> buildings = new List<string>(); 

    foreach (string buildingsitem in buildingsFilter.Split(",")) { 
     if (buildingsitem.Trim().Equals(string.Empty)) { 
      continue; 
     } 
     buildings.Add(buildingsitem); 
     if ((drpBuilding.Items.FindByValue(buildingsitem) != null)) { 
      drpBuilding.Items.FindByValue(buildingsitem).Selected = true; 
     } 
    } 
} 

// binding the building dropdown 

private void BindBuildingList() 
{ 
    this.drpBuilding.Items.Clear(); 

    List<PulseResponse.BLL.Building> lstBuildings = default(List<PulseResponse.BLL.Building>); 

    lstBuildings = this.mBuildingBLL.Get_ContactSiteBuildingsList(
     p_ContactID: UserWrapper.GetCurrentUser().ContactID, 
     p_ShowInactive: false); 

    if (lstBuildings.Count > 0) { 
     this.drpBuilding.Enabled = true; 
     this.drpBuilding.DataSource = lstBuildings; 
     this.drpBuilding.DataTextField = "Building"; 
     this.drpBuilding.DataValueField = "BuildingID"; 
     this.drpBuilding.DataBind(); 
    } else { 
     this.drpBuilding.Enabled = false; 
    } 
} 

有什麼辦法快速的jQuery/JavaScript的代碼(重新綁定的下拉列表)?

+0

您正在使用的for-each部分是無限循環的。 這就是瀏覽器無響應的原因。 你可以在你發出綁定請求的地方發佈你的代碼嗎? – HEGDE

+0

@HEGDE更新獲得,那麼所有的buidling過濾通過會話 –

+0

選擇你用結合下拉值buidling用戶 '如果(!的IsPostBack){// ...}'部分code.here我們第一次? – HEGDE

回答

1

首先,列表框將在瀏覽器

其次呈現爲

like this

,你可以調用Web方法來檢索使用jQuery AJAX從數據庫中的數據,你可以動態地添加<option>到listbox基於AJAX成功回調函數中的返回數據。

$.ajax({ 
     type: "Post", 
     url: "page/yourmethodname", 
     data: "{'Key':'value'}",//if any otherwise leave 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (data) { 
      //add new options to ListBox dynamically 
    //use for loop to iterate through kay-val pair and bind 
for(var i=0i<data.length;i++){ 
var newoption = '<option value='+data[i].key+'>'+data[i].value+'</option>'; 
      $("#ListBox1").append(newoption); 
} 

     }, 
     error: function (err) { 
      alert(err); 
     } 
    }) 
+0

我的列表框runat服務器,並通過此代碼,我將無法獲得價值服務器端 –

+0

您可以得到值, 而不是'$(「#ListBox1」)'要麼使用'($(「#< %= ListBox1.ClientID%>「)' 或者你可以去一個類'.ListBox1'獲取價值在jquery – HEGDE