1

I would like to return text label in my gridview with Jquery

 <div class="risksContainer">
        <div class="risksGrid">
            <asp:GridView ID="GridViewRisks" runat="server" 
        [...]
        />
                <Columns> 
                    <asp:TemplateField HeaderText="...">
                        <ItemTemplate>
                            <div class="CodeProductColumn">
                                <asp:Label ID="IDRisk" runat="server" CssClass="IDRiskIndex" Text="<%# Item.ID_Risk %>" />
                                <asp:Repeater ID="LabelRepeatCodeProduct" runat="server"
                                [....]
                          </asp:Repeater>
                   </div>
            </ItemTemplate>

I try many solutions, but i don't know how i get the label Text of my selected row. javascript :

function blabla(){
 var Id_risk = $(".risksContainer .risksGrid .IDRiskIndex").text();
 alert("Id_risk =" + Id_risk );
 return Id_risk ;
}

(this function start when i click on edit button in my row)

I get text of all rows in my gridview and not only my selected row. I try with "parent, child, first, selected, rows[]... I am beginner and i'm desperate to find it

2 Answers 2

1

Use try in JavaScript:

<script type="text/javascript"> 
  function blabla() 
  {
      // find gridview
      var gv = document.getElementById("<%=GridViewRisks.ID %>");

      // get row count
      var gvRowCount = gv.rows.length;
      var i = 1;
      for (i; i<= gvRowCount - 1; i++) 
      {
      // get label value from column 2
      alert(gv.rows[i].cells[2].childNodes[1].innerHTML);
      }
      return false;
  }
</script> 
<asp:Button ID="Button1" runat="server" OnClientClick=" blabla();return false;" ... />
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming you set the selected row in code behind with GridViewRisks.SelectedIndex = i, you can set the CSS class of the selected row.

<asp:GridView ID="GridViewRisks" runat="server" SelectedRowStyle-CssClass="selectedRow">

Now you can use that class to get the correct row in jQuery.

var Id_risk = $(".selectedRow .IDRiskIndex").text();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.