假設我有兩個下拉列表,分別是: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);
}
我答應接受用粗如果回答它解決了我的問題... :( – Selase 2011-01-21 15:16:09