这是一个简单的 DetailsView,用于在数据库中插入数据。我所有的参数都是以形式设置的。但是,我需要在后面的代码中设置其中一个。此表单用于使用 DetailsVeiw 中的 FileUpload 控件将文件上传到文件系统。但我需要输入上传到数据库的文件路径。我怎样才能做到这一点?在测试代码时,我发现表单中的参数正在工作并且数据被插入到数据库中,而不是来自后面代码的那个。提前致谢。
表格:
<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;
}
}
}