C# ASP.Net中未从静态类触发动态按钮

C# ASP.Net中未从静态类触发动态按钮,c#,asp.net,dynamic,C#,Asp.net,Dynamic,我正在尝试创建一个动态表,其中有几行,最后一行是加号按钮,它将添加一个新行。(为了简单起见,我只描述重要信息。) 我想到了这个方法来实现它: // .aspx code <li><asp:LinkButton ID="LM_Show" runat="server" Text="Show list" OnClick="Action"/></li> <asp:PlaceHolder ID="infoTable" runat="server"><

我正在尝试创建一个动态表,其中有几行,最后一行是加号按钮,它将添加一个新行。(为了简单起见,我只描述重要信息。)

我想到了这个方法来实现它:

// .aspx code 
<li><asp:LinkButton ID="LM_Show" runat="server" Text="Show list" OnClick="Action"/></li>
<asp:PlaceHolder ID="infoTable" runat="server"></asp:PlaceHolder>

//CreateTable function
public void Clicked(object sender, EventArgs e){
    table();
}
public void table() {
    //Do stuff...

    //Screen variable is keeping track of which screen should be shown in .aspx
    infoTable.Controls.Add(CreateView.createTable<Employee>(screen, this.Context, table));
}

//Create the actual table 
public static Table createTable<T>(Screen screen, HttpContext context, Action method) where T : new() {
    //New table and make it stretch
    Table tb = new Table();
    tb.Width = Unit.Percentage(100);
    tb.Height = Unit.Percentage(100);

    //Gather list from session
    List<T> items = (List<T>)context.Session["list"];

    //Create table content based on the list
    for (int i = 1; i <= items.Count(); i++) {
        TableRow tr = new TableRow();

       //Foreach property create cells with content according to the property
       //Add these cells to the row

        tb.Rows.Add(tr);
    }

    //Create 1 final row which has a button to be able to add 1 row
    TableRow tr2 = new TableRow();
    TableCell tc = new TableCell();
    tr.Cells.Add(tc);

    //Create the Button
    Button button = new Button();
    button.Text = "+";

    //!!This is not getting triggered!!//
    button.Click += (s, e) => { 
        List<T> tempItems = (List<T>)context.Session["list"]; 
        tempItems.Add(new T()); 
        context.Session["list"] = tempItems; 
        //When a button is pressed, it gives a postback.
        //The table has to be rebuild over again with the newly added item
        method(); 
    };
    //!!This is not getting triggered!!//

    //Add the button
    tr2.Cells[0].Controls.Add(button);
    tb.Rows.Add(tr2);

    return tb;
}
/.aspx代码
  • //CreateTable函数 已单击公共作废(对象发送者、事件参数){ 表(); } 公共空表(){ //做些事情。。。 //Screen变量用于跟踪.aspx中应显示的屏幕 infoTable.Controls.Add(CreateView.createTable(screen,this.Context,table)); } //创建实际的表 公共静态表createTable(屏幕、HttpContext上下文、操作方法),其中T:new(){ //新桌子,让它舒展一下 Table tb=新表(); tb.宽度=单位百分比(100); tb.高度=单位百分比(100); //从会话中收集列表 列表项=(列表)上下文。会话[“列表”]; //基于列表创建表内容 对于(int i=1;i{ List tempItems=(List)context.Session[“List”]; 添加(新的T()); context.Session[“list”]=tempItems; //当按下一个按钮时,它会发出回发。 //必须使用新添加的项重新生成表 方法(); }; //!!这不会被触发// //添加按钮 tr2.Cells[0]。Controls.Add(按钮); tb.Rows.Add(tr2); 返回结核病; }

    关于代码或如何更好地完成代码的任何评论都是非常受欢迎的。

    如果使用jQuery AJAX并与web服务/方法通信以获取数据,则可以更轻松地实现这一点。但是,也可以用C语言实现。假设下面给出的代码部分不起作用

    //Add the button
    tr2.Cells[0].Controls.Add(button);
    tb.Rows.Add(tr2);
    
    您可以先尝试将
    按钮添加到
    tc
    ,然后将
    tc
    添加到行
    tr

    tc.Controls.Add(button);
    tr2.Cells.Add(tc);
    tb.Rows.Add(tr2);
    

    祝您好运!

    如果您使用jQuery AJAX并与web服务/方法通信以获取数据,则可以更轻松地实现这一点。但是,它也可以用C#实现。假设下面给出的代码部分不起作用

    //Add the button
    tr2.Cells[0].Controls.Add(button);
    tb.Rows.Add(tr2);
    
    您可以先尝试将
    按钮添加到
    tc
    ,然后将
    tc
    添加到行
    tr

    tc.Controls.Add(button);
    tr2.Cells.Add(tc);
    tb.Rows.Add(tr2);
    

    祝你好运!

    谢谢你的帮助!但是按钮正在显示。这不是问题所在。它不会触发我传递的方法!-(你是指重新绘制表的方法吗?
    method()
    另外,您是如何添加新项目的?当您单击按钮时,会发生什么情况?是在您通过键入内容添加项目的位置添加新行,还是向用户提供一个弹出窗口,用户可以在其中添加新项目?很抱歉,我的错误,尝试将其缩短。编辑了函数名。现在您应该看到我正在尝试添加的内容了l、 谢谢您的帮助!但是按钮正在显示。这不是问题所在。它只是不会触发我传递的方法:-(您是指重新绘制表的方法吗?
    method()
    另外,您是如何添加新项目的?当您单击按钮时,会发生什么情况?是在您通过键入内容添加项目的位置添加新行,还是向用户提供一个弹出窗口,用户可以在其中添加新项目?很抱歉,我的错误,尝试将其缩短。编辑了函数名。现在您应该看到我正在尝试添加的内容了L