当前上下文中不存在ASP.NET按钮

时间:2017-07-06 13:43:47

标签: html 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 uk-child-width-expand@s 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>

1 个答案:

答案 0 :(得分: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并再次保存。