MoiD101
MoiD101

Reputation: 195

Selecting the text (innerhtml) from an asp.net control with jquery

i have an asp.net gridview with and icon on each row, that when clicked i wish to create a popup with editable text boxes/input fields in. and these text boxes and input fields are to contain the existing text from the row icon i just clicked on.

i thought i could do it like this but it gets the text from classes, but not from id's see bottom row of code:

$(".imgEditFields").click(function () {
        var row = $(this).closest("tr");
        var strServiceID = $(".clHiddenServiceID", row).text();
        var strTitleOfDoc = $(document).attr('title');
        var strSessionUser = strTitleOfDoc.replace("Order Tracking for ", "");
        var strPSTNNumber = $(".clHiddenPSTNNum", row).text();
        var strPSTNNum = strPSTNNumber.replace(/'/g, "");
        $('#popupTrackerEditFields h1:first').text("Edit Record " + strPSTNNum);
        var strSuppOrdNo = $("#lblSupplierOrderNum", row).text();

Below are my comments having tried several permatations of the comment/replies from you all

        $(".imgEditFields").click(function () {
        //I get the row with the icon that was clicked on
        var row = $(this).closest("tr");
        var strServiceID = $(".clHiddenServiceID", row).text();
        var strTitleOfDoc = $(document).attr('title');
        var strSessionUser = strTitleOfDoc.replace("Order Tracking for ", "");
        var strPSTNNumber = $(".clHiddenPSTNNum", row).text();
        var strPSTNNum = strPSTNNumber.replace(/'/g, "");
        $('#PopUpTrackerEditFields h1:first').text("Edit Record " + strPSTNNum);
        var strSuppOrdNo = $(".clSupplierOrderNumber", row).text();
        //All the above work great and i get what is expected, the problem begins blow when trying to select by //the id (in that row)

        //The below line achieves what i want visually but i cannot gaurantee its the correct row can i?
        $('#MainContent_tbPopUpEditSuppOrdNo').val(strSuppOrdNo);
        //The below line does not work as suggested by the replies and i have tried with HTML, val, Text etc
        $('#<%=tbPopUpEditSuppOrdNo.ClientID%>', row).html(strSuppOrdNo);
        //the blow works again not sure if it is selecting the correct row each time
        $('#MainContent_tbPopUpEditSuppOrdNo').attr('title', 'Edit your Supplier no here for ' + strPSTNNum);

Upvotes: 0

Views: 307

Answers (2)

tymeJV
tymeJV

Reputation: 104775

Since these are ASP.NET controls, ID's are rendered differently. You can target them with the following syntax:

$("#<%= idOfControlHere.ClientID%>")

For one of your ID's, here's an example:

$("#<%= lblSupplierOrderNu.ClientID %>", row).text();

Upvotes: -1

Chris Dixon
Chris Dixon

Reputation: 9167

This is due to the element ID being generated by .NET. I'd say to use the ID of the control, but this is going to be nasty as your controls are in a GridView, and will be unpredictable.

Best bet is to use a class on your textbox (using CssClass) and selecting that for the row, as you've previously done.

Upvotes: 2

Related Questions