更改下拉列表中选择值
我填充另一个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中选定的项目。
使用OnSelectedIndexChanged
event以及将下拉列表中的AutoPostBack
property设置为true。
而在OnSelectedIndexChanged
事件处理程序中,添加代码以填充第二个下拉列表。
我已经这样做了,但dropdownlist.selected的值被分配给pageload中的会话值,所以即使我选择了其他一些国家的会话值仍然存在。 –
如果理解了题正确,你想改变取决于DropDownList1的价值DropDownList2 的价值,下拉列表的初值来自另一个页面
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
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);
}
希望这将解决问题乌尔
当我这样做时,我得到一个错误,如 不能有一个DropDownList中选择多个项目。 –
问题是即使我在ddl中选择了一个新值,会话的值也没有发生变化 –
@Chandra sekhar 1.如果您面临异常“无法选择多个项目”,请先清除下拉菜单 \t DropDownList1 。清空选项(); 以编程方式设置任何新值之前 2.如果您希望更改seesion的值,则您必须在事件中执行DropDownList1_SelectedIndexChanged Session [“country”] = DropDownList1.SelectedValue; – Prateek
你可能想说“下拉列表”,而不是“DDL”。在阅读你的问题之后,你的意思很清楚,但是我对你的问题*标题*的最初反应是你提到了数据定义语言。 –