如何在页面加载数据绑定下拉列表

问题描述:

假设我有两个下拉列表,分别是: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

您还可以使用Page_LoadComplete事件,以帮助实现这一目标。它具有在加载之后但在其他页面驱动事件之前触发的好处。我建议将DropDownListB的绑定保存在单独的方法中,然后如果!IsPostBackPage_LoadComplete方法调用该方法。否则,请使用@ WraithNath从SelectedIndexChanged事件调用该方法的想法。

额外的代码如果你设置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

尝试dropdownlistA LISTINDEX设置为第一值(例如指数= 0),然后启动您的自动填充例程数组listB

+0

@Hor ...选择一个项目听起来很吸引人...我在哪里设置此索引?在页面加载或当我调用方法来填充dropdownlistB的数据集? – Selase 2011-01-21 15:24:48

我想你不希望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; 
     }