C# 更改下拉列表中的选定值
我用另一个DDL填充一个DDL,并从另一个页面获取值C# 更改下拉列表中的选定值,c#,asp.net,C#,Asp.net,我用另一个DDL填充一个DDL,并从另一个页面获取值 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DropDownList1.DataSource = ProfileMasterDAL.bindcountry(); DropDownList1.DataBind(); DropDow
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值时,都会将其固定为页面加载中的会话值,因此如何将该值更改为DDL中的选定项。使用,并将下拉列表设置为true
然后在事件处理程序的
OnSelectedIndexChanged
中,添加代码以填充第二个下拉列表。如果正确理解问题,则需要更改DropDownList 2的值
根据dropdownlist 1的值,dropdownlist的初始值来自另一个页面
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
*/
}
绑定dropdonwlist2代码
/// <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();
}
}
}
//
///绑定下拉列表2
///
///
受保护的void BindDropDownList(字符串selectedCountry)
{
ProfileMasterBLL bll=新的ProfileMasterBLL();
foreach(ProfileMasterDAL.bindcountry()中的var变量)
{
if(变量.ToString().Contains(selectedCountry))
{
var query=(ProfileMasterDAL.GetStatesByCountrys(selectedCountry));
DropDownList2.DataSource=查询;
DropDownList2.DataBind();
}
}
}
在dropdonwlist1的选定索引更改时,现在该值将更改
将DropDownList的autopostback设置为true 1
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);
}
DropDownList1.AutoPostBack=true;
///
///下拉列表1选择的索引更改
///
///
///
受保护的void DropDownList1\u SelectedIndexChanged(对象发送方,事件参数e)
{
BindDropDownList(DropDownList 1.SelectedItem.Text);
}
希望这能解决你的问题
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();
}
}
为DropDownList 1设置autopostback true并添加selectedIndexChanged
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
BindList();
}
您可能想说“下拉列表”而不是“DDL”。阅读您的问题后,我很清楚您的意思,但我对您的问题标题的最初反应是您询问的是数据定义语言。我已经这样做了,但dropdownlist.selected值被分配给pageload中的会话值,因此,即使我选择了其他国家/地区,会话值仍会保留。当我这样做时,我会收到一个错误,例如无法在DropDownList中选择多个项目。问题是,即使我在中选择了一个新值,会话的值也不会更改ddl@Chandra塞哈尔1。如果您面临异常“无法选择多个项目”,请首先清除下拉列表DropDownList1.ClearSelection()的选择;在以编程方式设置任何新值之前,请执行2。如果您想更改seesion的值,则必须在事件DropDownList1\u SelectedIndexChanged Session[“country”]=DropDownList1.SelectedValue中进行更改;