简体   繁体   English

asp.net fileupload值到文本框

[英]asp.net fileupload value to textbox

I am using formview for inserting data into my database.I have to store name of an image file using file upload.Code is doing good with textbox.I just need guidance that how to connect that fileupload via form view. 我正在使用formview将数据插入到数据库中。我必须使用文件上传来存储图像文件的名称。代码在文本框中表现良好。我只需要有关如何通过表单视图连接该文件上传的指导。

code: 码:

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

I have tried to connect text box to file upload control but in vain. 我试图将文本框连接到文件上传控件,但徒劳。 Googling has also not paid off. 谷歌搜索还没有得到回报。

If I understand your dilemma correctly the following should provide a solution, 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;
        }
    }
}

Update : Yes it is possible to do this inside an inserttemplate, the trick is to use the FindControl method something like as follows: 更新 :是的,可以在inserttemplate中执行此操作,技巧是使用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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM