FileUpload1 在当前上下文中不存在 [英] FileUpload1 does not exist in current context

查看:64
本文介绍了FileUpload1 在当前上下文中不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个站点,允许用户上传歌曲并下载已上传的任何歌曲.我正在使用 MySQL 来存储任何上传的歌曲.我认为我的 HTML 和 C# 代码是正确的,但它一直向我抛出一个错误,指出当前上下文中不存在 FileUpload1,您可以使用导航栏切换上下文.我的C#代码如下.

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

}

}

这是我的网站 HTML:

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>

推荐答案

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

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.

这篇关于FileUpload1 在当前上下文中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆