tags:

views:

75

answers:

2

I want to pass the value of the label control (lblNames) via jQuery. The current code is not working. I'm using the label control to collect the uploaded file names in code behind. Is there a way?

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="FileUploader.aspx.vb" Inherits="FileUploader" EnableEventValidation="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;

<head id="Head1" runat="server">
<base target="_self" />
<title>eReserve: Upload Multiple Files</title>
<script src="js/jquery-1.4.2.js" type="text/javascript"></script>
<script src="js/jquery.MultiFile.js" type="text/javascript"></script>
<script type="js/thickbox.js"></script>
</head>

<body>

<form id="form1" runat="server" name="uploader">

<div id="upload">

    <b>Click "Browse" or "Choose File" to upload a file. <br /><br />(If have additional files to upload, click "Browse" or "Choose File" button again).</b><br /><br />

    <asp:FileUpload ID="FileUpload1" runat="server" class="multi" /> <br />
    <asp:Button ID="btnUpload" runat="server" Text="Upload All" /> <br /><br />  
    <asp:Label ID="lblFiles" runat="server" ForeColor="Green"></asp:Label> <br /><br />
    <asp:Label ID="lblWarning" runat="server" ForeColor="Red"></asp:Label> <br /><br />
    <asp:Label ID="lblCloseWindow" runat="server" ForeColor="Red"></asp:Label> <br /><br />
    <asp:Label ID="lblNames" runat="server" class="uploadedNames" visible="true" ></asp:Label>

    <input type="submit" id="closeWindow" value="Close Window" onclick="self.parent.tb_remove(); " />

</div>
</form>

<script type="text/javascript">

    $(document).ready(function () {
    $("#btnUpload").click(function () {
       var lblNamesValue = $("#lblNames").val();
        $("#Notes", top.document).val(lblNamesValue);
    });
});
</script>

</body>  
</html>

Code behind:

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs)    Handles btnUpload.Click

Dim fileExt As String

fileExt = System.IO.Path.GetExtension(FileUpload1.FileName)

If (fileExt <> ".exe") Then
    Dim fileNames As New List(Of String)

    Try
        Dim hfc As HttpFileCollection = Request.Files

        lblFiles.Text = String.Empty
        lblNames.Text = String.Empty

        For i As Integer = 0 To hfc.Count - 1
            Dim hpf As HttpPostedFile = hfc(i)
            If hpf.ContentLength > 0 Then

                fileNames.Add(hpf.FileName)

                hpf.SaveAs(Server.MapPath("/directory") & "\" & System.IO.Path.GetFileName(hpf.FileName))

                lblFiles.Text += "<b>File: </b>" & hpf.FileName & "     " & "    Uploaded Successfully! <br />"

                lblNames.Text += hpf.FileName & ", "

            Else
                lblWarning.Text = "Please choose a file to upload."
            End If
        Next i
    Catch ex As Exception

    End Try
+1  A: 

In your jQuery selector, you need to pass it the ID that ASP.NET generates. Try something like this:

$('#<%=lblNames.ClientID %>').text();
Jimmy
didn't work for me.
codeLearner
Like marduk said, try using .html() or .text() instead of .val(). I edited my answer to reflect that
Jimmy
A: 

.val() is typically used for just input types. try using .html() or something to just grab the text.

marduk