假设我有两个下拉列表:dropdownlistA和dropdownlistB。在页面加载时,我将值绑定到dropdownlistA。但是,根据dropdownlistA中选择或显示的值,我想将数据绑定到dropdownlistB。
目前,我可以将数据绑定到dropdownlistA好了,我已经将所需的数据集和数据绑定数据绑定到下拉列表。但是,dropdownlistB在页面加载时不绑定,因为未选择填充数据集以绑定dropdownlistB(dropdownlistA的值)的条件。我怎么能做这个工作。
我目前正在考虑是否可行。如果我除了在页面加载中绑定之外,在另一个声明的方法中为dropdownlistA调用databind,并在声明的方法中选择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);
}
答案 0 :(得分:0)
如果将DropDownList A设置为AutoPostback,则连接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中完成所有操作
答案 1 :(得分:0)
尝试将dropdownlistA listindex设置为第一个值(例如index = 0),然后触发listB的自动填充例程
答案 2 :(得分:0)
您还可以使用Page_LoadComplete
事件来帮助完成此操作。它具有在Load之后但在其他页面驱动事件之前触发的优点。我建议将DropDownListB的绑定保存在单独的方法中,然后!IsPostBack
从Page_LoadComplete
方法调用该方法。否则使用@ WraithNath从SelectedIndexChanged
事件中调用该方法的想法。
答案 3 :(得分:0)
我认为您不希望dropdownlistB在页面加载时被绑定,因为您不知道要绑定哪些项目。 您可以使用asp.UpdatePanel来执行此操作,但这需要您配置Ajax。 你也可以考虑jQuery: 绑定更改事件:
$(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
}
}
然后在ASPX页面中获取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;
}