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

动态生成Button,事件该如何写.

2013-04-07 
动态生成Button,事件该怎么写..有一个根据数据多少动态生成的表格,表格中有一列(联系电话)是初次加载时不

动态生成Button,事件该怎么写..
有一个根据数据多少动态生成的表格,表格中有一列(联系电话)是初次加载时不显示的..

在该列中有一个Button,点击显示联系电话..(Button也是随表格动态添加的.)


现在遇到的问题是:添加的Button按钮添加事件后点击每次都会刷新页面,刷新后动态生成表格不见了..

下面是代码://动态添加第六列..
            HtmlTableCell htr1_c6 = new HtmlTableCell();

            Panel panel1 = new Panel();
            panel1.Visible = true;
            Button bt_01 = new Button();
            bt_01.Command += new CommandEventHandler(this.bt_01_Click);
            bt_01.Text = "显示";
            bt_01.CommandName = "bt_01_Click";
            bt_01.CommandArgument = i.ToString();
            panel1.Controls.Add(bt_01);

            Panel panel2 = new Panel();
            panel2.Visible = false;
            Label lb1 = new Label();
            lb1.Text = ds.Tables[0].Rows[i]["LXDH"].ToString().Trim();
            Button bt_02 = new Button();
            bt_02.Command += new CommandEventHandler(this.bt_02_Click);
            bt_02.Text = "隐藏";
            bt_02.CommandArgument = i.ToString();
            panel2.Controls.Add(lb1);
            panel2.Controls.Add(bt_02);

            htr1_c6.Controls.Add(panel1);
            htr1_c6.Controls.Add(panel2);

            htr1.Controls.Add(htr1_c6);

            ilist.Rows.Add(htr1);

    protected void bt_01_Click(object sender, EventArgs e)
    {
        //获得传递的参数:按钮所在的表格的行数..
        int i = Convert.ToInt32(((Button)sender).CommandArgument);
        Response.Write("aaaaaaaaaaa");//用这个测试也没有aaaaaaaaaaa输出在网页上..
        ((Panel)ilist.Rows[i].Cells[5].Controls[0]).Visible = false;
        ((Panel)ilist.Rows[i].Cells[5].Controls[1]).Visible = true;
    }
    protected void bt_02_Click(object sender, EventArgs e)
    {
        //获得传递的参数:按钮所在的表格的行数..
        int i = Convert.ToInt32(((Button)sender).CommandArgument);


        ((Panel)ilist.Rows[i].Cells[5].Controls[0]).Visible = true;
        ((Panel)ilist.Rows[i].Cells[5].Controls[1]).Visible = false;
    } asp.net 动态生成Button的问题 数据显示 求大神
[解决办法]
用个repeater,做下绑定,不就很方便吗?


在repeater内,直接放个<input type="button" id="button1" value="查看联系电话" onclick="在这里写段js代码" />

这样岂不更便捷.
[解决办法]
应该用客户端的JS来作这种在客户端的显示开关功能。如果用服务器控件,自然容易出现这种现象,除非你把在客户端动态操作的行为提交给服务器进行处理,那就没必要地麻烦了。用客户端JS做这个是很容易的,如果你们有专门负责前台JS的,让他做就是了。

热点排行