C# 如果在页面加载事件中更改索引,则不会触发下拉列表SelectedIndexChanged事件
我有一个下拉列表&一个标签,下拉列表是用字典装订的。当用户更改下拉列表的选定值时,我想更新标签。下面的代码运行良好,但我想设置label的初始值,我在page_load中设置所选索引的值,但是事件不会触发。如何修复它?是否有任何页面事件可以帮助我解决问题。我知道我可以使用javascript修复它,但我不想使用JSC# 如果在页面加载事件中更改索引,则不会触发下拉列表SelectedIndexChanged事件,c#,asp.net,C#,Asp.net,我有一个下拉列表&一个标签,下拉列表是用字典装订的。当用户更改下拉列表的选定值时,我想更新标签。下面的代码运行良好,但我想设置label的初始值,我在page_load中设置所选索引的值,但是事件不会触发。如何修复它?是否有任何页面事件可以帮助我解决问题。我知道我可以使用javascript修复它,但我不想使用JS public partial class WebForm1 : System.Web.UI.Page { Dictionary<
public partial class WebForm1 : System.Web.UI.Page
{
Dictionary<string, string> myDictionary = new Dictionary<string, string>();
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
myDictionary.Add("1", "Test Address 1");
myDictionary.Add("2", "Test Address 2");
myDictionary.Add("3", "Test Address 3");
myDictionary.Add("4", "Test Address 4");
myDictionary.Add("5", "Test Address 5");
drpTest.DataSource = myDictionary;
drpTest.DataTextField = "Key";
drpTest.DataValueField = "Value";
drpTest.DataBind();
// I want to set the index & update the label lblAddress
drpTest.SelectedIndex = 2;
}
}
protected void drpTest_SelectedIndexChanged(object sender, EventArgs e)
{
lblAddress.Text = drpTest.SelectedItem.Value;
}
public分部类WebForm1:System.Web.UI.Page
{
Dictionary myDictionary=新字典();
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!this.IsPostBack)
{
添加(“1”,“测试地址1”);
添加(“2”,“测试地址2”);
添加(“3”,“测试地址3”);
添加(“4”,“测试地址4”);
添加(“5”,“测试地址5”);
drpTest.DataSource=myDictionary;
drpTest.DataTextField=“Key”;
drpTest.DataValueField=“Value”;
drpTest.DataBind();
//我想设置索引并更新标签lblAddress
drpTest.SelectedIndex=2;
}
}
受保护的无效drpTest\u SelectedIndexChanged(对象发送方,事件参数e)
{
lblAddress.Text=drpTest.SelectedItem.Value;
}
更改pageload上的标签文本。请参见下文
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
myDictionary.Add("1", "Test Address 1");
myDictionary.Add("2", "Test Address 2");
myDictionary.Add("3", "Test Address 3");
myDictionary.Add("4", "Test Address 4");
myDictionary.Add("5", "Test Address 5");
drpTest.DataSource = myDictionary;
drpTest.DataTextField = "Key";
drpTest.DataValueField = "Value";
drpTest.DataBind();
drpTest.SelectedIndex = 2;
lblAddress.Text = drpTest.SelectedItem.Value; **// add this**
}
}
希望它对您有用。更改页面加载上的标签文本。请参阅下文
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
myDictionary.Add("1", "Test Address 1");
myDictionary.Add("2", "Test Address 2");
myDictionary.Add("3", "Test Address 3");
myDictionary.Add("4", "Test Address 4");
myDictionary.Add("5", "Test Address 5");
drpTest.DataSource = myDictionary;
drpTest.DataTextField = "Key";
drpTest.DataValueField = "Value";
drpTest.DataBind();
drpTest.SelectedIndex = 2;
lblAddress.Text = drpTest.SelectedItem.Value; **// add this**
}
}
希望它对您有用。您应该在页面加载时调用此函数
drpTest_SelectedIndexChanged(null, null)
它不会像正常情况一样触发,因为在初始化页面并准备好用于客户端后,您没有更改下拉选择值您应该在页面加载时调用此函数
drpTest_SelectedIndexChanged(null, null)
并且它不会像正常情况一样触发,因为在初始化页面并准备好用于客户端时,您没有更改下拉列表的选定值。当您定义下拉列表include drpTest.AutoPostBack=true时
这将在更改时触发SelectedIndexChanged事件。当您定义下拉列表include drpTest.AutoPostBack=true时
这将在更改时触发SelectedIndexChanged事件。Set下拉列表的AutoPostBack=“true”Autopostback已设置为“true”。当我从浏览器更改所选值时,它工作正常。但是我想设置标签的初始值。通过标签的初始值,您是指第一次加载页面时吗?设置下拉列表的Autopostback=“true”Autopostback已设置为“true”。当我从浏览器更改所选值时,它工作正常。但是我想设置标签的初始值。通过标签的初始值,您是指第一次加载页面时吗?我的问题是,如果我通过更改值将其值设置为2,为什么所选索引不会触发,您不能调用所选索引ge方法。更改selectedindex后必须手动调用它。如drpTest\u SelectedIndexChanged(drpTest,EventArgs.empty);我的问题是,如果我通过更改值将其值设置为2,则无法调用选定索引更改方法。更改selectedindex后,必须手动调用它。如drpTest_SelectedIndexChanged(drpTest,EventArgs.empty);这对我有效,但这是正确的方法吗?我在想,如果有任何页面/控件事件,我可以利用它来设置初始值抱歉,但这不存在,下拉列表默认情况下会将列表中的第一个项设置为第一个选定项,因此您必须通过任何编辑来覆盖此默认值。这对我有效,但这是吗正确的方法是,我想如果有任何页面/控件事件,我可以利用它来设置初始值抱歉,但这不存在,下拉列表默认情况下会将列表中的第一个项目设置为第一个选定的项目,因此您必须通过所需的任何编辑来覆盖此默认值