【发布时间】:2011-01-23 16:01:11
【问题描述】:
假设我有两个下拉列表,即:dropdownlistA 和 dropdownlistB。在页面加载时,我将值绑定到 dropdownlistA。但是,根据 dropdownlistA 中选择或显示的值,我想将数据绑定到 dropdownlistB。
目前,我可以将数据绑定到 dropdownlistA 好,并且我已经拥有所需的数据集和数据表将数据绑定到下拉列表。但是,由于未选择填充数据集以绑定 dropdownlistB(即 dropdownlistA 的值)的标准,因此 dropdownlistB 在页面加载时不会绑定。我怎样才能使这个工作。
我目前正在考虑这是否可行。如果我要在页面加载中的绑定之外的其他声明方法中调用 dropdownlistA 的数据绑定,并在声明的方法中从 bind 中选择值,是否会选择任何值?
例如: 在页面加载期间,我调用返回绑定到 dropdownlistA(caseIDDropDownList) 的数据集值的方法。然后我调用另一个方法 (CreateexhibitDataSet()),其中包含绑定 dropdownlistB(exhibitDropDownList) 的数据集值。但是,我需要在 CreateExhibitDataset() 方法中定义一个标准,我将使用它来生成数据集值以绑定 dropdownlistB。如果我在 CreateExhibitDataset() 方法中再次调用 dropdownlistA(caseIDDropdownList) 的数据绑定并在下拉列表中选择值,我会得到任何值吗?
如何解决这个问题,以便在页面加载时绑定两个下拉列表?
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);
}
【问题讨论】:
-
如果它解决了我的问题,我保证接受厚厚的答案...:(