首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

RadioButtonList1.SelectedIndexChanged += new EventHandler(为什么无效

2013-05-02 
RadioButtonList1.SelectedIndexChanged + new EventHandler(为何无效ASPX代码asp:RadioButtonList ID

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

热点排行