FileUpload1在当前上下文中不存在

时间:2017-10-03 15:47:17

标签: c# mysql asp.net visual-studio

我正在尝试创建一个网站,允许用户上传歌曲并下载上传的任何歌曲。我正在使用MySQL存储任何上传的歌曲。我认为我有正确的HTML和C#代码,但它一直给我一个错误,说当前上下文中不存在FileUpload1,你可以使用导航栏来切换上下文。我的C#代码如下。

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);

}

}

以下是该网站的HTML:

<%@ 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 个答案:

答案 0 :(得分:0)

您的代码后面定义了一个部分类“Default3”。我假设您的aspx页面名为“Default3”,并且visual studio已生成代码以实例化该aspx页面中的元素。生成的代码也是部分类“Default3”,但您会注意到在生成的代码中,部分类是在命名空间下定义的。您的代码后面没有在任何命名空间下定义该类的其余部分,因此它无法访问相同的对象。看起来在某些时候你从后面的代码中删除了命名空间规范。