[英]asp.net web form button onclick event error
我有asp.net Web表单,当我尝试将onclick事件关联到按钮时,出现以下错误:
编译器错误消息:CS1061:'ASP.webform1_aspx'不包含'Button1_Click'的定义,并且找不到扩展方法'Button1_Click'接受类型为'ASP.webform1_aspx'的第一个参数(您是否缺少using指令或组装参考?)
源错误:
第19行:第20行:第21行:第22行:第23行:
ASP代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="Form1" runat="server" method="post">
<div class="page">
<p>Persons</p>
<asp:Literal id="drpdwn" runat="server" />
<p>TransactionType</p>
<select id="sel" runat="server">
<option>bilance </option>
<option>liekākais parādnieks un aizdevējs</option>
<option>vidējais aizņēmuma lielums</option>
</select>
<asp:Button ID="Button1" runat="server" Text="Click Me" OnClick="Button1_Click" />
<table id="HTMLTable" width="300px;" runat="server">
<tr>
<td>
<b></b>
</td>
</tr>
</table>
</div>
</form>
</body>
<script type="text/javascript" >
var yourSelect = document.getElementById("sel");
var asdasd = yourSelect.options[yourSelect.selectedIndex].value;
</script>
</html>
而Codebehind如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.HtmlControls;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//int selectedIndex = (sel as HtmlSelect).SelectedIndex;
sel.Value.ToString();
SqlConnection cnn = new SqlConnection();
SqlDataReader rdr = null;
SqlCommand command = new SqlCommand();
cnn.ConnectionString = @"Server=localhost;Database=TildeTest;uid=AdminUser;pwd=AdminPwd;app=WebApplication1;";
DataSet ds = new DataSet();
DataTable dt = new DataTable();
string CommandText = "select * from persons";
command = new SqlCommand(CommandText);
command.CommandType = CommandType.Text;
//command.Connection = cnn;
try
{
cnn.Open();
command.Connection = cnn;
rdr = command.ExecuteReader();
if (rdr.HasRows)
{
drpdwn.Text = "<select>";
drpdwn.Text += "<option> </option>";
while (rdr.Read())
{
drpdwn.Text += "<option>" + rdr.GetString(1) + "</option>";
}
drpdwn.Text += "</select>";
}
else
{
Console.WriteLine("No rows found.");
}
rdr.Close();
}
catch (Exception ex)
{
Console.WriteLine(e.ToString());
}
finally
{
if (cnn.State == ConnectionState.Open)
cnn.Close();
}
}
void Button1_Click(Object sender,
EventArgs e)
{
// do something
}
}
}
您可以将void Button1_Click
更改为protected void Button1_Click
并尝试。
假设您正在使用Visual Studio,请在设计视图中双击按钮,然后在生成的函数内部编写代码。 那应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.