2011-01-21 77 views
0

假設我有兩個下拉列表,分別是:dropdownlistA和dropdownlistB。在頁面加載時,我將值綁定到dropdownlistA。然而,根據在dropdownlistA中選擇或顯示的值,我想將數據綁定到dropdownlistB。如何在頁面加載數據綁定下拉列表

目前,我可以綁定數據dropdownlistA好吧,我已經有所需的數據集和數據綁定數據dropdownlist。但是,dropdownlistB在頁面加載時不綁定,因爲填充數據集的條件是綁定dropdownlistB(這是dropdownlistA的值)未被選中。我怎麼能做這個工作。

我目前認爲,如果這可能工作。如果我要在不同的聲明方法中調用dropdownlistA的數據綁定,除了它在頁面加載中的綁定,並且在聲明的方法中從綁定中選擇值,是否會選擇任何值?

例如: 在頁面加載過程中,我調用了一個方法,該方法返回綁定到dropdownlistA(caseIDDropDownList)的數據集值。然後我調用另一個方法(CreateexhibitDataSet()),其中包含綁定dropdownlistB(exhibitDropDownList)的數據集值。然而,我需要在CreateExhibitDataset()方法中定義一個標準,我將使用該方法來生成綁定dropdownlistB的數據集值。如果我要在CreateExhibitDataset()方法中再次調用dropdownlistA(caseIDDropdownList)的數據綁定並在下拉列表中選擇該值,我會得到任何值嗎?

我該如何解決這個問題以便在頁面加載時綁定兩個dropdownlist?

protected void Page_Load(object sender, EventArgs e) 
    { 
     //mgrID = "M510"; 
     //mgrID = Request.QueryString["mgrID"]; 
     mgrID = (string)(Session["userID"]); 

     if (mgrID != null) 
     { 
      if (!IsPostBack) 
      { 
       CreateCasesDataset(); 
       DataView caseDataView = new DataView(caseDataSet.Tables[0]); 
       caseIDDropDownList.DataSource = caseDataView; 
       caseIDDropDownList.DataTextField = "CaseID"; 
       caseIDDropDownList.DataBind(); 

       CreateExhibitDataset(); 

       DataView exhibDataView = new DataView(exhibitDataSet.Tables[0]); 
       exhibitsDropDownList.DataSource = exhibDataView; 
       exhibitsDropDownList.DataTextField = "ExhibitID"; 
       exhibitsDropDownList.DataBind(); 
      } 
     } 
     else 
     { 
      string message = "The System couldnt identify you with any ID. Please Log in to access system functions"; 
      System.Text.StringBuilder sb = new System.Text.StringBuilder(); 
      sb.Append("<script type = 'text/javascript'>"); 
      sb.Append("window.onload=function(){"); 
      sb.Append("alert('"); 
      sb.Append(message); 
      sb.Append("')};"); 
      sb.Append("</script>"); 
      ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString()); 
     } 

    } 

這是的CreateExhibitMethod

private void CreateExhibitDataset() 
    { 
     caseIDDropDownList.DataBind(); 
     string selectedCaseID = caseIDDropDownList.SelectedValue.ToString(); 

     SqlConnection exhibitConnection = new SqlConnection(strConn); 
     exhibitSqlDataAdapter.SelectCommand = new SqlCommand("SELECT ExhibitID FROM Exhibits WHERE CaseID = '"+selectedCaseID+"'", exhibitConnection); 
     exhibitSqlDataAdapter.Fill(exhibitDataSet); 
    } 
+0

我答應接受用粗如果回答它解決了我的問題... :( – Selase 2011-01-21 15:16:09

回答

0

您還可以使用Page_LoadComplete事件,以幫助實現這一目標。它具有在加載之後但在其他頁面驅動事件之前觸發的好處。我建議將DropDownListB的綁定保存在單獨的方法中,然後如果!IsPostBackPage_LoadComplete方法調用該方法。否則,請使用@ WraithNath從SelectedIndexChanged事件調用該方法的想法。

0

額外的代碼如果你設置DropDownList的A到自動回,再用鋼絲了SelectedIndexChangedEvent。

如:

protected void DropDownListA_SelectedIndexChanged(object sender, EventArgs args) 
{ 
    //Get value from list A 
    string sValue = DropDownListA.SelectedValue.toString(); 

    //Get the data related to the selected Value in A 
    Datatable Data = DataManager.GetList(sValue); 

//Bind the list B 
DropDownListB.DataSource = Data; 
DropDownListB.DataBind(); 
} 

編輯:如果你真的要綁定在頁面加載兩個列表,你可以將所有值存儲在一個JavaScript陣列,添加和刪除根據所做的選擇值,但你必須做這一切在JavaScript

+0

@ Wra-感謝您的迴應..非常感謝。能在頁面加載這項工作嗎?我有一個類似的代碼選定的索引更改爲dropdownlistA ...怎麼這發生在回發。我正在尋找在表單加載...也回帖沒關係,但沒有任何值顯示在下拉列表中B直到我選擇dropdownlistA中的另一個值,然後selectedindexchangechanged爲dropdownlisA觸發並綁定dropdownlListB – Selase 2011-01-21 15:23:09

+0

嗨,我wouldnt綁定頁面加載兩個列表。你會在列表2中有冗餘項目,這隻會讓頁面花費更長的時間來渲染。我將在列表B中有1個列表項,表示類似'請選擇列表A中的項目'。這樣,列表幾乎爲空,直到在列表A中選擇一個項目爲止。如果將兩個列表都包含在一個asp:UpdatePanel中,則用戶甚至不會注意到回傳和列表B將加載時,A – WraithNath 2011-01-21 15:26:42

0

嘗試dropdownlistA LISTINDEX設置爲第一值(例如指數= 0),然後啓動您的自動填充例程數組listB

+0

@Hor ...選擇一個項目聽起來很吸引人...我在哪裏設置此索引?在頁面加載或當我調用方法來填充dropdownlistB的數據集? – Selase 2011-01-21 15:24:48

0

我想你不希望dropdownlistB綁定在頁面加載,因爲你不知道綁定到哪些項目。 您可以使用asp.UpdatePanel來執行此操作,但這需要您配置Ajax。 你也可以考慮jQuery的這個: 綁定change事件:在ASPX頁面

$(document).ready(function() { 
    $('#dropdownlistA').change(getdropdownlistB); 
}); 

function getdropdownlistB() { 
      var ID= $("#getdropdownlistA").attr("value"); 
dropdownlistB= $('#dropdownlistB').attr('disabled', true).children('option').remove().end().append('<option value="0">Loading...</option>'); 

      PageMethod("my.aspx/getdropdownlistBbyID", ["ID", ID], getdropdownlistBResponse) 
     } 

function PageMethod(wsurl, paramArray, onSuccess) { 
      var pagePath = window.location.pathname; 
      //Create list of parameters in the form: 
      //{"paramName1":"paramValue1","paramName2":"paramValue2"} 
      var paramList = ''; 
      if (paramArray.length > 0) { 
       for (var i = 0; i < paramArray.length; i += 2) { 
        if (paramList.length > 0) paramList += ','; 
        paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"'; 
       } 
      } 
      paramList = '{' + paramList + '}'; 
      //Call the page method 
      $.ajax({ 
       type: "POST", 
       url: wsurl, 
       contentType: "application/json; charset=utf-8", 
       data: paramList, 
       dataType: "json", 
       async: false, 
       success: onSuccess, 
       error: function(xhr, status, error) { 
        alert("error") 
       } 
      }); 
     } 

function getdropdownlistBResponse(response) { 
      var listB= (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d; 
      var dropdownlistB= $('#dropdownlistB'); 
      dropdownlistB.attr('disabled', false).children('option').remove(); 
      if (listB.length == 0) { dropdownlistB.attr('disabled', true).append('<option value="0">Select A...</option>'); } 
      for (var i = 0; i < listB.length; i++) { 
       var val = listB[i].bVal; 
       var text = listB[i].bText; 
       var def = listB[i].Default; 

       dropdownlistB.append('<option value="' + val + '">' + text + '</option>'); 
       if (def) { dropdownlistB.val(val); } //set the default 
      } 

     } 

然後你得到的數據dropdownlistB:

[WebMethod] 
     public static List<listB> getdropdownlistBbyID(string ID) 
      //called from script to get the dropdownlistB, using the selection ID from dropdownlistA 
     { 

      .. Insert code to get your data 
      List<listB> blist= new List<listB>() { }; 
      ...Insert code to fill blist with your data with rows of 3 values {ID, text, default yes/no} 


      return blist; 
     } 
相關問題