user1143550
user1143550

Reputation: 35

Gridview textbox efficiently get first and last index

For a gridview I'm working with, I have a textbox in an ItemTemplate where I would like to see if there is a more efficient way to get the first and last index value of however many rows ultimately get populated in the gridview. In the gridview below, that textbox I am evaluating is txtPercentage.

                    <asp:GridView ID="gvTest" AutoGenerateColumns="False" runat="server" 
                    GridLines="None" CssClass="table table-striped table-hover table-constant-values" 
                    ShowFooter="true" OnRowDataBound="gvTest_RowDataBound">
                    <Columns>
                        <asp:TemplateField HeaderText="Something" >
                            <ItemTemplate>
                                    Something
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Whatever">
                            <ItemTemplate>
                                    Whatever
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Percentage" SortExpression="Percentage">
                            <ItemTemplate>
                                <asp:TextBox ID="txtPercentage" runat="server" Text='<%# Eval("Percentage","{0:N8}") %>' class="calculate"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

For the jQuery side of things, I am able to get the index of the txtPercentage textboxes using this.

var i = 1;
var txtFirstPercentageIndex;
var txtLastPercentageIndex;

//works
$(".calculate").each(function (calindex, value)
{
  if (i == 1)
  {
      txtFirstPercentageIndex = calindex;
      i = 2;
  }
  txtLastPercentageIndex = calindex;
});

Is there a more efficient way of getting this? I thought something like this below might be better. However, it wasn't working.

//Doesn't work
txtFirstPercentageIndex = $(".calculate").first().index();
txtLastPercentageIndex = $(".calculate").last().index();

Thanks for any help/tips in advance.

Upvotes: 1

Views: 182

Answers (1)

Adil
Adil

Reputation: 148150

Try this,

lastIndex = $(".calculate").length-1;
if(lastIndex >= 0)
   firstIndex = 0;

Upvotes: 1

Related Questions