簡単なアップロードフォームがあります。これが私のコードです:
<form id="Form1" method="post" enctype="multipart/form-data" runat="server"
<asp:label id="lblMsg" runat="server" CssClass="msg" />
<span class="msg">Select Gallery:</span>
<asp:listbox id="gallerySelect" runat="server" Rows="1" DataTextField="galleryName" DataValueField="galleryID" />
<span class="msg">File:</span><input type="file" id="galleryLogo" name="galleryLogo" runat="server" />
<input type="button" value="Upload" OnServerClick="Upload" runat="server"><br />
</form>
ロゴ画像をアップロードするギャラリーを選択し、画像ファイルを選択してアップロードボタンをクリックできるはずです。これが私のアップロードサブです:
Sub Upload(Source As Object, e As EventArgs)
If Not (galleryLogo.PostedFile Is Nothing) Then
Dim intFileNameLength as Integer
Dim strFileNamePath as String
Dim strFileNameOnly as String
'Logic to find the FileName (excluding the path)
strFileNamePath = galleryLogo.PostedFile.FileName
intFileNameLength = Instr(1, StrReverse(strFileNamePath), "\")
strFileNameOnly = Mid(strFileNamePath, (Len(strFileNamePath)-intFileNameLength)+2)
galleryLogo.PostedFile.SaveAs("c:\inetpub\wwwroot\galleries\" & strFileNameOnly)
Dim cmd As SqlCommand
Cache("sqlconn") = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
Cache("sqlconn").Open()
Dim query = "update gallery set logo = '" & strFileNameOnly & "' where gallery_id = " & gallerySelect.SelectedItem.Value.ToString()
cmd = New SqlCommand(query, Cache("sqlconn"))
cmd.ExecuteNonQuery()
tell the user something positive
lblMsg.Text = "File Uploaded Successfully<br />"
End If
End Sub
私の問題は、データバインドされたリストボックスgallerySelectのselectedIndexが常に-1であるということです。データがバインドされているのでわかりますが、値を取得するために何をしなければならないのかわかりません。どんな助けでもいただければ幸いです。
編集:
必要に応じて、私のバインディングコードを次に示します。
Dim galleryCmdSelect2 As SqlCommand
Dim galleryData2 As SqlDataReader
galleryCmdSelect2 = New SqlCommand("select gallery_name as galleryName, gallery_id as galleryID from galleries order by gallery_name", Cache("sqlconn"))
galleryData2 = galleryCmdSelect2.ExecuteReader()
gallerySelect.DataSource = galleryData2
gallerySelect.DataBind()
galleryData2.Close()