<%@ Page Language="VB" MasterPageFile="~/site.master" AutoEventWireup="false" CodeFile="GalleryUpload.aspx.vb" Inherits="GalleryUpload" title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<asp:ListBox ID="ListBox1" runat="server" DataSourceID="SqlDataSource1" DataTextField="FixtureDate" DataValueField="FixtureID"></asp:ListBox><br /><br />
<asp:TextBox ID="TextBoxMemberName" runat="server"></asp:TextBox><br /><br />
<asp:TextBox ID="TextBoxNotes" runat="server"></asp:TextBox>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:WroxUnitedConnectionString %>"
InsertCommand="INSERT INTO [Gallery] ([FixtureID], [UploadedByMemberName], [Notes], [PictureURL]) VALUES (@FixtureID, @UploadedByMemberName, @Notes, @PictureURL)"
OldValuesParameterFormatString="original_{0}"
>
<InsertParameters>
<asp:ControlParameter Name="FixtureID" ControlID="ListBox1" PropertyName="selectedvalue" Type="Int32" />
<asp:ControlParameter Name="UploadedByMemberName" ControlID="TextBoxMemberName" PropertyName="text" Type="string" />
<asp:ControlParameter Name="Notes" ControlID="TextBoxNotes" PropertyName="text" Type="string" />
<asp:ControlParameter Name="PictureURL" ControlID="fileupload1" PropertyName="filename" Type="string" />
</InsertParameters>
</asp:SqlDataSource>
<asp:Label ID="Label2" runat="server" Style="position: relative" Text="Label"></asp:Label>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WroxUnitedConnectionString %>" SelectCommand="SELECT [FixtureID], [FixtureDate] FROM [Fixtures]"></asp:SqlDataSource>
</asp:Content>
注;修改<InsertParameters>集合,使它们从四个控件获取信息。ControlParameters将包含由用户向一些控件(不是容纳这些参数的数据绑定控件)中输入的值。通过在命令中将at(@)符号放在参数的名称前面引用参数中的值。参数按照不同的写命令组织成几个集合。因此当执行insert操作时,ASP.NET 2.0 将在 <InsertParameters>参数集内寻找值。
Partial Class GalleryUpload
Inherits System.Web.UI.Page
Dim result As Integer
Protected path As String = Server.MapPath("MatchImages")
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If FileUpload1.HasFile Then
Try
FileUpload1.SaveAs(path & "\" & FileUpload1.FileName)
Catch ex As Exception
Label1.Text = "上传失败" & ex.Message
End Try
Label1.Text = FileUpload1.FileName & "上传成功"
SqlDataSource2.Insert()
Label2.Text = SqlDataSource2.Insert()
Else
Label1.Text = "未选择上传文件"
End If
End Sub
End Class
注:通过在Button1_Click事件中添加一行触发insert方法的代码来实现SqlDataSource2执行插入新纪录的命令