FileUpload1 does not exist in current context

2019-08-22 03:21发布

I'm trying to create a site that allows the user to upload a song and download any song that was uploaded. I'm using MySQL to store any uploaded song. I think I have the HTML and C# code right but it keeps throwing me an error saying FileUpload1 does not exist in the current context, you can use a navigation bar to switch context. My C# code is as follows.

using System;
using System.Collections.Generic;
using System.Web;
using System.Xml.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.IO;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Configuration;

public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnUpload_Click(object sender, EventArgs e)
{

    using (BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream))
    {
        byte[] bytes = br.ReadBytes((int)FileUpload1.PostedFile.InputStream.Length);
        string strConnString = "server=localhost;user id=root;database=music;persistsecurityinfo=True";
        using (MySqlConnection con = new MySqlConnection(strConnString))
        {
            using (MySqlCommand command = new MySqlCommand())
            {
                con.Open();


                string SQL = "insert into tblFiles(Name, ContentType, Data) values (@Name, @ContentType, @Data)";
                command.CommandText = SQL;
                command.Parameters.AddWithValue("@Name", Path.GetFileName(FileUpload1.PostedFile.FileName));
                command.Parameters.AddWithValue("@ContentType", "audio/mpeg3");
                command.Parameters.AddWithValue("@Data", bytes);
                command.Connection = con;
                command.ExecuteNonQuery();
                con.Close();
            }
        }
    }
    Response.Redirect(Request.Url.AbsoluteUri);

}

}

Here is my HTML for the site:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Music.aspx.cs" Inherits="Default3" %>
<asp:Content ID="music" runat="server" ContentPlaceHolderID ="ContentPlaceHolder1">
<asp:FileUpload ID="FileUpload1" runat="server"/>
<asp:Button ID="btnUpload" runat="server" Text="Upload" 
    onclick="btnUpload_Click"/>
<hr/>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" RowStyle-BackColor="#A1DCF2" Font-Names = "Arial" Font-Size = "10pt"
HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="FileName"/>
        <asp:TemplateField>
            <ItemTemplate>
                <object type="application/x-shockwave-flash" data='dewplayer-vol.swf?mp3=File.ashx?Id=<%# Eval("Id") %>'
                    width="240" height="20" id="dewplayer">
                    <param name="wmode" value="transparent"/>
                    <param name="movie" value='dewplayer-vol.swf?mp3=File.ashx?Id=<%# Eval("Id") %>'/>
                </object>
            </ItemTemplate>
       </asp:TemplateField>
        <asp:HyperLinkField DataNavigateUrlFields="Id" Text = "Download" DataNavigateUrlFormatString = "~/File.ashx?Id={0}" HeaderText="Download"/>
   </Columns>
</asp:GridView>
    </asp:Content>

1条回答
爷、活的狠高调
2楼-- · 2019-08-22 03:47

Your code behind defines a partial class "Default3". I assume your aspx page is named "Default3", and visual studio has generated the code to instantiate the elements in that aspx page. That generated code is also a partial class "Default3", but you will notice in that generated code, that the partial class is defined under a namespace. Your code behind does not define the rest of that class under any namespace, so it does not have access to the same objects. Looks like at some point you deleted the namespace specification out of your code behind.

查看更多
登录 后发表回答