Reputation: 1076
I created a GridView and a DetailsView of Contacs. I want that if user select a contact in GridView, it will show full details in GridView.
this is the aspx source code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Contacs.aspx.cs" Inherits="Atid4.Contacs" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID"
DataSourceID="SqlDataSourceMain"
ShowSelectButton="true"
EnableViewState="true"
EnablePersistedSelection="true"
OnSelectedIndexChanged="GridViewMain_SelectedIndexChanged">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="first_name" HeaderText="first_name" SortExpression="first_name" />
<asp:BoundField DataField="last_name" HeaderText="last_name" SortExpression="last_name" />
</Columns>
</asp:GridView>
<asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="SqlDataSourceMain" Height="50px" Width="125px">
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSourceMain" runat="server" ConnectionString="<%$ ConnectionStrings:AtidConnectionString %>" SelectCommand="SELECT * FROM [Contacs]"></asp:SqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
I added a function GridViewMain_SelectedIndexChanged that is called when user selects a row in Gridview. the function is called when user selects one of the contacts, but the contact doesn't change in the GridView. it continues to show the first Contact.
this is the code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Atid4
{
public partial class Contacs : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DetailsView1.DataBind();
}
}
protected void GridViewMain_SelectedIndexChanged(object sender, EventArgs e)
{
DetailsView1.SetPageIndex(GridView1.SelectedIndex);
}
}
}
thanks.
Upvotes: 0
Views: 642