从codebehind按钮:添加ASP [英] Add asp:Button from codebehind

查看:132
本文介绍了从codebehind按钮:添加ASP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在codebehind建设。该表是一个数据库记录的列表(每行一个记录),我neeed添加一个删除按钮的每一行。要做到这一点,我当然需要建立一个按钮,为一个唯一的ID。要做到这一点,我想出了下面的... ...这是行不通的。关于如何得到这个工作有什么建议?

 按钮deleteButton =新按钮();
deleteButton.ID =deleteStudentWithID+ singleStudent.ID.ToString();
deleteButton.Text =X;字串行=< TR>中;
排+ =< TD类= \\青青草地\\>中+ deleteButton.ClientID +< / TD>中;
排+ =< / TR>中;


解决方案

您的问题是,您要添加仅客户端ID 你的控制到HTML,并且不添加控制页面本身。

  Controls.Add被(新LiteralControl(<表>));的foreach(学生VAR singleStudent)
{
    Controls.Add被(新LiteralControl(&所述; TR>中));    // code以添加其他列    按钮deleteButton =新按钮();
    deleteButton.ID =deleteStudentWithID+ singleStudent.ID.ToString();
    deleteButton.Text =X;    Controls.Add被(新LiteralControl(&下; TD类= \\青青草地\\>中));
    Controls.Add被(deleteButton);
    Controls.Add被(新LiteralControl(&下; / TD>&下; / TR>中);
}Controls.Add被(新LiteralControl(< /表>));

I'm building a in codebehind. The table is a listing of a database records (one record per row) and I neeed to add a delete button for each row. To do that, I of course need to build a button with a unique ID for that. To do that, I came up with the following ... which doesn't work. Any tips on how to get this working?

Button deleteButton = new Button();
deleteButton.ID = "deleteStudentWithID" + singleStudent.ID.ToString();
deleteButton.Text = "X";

string row = "<tr>";
row += "<td class=\"style5\">"+deleteButton.ClientID +"</td>";
row += "</tr>";

解决方案

Your problem is that you're adding only the ClientID of your control to the html and not adding the control to the page itself.

Controls.Add(new LiteralControl("<table>"));

foreach(var singleStudent in students)
{
    Controls.Add(new LiteralControl("<tr>"));

    //Code to add other columns

    Button deleteButton = new Button();
    deleteButton.ID = "deleteStudentWithID" + singleStudent.ID.ToString();
    deleteButton.Text = "X";

    Controls.Add(new LiteralControl("<td class=\"style5\">"));
    Controls.Add(deleteButton);
    Controls.Add(new LiteralControl("</td></tr>");
}

Controls.Add(new LiteralControl("</table>"));

这篇关于从codebehind按钮:添加ASP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆