循环动态添加的事件点击2次只执行一次

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ViewState["textname"] = "";
            ViewState["sum"] = 0;
        }
        if (Convert.ToInt32(ViewState["sum"].ToString()) > 1)
        {
            SeachData(Convert.ToInt32(ViewState["sum"].ToString()));
        }
    }
  
    private void SeachData(int sum)
    {
        this.PlaceHolder1.Controls.Clear();
        double index = sum / 10;
        if (index > 10)
        {
            for (int i = 1; i <= 10; i++)
            {
                LinkButton lb = new LinkButton();
                lb.Text = String.Format("{0}", i);
                lb.Click += new EventHandler(lb_Click);
                this.PlaceHolder1.Controls.Add(lb);
            }
        }
        else
        {
            for (int i =1; i <= index; i++)
            {
                LinkButton lb = new LinkButton();
                lb.Text = String.Format("{0}",i);
                lb.Click+=new EventHandler(lb_Click);
                this.PlaceHolder1.Controls.Add(lb);
            }
        }
    }
      
    #region 点击事件
    void lb_Click(object sender, EventArgs e)
    {
        LinkButton lb = (LinkButton)sender;
        string str = lb.Text;
        int index = Convert.ToInt32(lb.Text);//获取当前点击的页数
     }
    #endregion
千巷猫影
浏览 640回答 1
1回答

婷婷同学_

if (Convert.ToInt32(ViewState["sum"].ToString()) > 1) { SeachData(Convert.ToInt32(ViewState["sum"].ToString())); } 这里事件被重新绑定了
打开App,查看更多内容
随时随地看视频慕课网APP