开发者

how to pass value from asp.net server control using jQuery?

开发者 https://www.devze.com 2023-01-14 16:35 出处:网络
I UPDATED the code with code behind. I\'m doing something dynamic with the label contrtol. I have the following jQuery code working as it is (passing the value of \'test\") but what I want to do is t

I UPDATED the code with code behind. I'm doing something dynamic with the label contrtol.

I have the following jQuery code working as it is (passing the value of 'test") but what I want to do is to pass the value of the label control (lblNames). I'm using the label control to collect the uploaded file names. Is there a way?

jQuery:

$(document).ready(function () {
    $("#btnUpload").click(function () {
        $("#Notes", top.document).val('test');
    });
});

ASPX code:

<asp:Label ID="lblNames" runat="server" visible="true" ></asp:Label>

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.Ad开发者_JAVA百科d(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


You can easily insert the ID into your script and pull it out that way:

$(document).ready(function () {
    $("#btnUpload").click(function () {
        $("#Notes", top.document).val( $("#<%= lblNames.ClientID %>").text() );
    });
});


you can get the value of that control client side by doing

$("#<%=lblNames.ClientID%>").val() 

or it may be .text() i cant remember off the top of my head what a asp:label renders!!

hope that helps


As far as I understand your question you simply want to insert the value of the label text into this jQuery method to change the value of the Notes form element.

To do that when the page loads, you would do something like:

 $(document).ready(function () { 
    $("#btnUpload").click(function () { 
        $("#Notes", top.document).val('<%= lblNames.Text %>'); 
    }); 
}); 

Does that suffice or are you doing something dynamic with the label?


asp:Label renders as a span with id lblNames. you can try something like this

$('#lblNames').html() in place of 'test'


EDIT: When you call this click event, is it a full page post back or is it partial? If it's partial, are you using Update Panels or AJAX to handle it?

From what you've posted, the click event of the button seems to be just updating the label's value with the filename and a comma after the entry.

Are you also looking to parse the value to break it out into individual pieces? I ask that because I'm still not clear what "#Notes" is referencing to exactly.

(I'm still learning quite a bit myself but eager to help out if I can!)


ORIGINAL

You can assigned a class to the label such as class="uploadedNames" and you should be able to access it's value like this:

var myValues = $(".uploadedNames").text();

So your label would be listed as:

<asp:Label ID="lblNames" runat="server" visible="true" class="uploadedNames"></asp:Label>

So the end result would be:

$(document).ready(function () {

var myValues = $(".uploadedNames").text();

    $("#btnUpload").click(function () {
        $("#Notes", top.document).val(myValues);
    });
});

Or keep it simpler and just go with:

$(document).ready(function () {
    $("#btnUpload").click(function () {
        $("#Notes", top.document).val( $(".uploadedNames").text() );
    });
});

And, as others have mentioned, if you prefer sticking with the actual ID over classes:

$(document).ready(function () {
    $("#btnUpload").click(function () {
        $("#Notes", top.document).val( $("#<%= lblNames.ClientID %>").text() );
    });
});

When using the ID directly, if you don't use ClientID, keep in mind that the ID rendered is auto generated, depending on which version of .NET you're running (as 4.0 introduced static names, predictable, auto, etc.) Accessing it by a Class name just removes the worry about how the ID will render in the html source, as well as giving you options to responding to multiple events/controls at once.


If you don't mix asp.net code in your script, set CssClass property of the label control as follows:

<asp:Label ID="lblNames" runat="server" visible="true" CssClass="lblNames" ></asp:Label>

Then, in your script you can get the innertext of the label control by using this class name:

$("#Notes", top.document).val($('.lblNames:first').text()); 
0

精彩评论

暂无评论...
验证码 换一张
取 消