数据绑定列表框 selectedindex 始终为 -1

发布于 2024-08-22 05:13:23 字数 2330 浏览 7 评论 0原文

我有一个简单的上传表单。这是我的代码:

<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()

I've got a simple upload form. Here's my code:

<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>

You are supposed to be able to select which gallery you want to upload a logo image for, and then select your image file and click the upload button. Here's my upload sub:

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

My problem is that the data bound listbox gallerySelect's selectedIndex is always -1. I know its because it's data bound but I don't know what I have to do to be able to get the value. Any help would be appreciated.

EDIT:

Here's my binding code if you need it:

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凌乱心跳 2024-08-29 05:13:23

如果您在每次回发时对控件进行数据绑定,这将清除 SelectedValue,而是像这样包装您的数据绑定代码:

If Not (IsPostBack) Then
 //DataBind here
End If

If you're databinding the control each postback, this will clear the SelectedValue, instead wrap your databind code like this:

If Not (IsPostBack) Then
 //DataBind here
End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文