C# 按钮是在代码隐藏中使用stringbuilder创建的。如何让它们调用函数?
我在stringbuilder中编写了一个表,它根据数据库中的记录数向表中添加行。每行应该有三个按钮,用于调用代码隐藏中的不同函数。 这里有一个例子来说明我的意思:C# 按钮是在代码隐藏中使用stringbuilder创建的。如何让它们调用函数?,c#,html,asp.net,code-behind,stringbuilder,C#,Html,Asp.net,Code Behind,Stringbuilder,我在stringbuilder中编写了一个表,它根据数据库中的记录数向表中添加行。每行应该有三个按钮,用于调用代码隐藏中的不同函数。 这里有一个例子来说明我的意思: using (superCommand) { using (SqlDataReader employeeReader = superCommand.ExecuteReader()) { while (employeeReader.Read()) { string employee = employee
using (superCommand)
{
using (SqlDataReader employeeReader = superCommand.ExecuteReader())
{
while (employeeReader.Read())
{
string employee = employeeReader[0].ToString();
tString.Append(
"<tr style=\"padding: 15px;\">" +
"<td style=\"text-align: left; width:250px;\">" +
employee +
"</td>" +
"<td style=\"text-align: center;\">" +
employeeReader[1] +
"</td>" +
"<td style=\"text-align: center;\">" +
employeeReader[2] +
"</td>" +
"<td style=\"text-align: center;\">" +
"<button type=\"button\" >Print</button>" +
"</td>" +
"<td style=\"text-align: center;\">" +
"<button type=\"button\">Close And Lock</button>" +
"</td>" +
"<td style=\"text-align: center;\">" +
"<button runat=\"server\" onclientclick=\"viewEmployee(\"" + employee + "\")\">View</button>" +
"</td>" +
"</tr>"
);
}
}
}
onclientclick也应该调用在代码背后编写的函数。我知道我应该用另一种方法来写这篇文章,但我想试着让它起作用。您知道如何启动这些按钮吗?您不能在ASP.NET Web表单中将按钮呈现为html字符串。由于它们不在控制树中,因此无法附加到服务器端单击事件
最简单的方法是使用服务器端数据控件,如GridView、ListView或Repeater
下面是GridView示例-
代码隐藏
你真的应该使用一个转发器来实现这一点,但是如果你想让上面的方法发挥作用,可以通过调用u doPostBack来查找——你可以在你的字符串构建中的每个按钮上添加一个JS click事件,将发送者和参数传递回PageLoad;在PageLoad中,查看_EVENTARGUMENT Request.Form变量以查看单击了哪个按钮。如果你在谷歌上搜索doPostBack和EVENTARGUMENT,有很多这样做的例子。但是正如我所说的,这种方法并不好,顺便说一句,常规的HTML按钮没有onclientclick事件;这是一个webforms事件,它会生成客户端单击事件。@sh1rts这在以下链接的帮助下起作用:@sh1rts感谢您为我指明了正确的方向。@TroutIn2D不要在ASP.NET Web表单中使用_doPostBack手动发布按钮。它非常脆弱,很难长期维护。如果要触发服务器端事件,应该尽可能多地使用服务器控件。
<asp:GridView ID="EmployeeGridView" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" ID="PrintButton"
Text="Print"
OnCommand="PrintButton_Command"
CommandArgument='<%# Eval("Id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" ID="CloseButton"
Text="Close And Lock"
OnCommand="CloseButton_Command"
CommandArgument='<%# Eval("Id") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
/*string connectionString = ConfigurationManager
.ConnectionStrings["SqlSqlConnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT * FROM Employees";
cmd.Connection = con;
con.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
}
}*/
// Since I do not have database, I've to manually populate the data here.
EmployeeGridView.DataSource = new List<Employee>
{
new Employee {Id = 1, FirstName = "John", LastName = "Doe"},
new Employee {Id = 2, FirstName = "Janet", LastName = "Doe"},
new Employee {Id = 3, FirstName = "Eric", LastName = "Newton"}
};
EmployeeGridView.DataBind();
}
}
protected void PrintButton_Command(object sender, CommandEventArgs e)
{
var id = e.CommandArgument;
}
protected void CloseButton_Command(object sender, CommandEventArgs e)
{
var id = e.CommandArgument;
}
}
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}