Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.Net按钮和onclick事件不起作用_C#_Asp.net_Button_Onclick - Fatal编程技术网

C# ASP.Net按钮和onclick事件不起作用

C# ASP.Net按钮和onclick事件不起作用,c#,asp.net,button,onclick,C#,Asp.net,Button,Onclick,我在单元格中创建了一个带有按钮的动态表,但当我单击某个按钮时,控制台中会显示此错误: Uncaught ReferenceError: bttn_Click is not defined at HTMLUnknownElement.onclick (VM51 panel.aspx:1) 这是我在html和asp.net中创建按钮的方式: StringBuilder htmlStr = new StringBuilder(); htmlStr.Append("<asp:button

我在单元格中创建了一个带有按钮的动态表,但当我单击某个按钮时,控制台中会显示此错误:

Uncaught ReferenceError: bttn_Click is not defined
    at HTMLUnknownElement.onclick (VM51 panel.aspx:1)
这是我在html和asp.net中创建按钮的方式:

StringBuilder htmlStr = new StringBuilder();
htmlStr.Append("<asp:button onclick='bttn_Click' class='btn btn-warning btn-xs'  ID='Bttn_PDF'>PDF</asp:button>");
PlaceHolder1.Controls.Add(new Literal { Text = htmlStr.ToString() });
这是因为事件实际上期望调用特定于Javascript的事件,而您当前正试图调用服务器端事件。在实际的ASP.NET按钮控件上定义此类事件时,您希望改为使用OnClientClick事件。但这并不重要,因为您希望针对的是服务器端事件,而不是客户端事件

构建您的按钮服务器端

您可能需要创建一个实际的按钮控件,定义一个指向您的方法的click事件处理程序,然后将该控件添加到页面中,而不是构建字符串:

// Build your button with its specific properties
Button button = new Button();
button.ID = "Bttn_PDF";
button.CssClass = "btn btn-warning btn-xs";
// Wire up its click event
button.Click += bttn_Click;
// Add it to the form
PlaceHolder1.Controls.Add(button);

我不确定它是否会工作,但您缺少runat=server,这是服务器上呈现的控件所必需的。
// Build your button with its specific properties
Button button = new Button();
button.ID = "Bttn_PDF";
button.CssClass = "btn btn-warning btn-xs";
// Wire up its click event
button.Click += bttn_Click;
// Add it to the form
PlaceHolder1.Controls.Add(button);