简体   繁体   English

如果在page_load事件中更改了索引,则不会触发dropdown SelectedIndexChanged事件

[英]dropdown SelectedIndexChanged event is not triggering if index is changed in page_load event

I have a drop down & a label, drop down is binded with a dictionary. 我有一个下拉菜单和一个标签,下拉菜单与字典绑定在一起。 When user changes the selected value of drop down I want to update the label. 当用户更改下拉列表的选定值时,我想更新标签。 Following code works well but i want to set the initial value of label, I set the value of selected index in page_load, however event does not trigger. 下面的代码很好用,但是我想设置标签的初始值,我在page_load中设置选定索引的值,但是事件不会触发。 How to fix it? 如何解决? is there any page event which can help me solve the issue. 是否有任何页面事件可以帮助我解决问题。 I know i can fix it using javascript but I do not want to use JS. 我知道我可以使用JavaScript修复它,但我不想使用JS。

 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;
            }

change label text on pageload. 更改页面加载上的标签文本。 See below. 见下文。

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**
     }
}

Hope it works for you. 希望对你有效。

you should call this function at page load 您应该在页面加载时调用此函数

drpTest_SelectedIndexChanged(null, null)

and it doesn't trigger as normal because you didn't change the dropdown selected value after initializing the page and ready to be used for client 并且它不会正常触发,因为在初始化页面并准备好用于客户端之后您没有更改下拉列表的选定值

当您定义下拉列表时,请包括drpTest.AutoPostBack = true这将在更改后触发SelectedIndexChanged事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM