這是一個簡單的DetailsView,用於在數據庫中插入數據。我所有的參數都是以形式設置的。不過,我需要在代碼背後設置其中的一個。此窗體用於使用DetailsVeiw中的FileUpload控件將文件上載到文件系統。但我需要輸入上傳到DB的文件路徑。我怎樣才能做到這一點? 在測試代碼時,我發現表單中的參數正在工作,數據被插入到數據庫中,但沒有從後面的代碼中插入。 在此先感謝。ASP.Net C#DetailsView。我無法從代碼後面寫入單個插入參數
的形式:
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateInsertButton="True" AutoGenerateRows="False" DataKeyNames="attachmentID" DataSourceID="SqlDataSource1" DefaultMode="Insert" Height="50px" Width="125px"
OnItemInserted="DetailsView1_ItemInserted">
<Fields>
<asp:BoundField DataField="attachmentID" HeaderText="attachmentID" InsertVisible="False" ReadOnly="True" SortExpression="attachmentID" />
<asp:BoundField DataField="attachmentTitle" HeaderText="attachmentTitle" SortExpression="attachmentTitle" />
<asp:TemplateField HeaderText="Attach File" SortExpression="FileName">
<InsertItemTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
</InsertItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="attachmentType" HeaderText="attachmentType" SortExpression="attachmentType" />
<asp:BoundField DataField="attachmentDescription" HeaderText="attachmentDescription" SortExpression="attachmentDescription" />
<asp:BoundField DataField="attachmentFile" HeaderText="attachmentFile" SortExpression="attachmentFile" InsertVisible="false"/>
</Fields>
</asp:DetailsView>
<asp:Label ID="StatusLabel" runat="server" Text="Label"></asp:Label>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SPMS_DBConnectionString1 %>"
InsertCommand="INSERT INTO [Attachment] ([attachmentTitle], [attachmentFile], [attachmentType], [projectID], [attachmentDescription], [attachmentDate]) VALUES (@attachmentTitle, @attachmentFile, @attachmentType, @projectID, @attachmentDescription, GetDate())"
ProviderName="<%$ ConnectionStrings:SPMS_DBConnectionString1.ProviderName %>">
<InsertParameters>
<asp:Parameter Name="attachmentTitle" Type="String" />
<asp:Parameter Name="attachmentType" Type="String" />
<asp:SessionParameter Name="projectID" SessionField="project" Type="String" />
<asp:Parameter Name="attachmentDescription" Type="String" />
<asp:Parameter Name="attachmentFile" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
</div>
</form>
在後面的代碼
: 這是代碼不影響參數的行:
SqlDataSource1.InsertParameters["attachmentFile"].DefaultValue = Convert.ToString(MapPath("~/AttachedFiles/") + filename);
守則後面:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class stdDocumentsInsert : System.Web.UI.Page
{
string filename;
protected void Page_Load(object sender, EventArgs e)
{
if (!Directory.Exists(Server.MapPath("~/AttachedFiles/")))
{
Directory.CreateDirectory(Server.MapPath("~/AttachedFiles/"));
}
}
protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
FileUpload fu1 = (FileUpload)this.DetailsView1.FindControl("FileUpload1");
//if (fu1 == null)
//{
// //e.Cancel = true;
// StatusLabel.Text = "Could not find file upload";
//}
if (fu1.HasFile)
{
try
{
filename = Path.GetFileName(fu1.PostedFile.FileName);
fu1.SaveAs(Server.MapPath("~/AttachedFiles/") + filename);
StatusLabel.Text = "Upload status: File uploaded!";
SqlDataSource1.InsertParameters["attachmentFile"].DefaultValue = Convert.ToString(MapPath("~/AttachedFiles/") + filename);
//SqlDataSource1.insert(); // when i used this it worked but other fields in DB became Null
}
catch (Exception ex)
{
StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
else
{
//e.Cancel = true;
StatusLabel.Text = "No file uploaded";
return;
}
}
}