2016-12-31 187 views
0

我是C#的新手。什么这个错误表示无法理解:BC30456:'btnSubmit_Click'不是'webform2_aspx'的成员

enter image description here

我只是想从 http://csharp-video-tutorials.blogspot.com/2012/10/calling-stored-procedure-with-output.html

简单的例子,这是我的.aspx文件

<!DOCTYPE html> 


<table style="border: 1px solid black; font-family:Arial"> 
    <tr> 
     <td> 
      Employee Name 
     </td> 
     <td> 
      <asp:TextBox ID="txtEmployeeName" runat="server"></asp:TextBox> 
     </td> 
    </tr>   
    <tr> 
     <td> 
      Gender 
     </td> 
     <td> 
      <asp:DropDownList ID="ddlGender" runat="server"> 
       <asp:ListItem>Male</asp:ListItem> 
       <asp:ListItem>Female</asp:ListItem> 
      </asp:DropDownList> 
     </td> 
    </tr> 
    <tr> 
     <td> 
      Salary 
     </td> 
     <td> 
      <asp:TextBox ID="txtSalary" runat="server"></asp:TextBox> 
     </td> 
    </tr>  
    <tr> 
     <td colspan="2"> 
      <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
       onclick="btnSubmit_Click" /> 
     </td> 
    </tr>   
    <tr> 
     <td colspan="2"> 
      <asp:Label ID="lblMessage" runat="server"></asp:Label> 
     </td> 
    </tr> 
</table> 

这是aspx.cs文件

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.Configuration; 

namespace adoDemo 
{ 
    public partial class WebForm2 : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { } 
      protected void btnSubmit_Click(object sender, EventArgs e) 
     { 
      //Read the connection string from Web.Config file 
      string ConnectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; 
      using (SqlConnection con = new SqlConnection(ConnectionString)) 
      { 
       //Create the SqlCommand object 
       SqlCommand cmd = new SqlCommand("spAddEmployee", con); 
       //Specify that the SqlCommand is a stored procedure 
       cmd.CommandType = System.Data.CommandType.StoredProcedure; 

       //Add the input parameters to the command object 
       cmd.Parameters.AddWithValue("@Name", txtEmployeeName.Text); 
       cmd.Parameters.AddWithValue("@Gender", ddlGender.SelectedValue); 
       cmd.Parameters.AddWithValue("@Salary", txtSalary.Text); 

       //Add the output parameter to the command object 
       SqlParameter outPutParameter = new SqlParameter(); 
       outPutParameter.ParameterName = "@EmployeeId"; 
       outPutParameter.SqlDbType = System.Data.SqlDbType.Int; 
       outPutParameter.Direction = System.Data.ParameterDirection.Output; 
       cmd.Parameters.Add(outPutParameter); 

       //Open the connection and execute the query 
       con.Open(); 
       cmd.ExecuteNonQuery(); 

       //Retrieve the value of the output parameter 
       string EmployeeId = outPutParameter.Value.ToString(); 
       lblMessage.Text = "Employee Id = " + EmployeeId; 
      } 
     } 

    } 

} 

如果我把这个指令(我想这就是它调用)上的.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="adoDemo.WebForm2" %> 

顶部,然后我得到另一个错误:

System.Web.HttpException: Control 'txtEmployeeName' of type 'TextBox' must be placed inside a form tag with runat=server. 

它是什么,我的思念?

+0

尝试从asp:button中删除onclick并检查会发生什么? –

+0

Gotcha ..你的aspx页面乱七八糟。你的html没有runat服务器的表格标签 –

回答

1

您的两页不通信。你还没有告诉前端谁是后端。

the @ Page directive in the code-behind model contains attributes that reference an external file and a class. These attributes link the .aspx page to its code. https://msdn.microsoft.com/en-us/library/015103yb.aspx

一旦你添加了attributres,你需要围绕你的标记与<form></form>具有runat="server"属性。

User George Stocker给出了关于runat="server"属性的解释。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebForm2.aspx.cs" Inherits="WebForm2" %> 

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <table style="border: 1px solid black; font-family:Arial"> 
      <tr> 
       <td> 
        Employee Name 
       </td> 
       <td> 
        <asp:TextBox ID="txtEmployeeName" runat="server"></asp:TextBox> 
       </td> 
      </tr>   
      <tr> 
       <td> 
        Gender 
       </td> 
       <td> 
       <asp:DropDownList ID="ddlGender" runat="server"> 
        <asp:ListItem>Male</asp:ListItem> 
        <asp:ListItem>Female</asp:ListItem> 
       </asp:DropDownList> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        Salary 
       </td> 
       <td> 
        <asp:TextBox ID="txtSalary" runat="server"></asp:TextBox> 
       </td> 
      </tr>  
      <tr> 
       <td colspan="2"> 
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" /> 
       </td> 
      </tr>   
      <tr> 
       <td colspan="2"> 
        <asp:Label ID="lblMessage" runat="server"></asp:Label> 
       </td> 
      </tr> 
     </table> 
    </form> 
</body> 
</html> 
+0

非常感谢你#Kramb。这里有很多东西要学 – Oleg

相关问题