2014-02-27 69 views
0

我正在使用formview插入數據到我的數據庫中。我必須使用文件上傳存儲圖像文件的名稱。代碼與textbox.I只需要指導如何連接通過表單視圖進行文件上傳。asp.net文件上傳值到文本框

代碼:

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

我試圖連接文本框文件上傳控制,但徒勞無功。谷歌搜索也沒有回報。

+0

您是否試圖從fileupload控件獲取文件名? – shanish

+0

是的,我必須在文本框中獲取文件名。 – user2125727

+0

只是'FileUpload1.FileName'會給你的文件名權嗎? – shanish

回答

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

更新:是的,它是有可能做到這一點的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 
    } 
} 
+0

是否可以在表單視圖的插入模板中使用? – user2125727

+0

@ user2125727是的,請參閱我的'更新'部分。也許你應該考慮將圖像綁定到控件並使用TextBox/Label來顯示文件上傳信息? –