Getting a gridview's row index on command

Discussion in 'ASP .Net' started by David Lozzi, May 7, 2007.

  1. David Lozzi

    David Lozzi Guest

    Howdy,

    I have a gridview with a linkbutton in a template column (code is below).
    When the linkbutton is clicked i want to display a little confirm right in
    the gridview to confirm deleting the row. I tried using the following to
    grab the index, which appears to work on command columns, but not with
    buttons in a template? is this true? or am i missing something. how do I get
    the row index? Its returning the ID of the record, not the index of the row.
    THANKS!

    David Lozzi


    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.GridViewCommandEventArgs) Handles
    GridView1.RowCommand

    Dim rowID As Integer = Convert.ToInt32(e.CommandArgument) 'THIS
    DOESN'T WORK
    If e.CommandName = "Delete1" Then
    Dim lbDel As LinkButton =
    GridView1.Rows(rowID).FindControl("lbDelete")
    Dim div As HtmlGenericControl =
    GridView1.Rows(rowID).FindControl("confirm")
    lbDel.Visible = False
    div.Visible = True
    ElseIf e.CommandName = "Cancel1" Then
    Dim lbDel As LinkButton =
    GridView1.Rows(rowID).FindControl("lbDelete")
    Dim div As HtmlGenericControl =
    GridView1.Rows(rowID).FindControl("confirm")
    lbDel.Visible = True
    div.Visible = False
    ElseIf e.CommandName = "Confirm1" Then
    Dim lblLID As Label =
    GridView1.Rows(rowID).FindControl("lblListID")
    Dim listID As Integer = lblLID.Text
    sdsLists.DeleteCommand = "DELETE FROM [tblEmailLists] WHERE ID =
    " & listID & ""
    sdsLists.Delete()
    End If
    End Sub


    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    DataSourceID="sdsLists">
    <Columns>
    <asp:TemplateField HeaderText="ID" SortExpression="ID"
    Visible="False">
    <EditItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%#
    Eval("ID") %>'></asp:Label>
    </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Lists"
    SortExpression="strListName">
    <EditItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server" Text='<%#
    Bind("strListName") %>'></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:HyperLink ID="HyperLink1" runat="server"
    NavigateUrl='<%# "~/salesrep/editlist.aspx?id=" & Eval("ID") %>'
    Text='<%# Eval("strListName") %>'></asp:HyperLink>
    </ItemTemplate>
    <ItemStyle Wrap="False" />
    </asp:TemplateField>
    <asp:BoundField DataField="intSalesRep" HeaderText="intSalesRep"
    SortExpression="intSalesRep"
    Visible="False" />
    <asp:TemplateField HeaderText="# Dealers"
    SortExpression="Total">
    <EditItemTemplate>
    <asp:TextBox ID="TextBox2" runat="server" Text='<%#
    Bind("Total") %>'></asp:TextBox>
    </EditItemTemplate>
    <HeaderStyle Wrap="False" />
    <ItemTemplate>
    <div align="center"><asp:Label ID="Label2"
    runat="server" Text='<%# Bind("Total") %>'></asp:Label></div>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:LinkButton ID="lbDelete" runat="server"
    CommandName="Delete1">Delete</asp:LinkButton><div
    runat="server" id="confirm" visible="false"><asp:Label ID="Label1"
    runat="server"
    CssClass="required" Text="Are you sure you want
    to delete this list? Note that your dealers will NOT be
    deleted."></asp:Label>
    <asp:LinkButton ID="lbConfirm" runat="server"
    CommandName="Confirm1">Yes</asp:LinkButton>
    <asp:LinkButton ID="lbCancel" runat="server"
    CommandName="Cancel1">No</asp:LinkButton>
    <asp:Label ID="lblListID" runat="server"
    Text='<%# Bind("ID") %>' Visible="False"></asp:Label></div>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
     
    David Lozzi, May 7, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. H5N1
    Replies:
    0
    Views:
    2,739
  2. Replies:
    5
    Views:
    678
  3. troutbum

    Cannot Find Row Index after using row filter

    troutbum, Jan 22, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    10
    Views:
    487
    Andre
    Feb 19, 2004
  4. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    321
    Tomasz Chmielewski
    Mar 4, 2008
  5. Giggle Girl

    ie6 Insert Row at specific Row Index of Table

    Giggle Girl, Feb 5, 2006, in forum: Javascript
    Replies:
    18
    Views:
    297
    Thomas 'PointedEars' Lahn
    Feb 7, 2006
Loading...

Share This Page