动态添加ASP.NET控件并绑定处理事件一例

 private void Page_Load(object sender, System.EventArgs e)
    {
      Control c3 = ParseControl("<asp:Button id='Button3' text='Btn3' commandname='Btn' commandargument='b3' runat='server' />");
      Control c4 = ParseControl("<asp:Button id='Button4' text='Btn4' commandname='Btn' commandargument='b4' runat='server' />");
      PlaceHolder2.Controls.Add(c3);
      PlaceHolder2.Controls.Add(c4);
      Button myBut = (Button)Page.FindControl("Button3");
      myBut.Command += new CommandEventHandler(this.OnButton);
      Button myBut2 = (Button)Page.FindControl("Button4");
      myBut2.Command += new CommandEventHandler(this.OnButton);
    }

    public void OnButton(Object Sender, CommandEventArgs e)
    {
      switch (e.CommandArgument.ToString().ToLower())
      {
        case "b0":
          Label1.Text = "Button 0";
          break;
        case "b1":
          Label1.Text = "Button 1";
          break;
        case "b3":
          Label1.Text = "Button 3";
          break;
        case "b4":
          Label1.Text = "Button 4";
          break;
      };
    }

你可能感兴趣的:(c,object,server,asp.net,asp,button)