Default.aspx的:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html>
<head>
<title>Test Case</title>
</head>
<body>
<div id="testdiv" runat="server"></div>
</body>
</html>
Default.aspx.cs:
using System;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
testdiv.InnerHtml = "<input type=\"button\" runat=\"server\" OnServerClick=\"Test_Click\"/>";
}
protected void Test_Click(object sender, EventArgs e)
{
testdiv.InnerHtml = "REGISTERED CLICK!";
}
}
我希望在点击按钮时在服务器上运行Test_Click
,但它不会。
我尝试将其放在服务器端表单中,但它仍然无法正常工作。
答案 0 :(得分:4)
试试这个,
protected void Page_Load(object sender, EventArgs e)
{
var button = new Button {ID = "Button1", Text = "Test Button"};
button.Click += button_Click;
PlaceHolder1.Controls.Add(button);
}
private void button_Click(object sender, EventArgs e)
{
testdiv.InnerHtml = "Button1 is clicked";
}