我正在使用 ASP.NET 开发一个 Web 应用程序。现在,我正在处理管理任务,我想让管理员能够上传将显示在 Web 应用程序页面之一上的时事通讯。我正在尝试在 ListView 中使用 ListView 控件和 UploadFile 控件。一切都以这种方式正常工作,除非代码隐藏中有问题,这是我无法上传任何内容并将其显示在 listView 中的方式。
部分来自 ASP.NET 代码:
<asp:ListView ID="ListView1" runat="server" DataKeyNames="NewsletterID"
DataSourceID="SqlDataSource1" InsertItemPosition="LastItem"
oniteminserting="ListView1_ItemInserting">
<InsertItemTemplate>
<tr style="">
<td>
<asp:ImageButton ID="InsertButton" ImageUrl="images/insert.png" Width="20px" runat="server" CommandName="Insert" />
<asp:ImageButton ID="CancelButton" ImageUrl="images/clear3.png" Width="20px" runat="server" CommandName="Cancel" />
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("Username") %>' OnPreRender="LoadUsername" />
</td>
<td>
<asp:TextBox ID="NewsletterTitleTextBox" runat="server"
Text='<%# Bind("NewsletterTitle") %>' />
</td>
<td>
asp:FileUpload ID="fileUpload1" runat="server" size="10" />
<asp:HiddenField ID="HiddenField2" runat="server" Value='<%# Bind("Url") %>' />
</td>
</tr>
</InsertItemTemplate>
<ItemTemplate>
<tr style="">
<td>
<asp:ImageButton ID="DeleteButton" ImageUrl="images/delete.png" Width="20px" runat="server" CommandName="Delete" />
<asp:ImageButton ID="EditButton" ImageUrl="images/edit.png" Width="20px" runat="server" CommandName="Edit" />
</td>
<td>
<asp:Label ID="NewsletterTitleLabel" runat="server" Text='<%# Eval("NewsletterTitle") %>' />
</td>
<td>
<a href='<%# Eval("Url") %>' target="_blank">
<asp:Label ID="UrlLabel" runat="server" Text='<%# Eval("Url") %>' />
</a>
</td>
</tr>
</ItemTemplate>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
DeleteCommand="DELETE FROM [Newsletter] WHERE [NewsletterID] = @NewsletterID"
InsertCommand="INSERT INTO [Newsletter] ([NewsletterTitle], [Url], [Username]) VALUES (@NewsletterTitle, @Url, @Username)"
SelectCommand="SELECT Newsletter.NewsletterID, Newsletter.NewsletterTitle, Newsletter.Url, Newsletter.Username FROM Newsletter INNER JOIN employee ON Newsletter.Username = employee.Username"
UpdateCommand="UPDATE [Newsletter] SET [NewsletterTitle] = @NewsletterTitle, [Url] = @Url, [Username] = @Username WHERE [NewsletterID] = @NewsletterID">
<DeleteParameters>
<asp:Parameter Name="NewsletterID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="NewsletterTitle" Type="String" />
<asp:Parameter Name="Url" Type="String" />
<asp:Parameter Name="Username" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="NewsletterTitle" Type="String" />
<asp:Parameter Name="Url" Type="String" />
<asp:Parameter Name="NewsletterID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
修改后的代码隐藏(在 C# 中):
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LoadUsername(object sender, EventArgs e)
{
((HiddenField)sender).Value = Service.User.ID;
}
protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
{
ListView lv = (ListView)sender;
FileUpload fUpload = (FileUpload)lv.InsertItem.FindControl("fileUpload1");
//string path = ("UploadedPix\\" + fUpload.FileName);
//var tempPath = "some-temp-path-";
var tempFile = fUpload.FileName;
if (fUpload.HasFile)
{
try {
fUpload.SaveAs(Server.MapPath("~/") + tempFile);
HiddenField hField= (HiddenField)lv.InsertItem.FindControl("HiddenField2");
}
catch(Exception ex){
e.Cancel = true;
}
}
}
稍微修改一下代码隐藏后,我收到以下错误:
无法将值 NULL 插入列“Url”、表“psspdb.dbo.Newsletter”;列不允许空值。插入失败。该语句已终止。
此 ListView 绑定到数据库中的 Newsletter 表。该表的结构如下: NewsletterID, NewsletterTitle, url, Username
用户名是 Usre 表中用户名的外键。通讯将采用 pdf 格式。我想要的是让管理员在用户单击此链接下载或预览时事通讯之前指定将显示给用户的时事通讯的标题。