2015-06-03 120 views
3

假設我有一個aspx代碼在這裏:獲取未捕獲參考錯誤:沒有定義的button1_Click

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SearchCustomer.aspx.cs" Inherits="WebApplication1.eyeofheaven.SearchCustomer" %> 

<!DOCTYPE html> 

<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<meta charset="utf-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<link rel="stylesheet" type="text/css" href="StyleSheets/SearchCustomerStyle.css"> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css"> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script> 
<title>Search Customer</title> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div class="row"> 
    <div class="twelve columns"> 
      <!-- Header--> 
      <div class="container"> 
       <nav role="navigation" class="navbar navbar-inverse navbar-fixed-top"> 
        <!-- Brand and toggle get grouped for better mobile display --> 
        <div class="navbar-header"> 
         <button type="button" data-target="#navbarCollapse" data-toggle="collapse" class="navbar-toggle"> 
          <span class="sr-only">Toggle navigation</span> 
          <span class="icon-bar"></span> 
          <span class="icon-bar"></span> 
          <span class="icon-bar"></span> 
         </button> 
        </div> 
        <!-- Collection of nav links, forms, and other content for toggling --> 
        <div id="navbarCollapse" class="collapse navbar-collapse"> 
         <ul class="nav navbar-nav"> 
          <li><a href="EyeOfHeaven.aspx">Home</a></li> 
          <li class="dropdown"> 
           <a data-toggle="dropdown" class="dropdown-toggle active" href="#">Search<b class="caret"></b></a> 
           <ul role="menu" class="dropdown-menu"> 
            <li><a href="SearchCustomer.aspx">Search Form(Customer)</a></li> 
            <li><a href="SearchVehicle.aspx">Search Form(Vehicle)</a></li> 
           </ul> 
          </li> 
         </ul> 
        </div> 
       </nav> 
      </div> 
    </div> 
</div> 

<!-- Search form customer--> 
<div id="searchcustomer" class="page-header"> 
    <h3><span class="glyphicon glyphicon-th-large"></span>Search Customer</h3> 
</div> 
<div class="row"> 
    <div class="col-md-4"><input type="text" size="20" class="form-control" placeholder="Customer ID"></div> 
    <div class="col-md-4"> 
     <select class="form-control" id="Countries"> 
       <option>Country</option> 
     </select> 
    </div> 
    <div class="col-md-4"> 
     <select class="form-control" id="Regions"> 
       <option>Regions</option> 
     </select> 
    </div> 
</div> 
<div class="row"> 
    <div class="col-md-4"> 
     <button type="button" onclick="Button1_Click()" id="searchinfo" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span> Search Info</button> 
    </div> 
</div> 

<!-- Information Table--> 
<div class="table-responsive"> 
    <table id="MyTable" class="table table-bordered"> 
    </table> 
</div> 
</form> 
</body> 
</html> 

而且aspx.cs後臺代碼:

using MSSQLConnector; 
using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Data.SqlClient; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace WebApplication1.eyeofheaven 
{ 
    public partial class SearchCustomer : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
     } 
     protected void Button1_Click(object sender, EventArgs e) 
     { 
      Response.Write("<script>alert('Data Not Found.');</script>"); 
     } 
    } 
} 

我要顯示一個警告(「數據找不到。')在搜索信息按鈕中。但是我在控制檯出現錯誤「Uncaught Reference Error:Button1_Click is not defined」。我不知道是什麼讓代碼出現錯誤。或者,也許我的警報消息不是一個有效的C#代碼?我在此代碼中調用了Button1_Click函數:

<button type="button" onclick="Button1_Click()" id="searchinfo" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span> Search Info</button> 

請大家幫忙我是新來的c#語言,我正在修改以下語法和格式。

回答

4

您的按鈕沒有在服務器端定義。您需要使用runat = "server"到按鈕並更改onclick事件onserverclick像下面

<button type="button" id="searchinfo" runat="server" onserverclick="Button1_Click" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span> Search Info</button> 

而是使用Asp.Net asp:Button control象下面這樣:

<asp:Button ID="searchinfo" runat="server" onclick="Button1_Click" cssClass="btn btn-primary" Text="Search Info" /> 
+0

大家好,先生謝謝大家回答我的問題,肯定有用。因此,不建議使用此代碼:? –

+0

如果您使用的是asp.net,那麼最好使用asp.net控件。 –

+0

@laurencekeithalbano你仍然可以使用'

3

你需要使它asp button這樣

<asp:Button onclick="Button1_Click" ID="searchinfo" cssClass="btn btn-primary" Text="Search Info" runat="Server"> 
+0

@RahulNikate是錯字感謝。 – Mairaj

2

試試這個:

<asp:Button onclick="Button1_Click" ID="searchinfo" 
     cssClass="btn btn-primary" Text="Search Info" runat="Server"> 

即,將按鈕定義爲runat="Server"。只需添加runat屬性將告訴ASP.Net將該元素及其屬性和內容解析爲服務器控件。

相關問題