2017-07-06 54 views
0

我有一個非常簡單的頁面上的按鈕。它接受一個文件,當你按下提交按鈕時,它會加載一個文件。沒有編碼,因爲它表示當試圖編寫按鈕的事件處理程序時,該按鈕在當前上下文中不存在。ASP.NET按鈕在當前上下文中不存在

代碼背後:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.HtmlControls; 

public partial class mass_upload : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     btnSubmitMass.ServerClick += new EventHandler(btnSubmitMass_Click); 
    } 

    protected void btnSubmitMass_Click(object sender, EventArgs e) 
    { 

    } 
} 

網頁:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="mass-upload.aspx.cs" Inherits="mass_upload" %> 
<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <title>Submissions</title> 
    <link href="/favicon.png" rel="icon"> 
    <link href="css/uikit.min.css" rel="stylesheet"> 
    <link href="css/uikit.css" rel="stylesheet"> 
    <link href="css/styles.css" rel="stylesheet" /> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> 
    <link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet"> 
    <script src="https://use.fontawesome.com/50bcaa1a6b.js"></script> 
    <script src="js/uikit.min.js"></script> 
    <script src="js/uikit.js"></script> 
    <script src="js/uikit-icons.min.js"></script> 
</head> 
<body runat="server"> 
    <div class="uk-section uk-section-xsmall" style="background-color: #820024; margin:0px;"> 
     <div class="uk-container uk-container-large"> 
      <div uk-grid=""> 
       <div class="uk-width-1-2"> 
        <a href="index.aspx"><img src="images/image.png"></a> 
       </div> 
       <div class="uk-width-1-2"> 
        <div class="uk-float-right"> 
        </div> 
       </div> 
      </div> 
     </div> 
    </div> 
    <nav class="uk-navbar-container" style="background-color: rgba(138,121,103,1); color: #ffffff;" uk-navbar="mode: click"> 
     <div class="uk-navbar-center"> 
      <ul class="uk-navbar-nav uk-text-primary" style="color: #ffffff;"> 
       <li class="uk-active"> 
        <a href="index.aspx">Home</a> 
       </li> 
       <li> 
        <a href="manual-upload.aspx">Manual Submit</a> 
       </li> 
       <li> 
        <a href="mass-upload.aspx">Mass Submit</a> 
       </li> 
      </ul> 
     </div> 
    </nav> 
    <div class="uk-section uk-margin-remove-bottom uk-padding-remove-bottom"> 
     <div class="uk-container"> 
      <div class="uk-grid"> 
       <div class="uk-text-center uk-width-1-1"> 
        Click in the field below, browse to the file to be uploaded and click "Open". Once the file name is visible in the form field, click the Submit button. 
       </div> 
      </div> 
     </div> 
    </div> 
    <div class="uk-section" runat="server"> 
     <div class="uk-container uk-container-large uk-margin uk-margin-left uk-margin-right uk-padding" style="border:1px solid #cccccc;" runat="server"> 
      <form id="form1" runat="server"> 
       <fieldset class="uk-fieldset" runat="server"> 
        <!-- ****************************************************** --> 
        <div class="uk-grid-small [email protected] uk-text-center" uk-grid> 
         <div> 
          <div class="uk-margin" uk-margin> 
           <div uk-form-custom="target: true"> 
            <input type="file"> 
            <input class="uk-input uk-form-width-medium" disabled placeholder="Select file" type="text"> 
           </div> 
           <input type="button" id="btnSubmitMass" runat="server" value="Submit" class="uk-button uk-button-default" /> 
          </div> 
         </div> 
        </div> 
        <!-- ****************************************************** --> 
       </fieldset> 
      </form> 
     </div> 
    </div> 
    <div class="uk-grid" style="margin-bottom:15px;"> 
     <div class="uk-text-center uk-width-1-1"> 
      Copyright &copy 
     </div> 
    </div> 
</body> 
</html> 
+0

嘗試刪除它,然後再次將其添加到aspx頁面。有時,當您複製/粘貼代碼時,Visual Studio有時會變得有點困惑。 – VDWWD

+0

我已經完成了這項工作,我刪除了該頁面並重新創建了該頁面,但仍然存在相同的錯誤。 – Caveman42

+0

奇怪。我已經複製了所有代碼,我的VS找到了按鈕... – VDWWD

回答

0

你可以嘗試手動將它聲明:

protected HtmlInputButton btnSubmitMass; 

protected void Page_Load(object sender, EventArgs e) 
{ 
    btnSubmitMass.ServerClick += new EventHandler(btnSubmitMass_Click); 
} 

protected void btnSubmitMass_Click(object sender, EventArgs e) 
{ 

} 

通常控制應被自動聲明如果添加runat="servser"然後保存aspx頁面。所以你也可以簡單地嘗試重命名,保存,然後重新命名爲你想要的ID並再次保存。

+1

爲什麼不讓按鈕變成一個簡單的''並放棄手動聲明和'EventHandler'附件? (真的好奇) – Santi

+0

這樣做表示它已經包含了btnSubmitMass的定義。 並沒有@Santi,我一直在使用輸入類型=「按鈕」,所以我沒有看到在這個頁面上更改的理由。 – Caveman42

+0

@Santi:這是一個html按鈕,我不知道爲什麼OP需要它 –

相關問題