asp.net web表单按钮onclick事件错误

时间:2015-03-23 09:51:59

标签: c# asp.net .net visual-studio-2010

我有asp.net网页表单,当我试图将onclick事件添加到按钮时,我收到以下错误:

  

编译器错误消息:CS1061:' ASP.webform1_aspx'不包含' Button1_Click'的定义没有扩展方法' Button1_Click'接受类型' ASP.webform1_aspx'的第一个参数。可以找到(你错过了使用指令或程序集引用吗?)

     

来源错误:

     

第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
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您可以将void Button1_Click更改为protected void Button1_Click并尝试。

答案 1 :(得分:0)

假设您正在使用visual studio,请从设计视图中双击该按钮,然后在生成的函数内部编写代码。这应该有用。

相关问题