[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I get the SelectedIndex of a GridView when clicking a LinkButton in an ItemTemplate?

Posted on 2009-02-24
9
Medium Priority
?
692 Views
Last Modified: 2012-05-06
My GridView looks like this:
        <asp:GridView ID="myGridView" runat="server" AutoGenerateColumns="False" AllowPaging="True" >
            <Columns>            
                <asp:BoundField DataField="PHONEID" Visible="False"/>
                <asp:BoundField DataField="PHONENUM" HeaderText="Phone Number" />
                <asp:BoundField DataField="PHONE_LABEL" HeaderText="Label"  />
                <asp:BoundField DataField="PRIMARY_PHONE" HeaderText="Primary" />
                <asp:BoundField DataField="LID" Visible="False"/>
                <asp:TemplateField ShowHeader="false">
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="false" Text="Edit" OnClick="lnkEdit_Click()" />
                    </ItemTemplate>
                </asp:TemplateField>
               
                <asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:LinkButton  ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
                             OnClientClick='return confirm("Are you sure you want to delete this phone?");' Text="Delete" CommandArgument="x" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <PagerStyle CssClass="pager" />
        </asp:GridView>

In the OnClick event I want to call a procedure that takes the selected row and populates the data into textboxes for edits.  I'm not quite sure how to get the index of the row that was selected when the LinkButton is clicked.  I don't want to edit the data directly on the GridView.
0
Comment
Question by:nightshadz
  • 5
  • 4
9 Comments
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 2000 total points
ID: 23726096
use the DataKeyNames property of the gridveiw and from that you can get the ID of the row - Phone ID in this case
http://www.codeproject.com/KB/webforms/GridViewConfirmDelete.aspx
0
 

Author Comment

by:nightshadz
ID: 23726228
I'm not sure how to implement the example in that link you posted.  How would I use DataKeyNames if I don't yet know the row index?  Could you provide an example please?
0
 

Author Comment

by:nightshadz
ID: 23726361
Wait!  Don't answer yet!  I set CommandArgument='<%# Eval("PHONEID") %>' in the linkbutton.  I'll catch this in the RowCommand event.  If I'm on the right track let me know! =)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 23726391
if you look at the sample you will find everything in it
please at least go through the sampe attached and if there are any further issues i will be glad to help
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 23726405
yes that might also work
0
 

Author Comment

by:nightshadz
ID: 23726650
I'm a little stumped.  My link button now looks like: <asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="false" Text="Edit" CommandArgument='<%# Eval("PHONEID") %>' CommandName="EditPhone" />

In the myGridView_RowCommand I can get the PHONEID but how do I then get the selected row index based on this value?  Do I have to pass in all the other fields I need via the CommandArgument (doesn't seem like the best solution)?
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 23726665
wont phone id get you the record from the data source itslef?
0
 

Author Comment

by:nightshadz
ID: 23726837
My RowCommand event looks something like:

        If e.CommandName = "EditPhone" Then
            Try
                Dim dv As New DataView(Me._myDT)
                dv.RowFilter = "PHONEID = " & e.CommandArgument.ToString
                If dv.Count = 0 Then
                   'Not Found
                Else
                   'Found
                End If
            ......

This seems to do the trick.  If you think there is a better way or if there is something wrong with this code please let me know.  Thanks!
0
 

Author Comment

by:nightshadz
ID: 23726914
This seems to work better:
Dim dr() As DataRow = Me._myDT.Select("PHONEID = " & e.CommandArgument.ToString)

Thanks for your help!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month20 days, 11 hours left to enroll

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question