我用另一个DDL填充DDL,我从另一个页面获取值
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.DataSource = ProfileMasterDAL.bindcountry();
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, "--Select country--");
}
if(Session["uname"]!=null)
{
DropDownList1.SelectedValue = Session["country"].ToString();
ProfileMasterBLL bll=new ProfileMasterBLL();
foreach (var VARIABLE in ProfileMasterDAL.bindcountry())
{
if (VARIABLE.ToString().Contains(DropDownList1.SelectedItem.Text))
{
var query = (ProfileMasterDAL.GetStatesByCountrys(DropDownList1.SelectedItem.Text));
DropDownList2.DataSource = query;
DropDownList2.DataBind();
}
}
TextBox8.Text = Session["email"].ToString();
string pwd = Session["pwd"].ToString();
TextBox9.Attributes.Add("value",pwd);
TextBox10.Attributes.Add("value", pwd);
}
}
但问题是每当我更改DDL值固定为会话值时,因为它在page_load中,所以如何将值更改为DDL中的选定项目。
答案 0 :(得分:0)
使用OnSelectedIndexChanged
event以及为下拉列表将AutoPostBack
property设置为true。
在OnSelectedIndexChanged
事件处理程序中,添加代码以填充第二个下拉列表。
答案 1 :(得分:0)
如果已经理解了问题的正确性,那么您想要更改DropDownList2的值 根据DropDownList1的值,下拉列表的初始值来自另一个页面
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.DataSource = ProfileMasterDAL.bindcountry();
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, "--Select country--");
//get the selected country from another page
string selectedCountry = Convert.ToString(Session["country"]);
//set the selected value
DropDownList1.Items.FindByValue(selectedCountry).Selected = true;
//Bind Dropdonwlist2
BindDropDownList(DropDownList1.SelectedItem.Text);
}
/*
remaining code
*/
}
绑定dropdonwList 2代码
/// <summary>
/// Bind dropdownlist2
/// </summary>
/// <param name="selectedCountry"></param>
protected void BindDropDownList(string selectedCountry)
{
ProfileMasterBLL bll = new ProfileMasterBLL();
foreach (var VARIABLE in ProfileMasterDAL.bindcountry())
{
if (VARIABLE.ToString().Contains(selectedCountry))
{
var query = (ProfileMasterDAL.GetStatesByCountrys(selectedCountry));
DropDownList2.DataSource = query;
DropDownList2.DataBind();
}
}
}
关于dropdonwlist1的选定索引更改,现在值将更改
为dropdownlist1设置autopostback为true
DropDownList1.AutoPostBack = true;
/// <summary>
/// DropDownList1 Selcted Index change
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
BindDropDownList(DropDownList1.SelectedItem.Text);
}
希望这能解决你的问题
答案 2 :(得分:0)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.DataSource = ProfileMasterDAL.bindcountry();
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, "--Select country--");
if(Session["uname"]!=null)
{
DropDownList1.SelectedValue = Session["country"].ToString();
BindList()
}
}
if(Session["uname"]!=null)
{
TextBox8.Text = Session["email"].ToString();
string pwd = Session["pwd"].ToString();
TextBox9.Attributes.Add("value",pwd);
TextBox10.Attributes.Add("value", pwd);
}
}
添加绑定ddl2的方法
private void BindList()
{
ProfileMasterBLL bll=new ProfileMasterBLL();
foreach (var VARIABLE in ProfileMasterDAL.bindcountry())
{
if (VARIABLE.ToString().Contains(DropDownList1.SelectedItem.Text))
{
var query =
ProfileMasterDAL.GetStatesByCountrys(DropDownList1.SelectedItem.Text));
DropDownList2.DataSource = query;
DropDownList2.DataBind();
}
}
为dropdownlist1设置autopostback true,并添加selectedIndexChanged
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
BindList();
}