日期:2014-05-17  浏览次数:20396 次

RadioButtonList1.SelectedIndexChanged += new EventHandler(为何无效
ASPX代码
        <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True">
            <asp:ListItem>111</asp:ListItem>
            <asp:ListItem>222</asp:ListItem>
            <asp:ListItem>333</asp:ListItem>
        </asp:RadioButtonList>

CS代码
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RadioButtonList1.SelectedIndexChanged += new EventHandler(proc);
            }
        }
        protected void proc(object sender, EventArgs e)
        {
            int i = 0;
            //....
        }

为何无效,没有触发? AUTOPOSTBACK已经设置为TRUE

------解决方案--------------------
页面第一次加载的时候,你的RadioButtonList中的选项并没有发生变化,所以不会触发SelectedIndexChanged事件的。

<asp:RadioButtonList ID="RadioButtonList1" runat="server" OnSelectedIndexChanged="proc" AutoPostBack="True">
        <asp:ListItem>111</asp:ListItem>
        <asp:ListItem>222</asp:ListItem>
        <asp:ListItem>333</asp:ListItem>
</asp:RadioButtonList>

 protected void Page_Load(object sender, EventArgs e)
{
     if (!IsPostBack)
     {
          // RadioButtonList1.SelectedIndexChanged += new EventHandler(proc);
     }
 }

  protected void proc(object sender, EventArgs e)
  {
        int i = 0;
         //....
   }
------解决方案--------------------
不是注册在page_load,而是注册在Page_Init事件内:

 protected void Page_Init(object sender, EventArgs e)
    {
        RadioButtonList1.SelectedIndexChanged += new EventHandler(proc);
    }


更详细参考:
http://www.cnblogs.com/insus/archive/2013/04/25/3043604.html