码迷,mamicode.com
首页 > 其他好文 > 详细

DropDownList 控件的SelectedIndexChanged事件触发不了

时间:2014-08-06 18:09:21      阅读:339      评论:0      收藏:0      [点我收藏+]

标签:blog   http   使用   os   strong   for   art   ar   

先看看网友的问题:
bubuko.com,布布扣



根据Asp.NET的机制,在html markup有写DropDownList控件与动态加载的控件有点不一样。如果把DropDownList控件写在html markup,即.aspx网页,网友的代码是没有问题的。但是,如果是在.aspx.cs动态加载的控件,那就不能使用Page_Load事件内加载,如果以此加载,就算是使用AutoPostBack = true也没有效果。
正确做法是在Page_Init事件中加载


下面动态实时操作中,首先是运行网友的代码,确定无法运行。然后Insus.NET把Page_Load事件以及Showpage方法Comment out之后,并把动态产生DropDownList控件拷贝至page_Init事件。最后奇迹发生,能正确运行了。
bubuko.com,布布扣


演示中的代码:

bubuko.com,布布扣
bubuko.com,布布扣
protected void Page_Init(object sender, EventArgs e)
    {
        DropDownList ddl = new DropDownList();
        ddl.AutoPostBack = true;
        ddl.Items.Add(new ListItem("国家", "c"));
        ddl.Items.Add(new ListItem("中國", "c1"));
        ddl.Items.Add(new ListItem("美國", "c2"));
        ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
        this.Form.Controls.Add(ddl); 
    }
    
    
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!IsPostBack)
        //{
        //    ShowPage();
        //}
    }

    //public void ShowPage()
    //{
    //    DropDownList ddl = new DropDownList();
    //    ddl.AutoPostBack = true;
    //    ddl.Items.Add(new ListItem("国家", "c"));
    //    ddl.Items.Add(new ListItem("中國", "c1"));
    //    ddl.Items.Add(new ListItem("美國", "c2"));
    //    ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
    //    this.Form.Controls.Add(ddl);
    //}
    protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList dl = (DropDownList)sender;
        Response.Write("<script>alert(‘" + dl.SelectedValue + "‘)</script>");
    }
bubuko.com,布布扣


 

protected void Page_Init(object sender, EventArgs e)
    {
        DropDownList ddl = new DropDownList();
        ddl.AutoPostBack = true;
        ddl.Items.Add(new ListItem("国家", "c"));
        ddl.Items.Add(new ListItem("中國", "c1"));
        ddl.Items.Add(new ListItem("美國", "c2"));
        ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
        this.Form.Controls.Add(ddl); 
    }
    
    
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!IsPostBack)
        //{
        //    ShowPage();
        //}
    }

    //public void ShowPage()
    //{
    //    DropDownList ddl = new DropDownList();
    //    ddl.AutoPostBack = true;
    //    ddl.Items.Add(new ListItem("国家", "c"));
    //    ddl.Items.Add(new ListItem("中國", "c1"));
    //    ddl.Items.Add(new ListItem("美國", "c2"));
    //    ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
    //    this.Form.Controls.Add(ddl);
    //}
    protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList dl = (DropDownList)sender;
        Response.Write("<script>alert(‘" + dl.SelectedValue + "‘)</script>");
    }

DropDownList 控件的SelectedIndexChanged事件触发不了,布布扣,bubuko.com

DropDownList 控件的SelectedIndexChanged事件触发不了

标签:blog   http   使用   os   strong   for   art   ar   

原文地址:http://www.cnblogs.com/lschenblog/p/3894716.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!