工作日志  
记录工作,学习中的点点滴滴
问题:商城,商品自定义属性,需要在页面上动态产生按钮及关联到方法并传递参数
先写了如下方法
protected void Page_Load(object sender, EventArgs e)
    {
        Button LB = new Button();
        LB.Text = "测试";
        //DIV1是页面上一个DIV
        DIV1.Controls.Add(LB);
        LB.Click += new EventHandler(Test);
    }
    protected void Test(object sender, EventArgs e)
    {
        Response.Write("<script>alert('This is a test');</script>");
    }
但上面的方法没有传传递参数,只能调用一个方法,当然用极端方法可以把要传的参数赋给按钮的ID或什么属性的带过去,但不是最好的方法,于是又找到如下方法:

    protected void Page_Load(object sender, EventArgs e)
    {
            Button LB = new Button();
            LB.Text = "测试";
            LB.CommandName = "1";
            LB.CommandArgument = "5";
            DIV1.Controls.Add(LB);
            LB.Command += new CommandEventHandler(this.Test);
    }
    protected void Test(object sender, CommandEventArgs e)
    {
        Response.Write("<script>alert('"+e.CommandArgument+"|"+e.CommandName+"');</script>");
    }
这样就可以通过按钮的CommandName 和CommandArgument 进行参数传递了。

posted on 2007-06-09 11:16  风的影  阅读(894)  评论(1编辑  收藏  举报