【问题标题】:asp.net fileupload value to textboxasp.net fileupload 值到文本框
【发布时间】:2014-02-27 09:33:43
【问题描述】:

我正在使用 formview 将数据插入到我的数据库中。我必须使用文件上传来存储图像文件的名称。代码在 textbox 上做得很好。我只需要指导如何通过表单视图连接该文件上传。

代码:

<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1" 
DefaultMode="Insert">

<InsertItemTemplate>
  Name:
    <asp:TextBox ID="shop_nameTextBox" runat="server" 
        Text='<%# Bind("shop_name") %>' />
    <br />
    shop_image:
    <asp:TextBox ID="shop_imageTextBox" runat="server" 
        Text='<%# Bind("shop_image") %>'  />
    <asp:FileUpload ID="FileUpload1" runat="server"  />
    <br />

    shop_desc:
    <asp:TextBox ID="shop_descTextBox" runat="server" 
        Text='<%# Bind("shop_desc") %>' />
    <br />


    shop_contact:
    <asp:TextBox ID="shop_contactTextBox" runat="server" 
        Text='<%# Bind("shop_contact") %>' />
    <br />
    <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
        CommandName="Insert" Text="Insert" />

    &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" 
        CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>

</asp:FormView>

我尝试将文本框连接到文件上传控件,但没有成功。谷歌搜索也没有得到回报。

【问题讨论】:

  • 您是否尝试从文件上传控件获取文件名?
  • 是的,我必须在文本框中获取文件名。
  • FileUpload1.FileName 会给你文件名对吗?
  • 但是 textbox 的属性 Text='' 已经存在。如何添加文件名?
  • 上传文件后,您将文件保存在本地,只将文件名以及其他详细信息发送到数据库对吗?

标签: c# asp.net file-upload


【解决方案1】:

如果我正确理解您的困境,以下应该提供解决方案,taken from here

<form id="form1" runat="server">
    <asp:FileUpload id="FileUploadControl" runat="server" />
    <asp:Button runat="server" id="UploadButton" text="Upload" onclick="UploadButton_Click" />
    <br /><br />
    <asp:Label runat="server" id="StatusLabel" text="Upload status: " />
</form>

protected void UploadButton_Click(object sender, EventArgs e)
{
    if(FileUploadControl.HasFile)
    {
        try
        {
            if(FileUploadControl.PostedFile.ContentType == "image/jpeg")
            {
                if(FileUploadControl.PostedFile.ContentLength < 102400)
                {
                    string filename = Path.GetFileName(FileUploadControl.FileName);
                    FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
                    StatusLabel.Text = "Upload status: File " + FileUploadControl.FileName + " uploaded!";
                }
                else
                    StatusLabel.Text = "Upload status: The file has to be less than 100 kb!";
            }
            else
                StatusLabel.Text = "Upload status: Only JPEG files are accepted!";
        }
        catch(Exception ex)
        {
            StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        }
    }
}

更新:是的,可以在插入模板中执行此操作,诀窍是使用 FindControl 方法,如下所示:

protected void BtnUpload_Click(object sender, EventArgs e)
{
    FormView formView = (FormView)((Button)sender).Parent.Parent;
    FileUpload fileUpload1 = (FileUpload)formView.FindControl("FileUpload1");

    if (fileUpload1.HasFile)
    {
        string filename = fileUpload1.FileName;
        //do inserting or uploading as you want
    }
}

【讨论】:

  • 是否可以在表单视图的插入模板中?
  • @user2125727 是的,请参阅我的“更新”部分。也许您应该考虑将图像绑定到 控件并使用 TextBox/Label 来显示文件上传信息?
猜你喜欢
  • 1970-01-01
  • 2011-08-30
  • 1970-01-01
  • 2013-12-12
  • 1970-01-01
  • 2012-03-01
  • 1970-01-01
  • 2011-08-31
  • 2014-08-03
相关资源
最近更新 更多