I am using ASP.NET 4 and MVC and trying to add code to handle the “select” link. Once the user selects the row, I need to go display a new page with more detail. I have tried to capture the rowcommand but I never hit a the breakpoint. This is my current GridView code for the first page –
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<ValidationPortal.Models.Contact>>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<form id="form1" runat="server">
<h2>Index</h2>
<p>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="Id" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False"
ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Contacts]"></asp:SqlDataSource>
</p>
</form>
</asp:Content>
What do I need to add to make this work?
I think you need to specify an OnSelectedIndexChanged event in your grid markup. Your event would probably just redirect to your details page with the ID of the selected item. It would look something like this:
Grid:
C#:
Personally, for what you are doing I would skip the select command and create an item template with a link to your url, as shown here