@Brian if I'm correctly understanding your question you mean that you have everything wired up it user clicks on a row when the page is showing. You want that if somebody types the URL of page directly in browser and appends ?id=x to it, you should direct him to relevant page, select relevant row and populate FormView based on selected record.
Well if this is the case I've made something, but let me warn before hand it is not very elegant and its 2:30 AM here so please don't laugh.
Assumption:- In presented solution the DataKeyNames field is set to Primary Key of the source table. The rows fetched are sorted by this key. I'm using SubSonic 3 for talking to SQL Server 2005 Express DB (coincidently this is my first SS3 project, very busy at office to try it).
The table definition is as follows:
Create Table StudentsTable
( EnrolmentNumber int not null identity primary key,
StudentName nvarchar(50) not null)
The aspx:
<asp:GridView ID="gvStudents" runat="server" AllowPaging="True" PageSize="3"
OnSelectedIndexChanged="GridRowChanged" OnPageIndexChanging="GridPageChanging"
DataKeyNames="EnrolmentNumber">
<RowStyle BackColor="LightBlue" />
<AlternatingRowStyle BackColor="LightCoral" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
CommandName="Select" Text="Select"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<SelectedRowStyle BackColor="Red" BorderColor="Yellow" />
</asp:GridView>
<asp:FormView ID="fvSubjects" runat="server" AllowPaging="false"
Caption="Student" CaptionAlign="Left">
<ItemTemplate>
<table>
<tr>
<td>Enrolment Number</td>
<td><asp:Label ID="lblEN" runat="server"
Text=<%# Eval("EnrolmentNumber") %> /></td>
</tr>
<tr>
<td>Student Name</td>
<td><asp:Label ID="lblName" runat="server"
Text=<%# Eval("StudentName") %> /></td>
</tr>
</table>
</ItemTemplate>
</asp:FormView>
C# Code (Hold your laughs):
private IList<StudentsTable> students;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadData();
CheckQueryString(students);
}
}
private void LoadData()
{
students = StudentsTable.All().ToList();
gvStudents.DataSource = students;
gvStudents.DataBind();
}
private void CheckQueryString(IList<StudentsTable> students)
{
if (!Request.QueryString.HasKeys() ||
string.IsNullOrEmpty(Request.QueryString["erno"]))
{
return;
}
var erno = Request.QueryString["erno"];
int key;
if (!int.TryParse(erno, out key))
return;
for (var i = 0; i < students.Count; i++)
{
if (students[i].EnrolmentNumber == key)
{
SetPageSize(students, i);
break;
}
}
}
private void SetPageSize(IList<StudentsTable> students, int i)
{
var ps = gvStudents.PageSize;
var cp = i / ps;
var ridx = i - ps - 1;
var pageEvent = new GridViewPageEventArgs(cp);
GridPageChanging(null, pageEvent);
gvStudents.SelectedIndex = ridx;
GridRowChanged(null, EventArgs.Empty);
}
protected void GridRowChanged(object sender, EventArgs e)
{
var rIdx = gvStudents.SelectedIndex;
if (rIdx < 0) return;
var key = gvStudents.DataKeys[rIdx];
var lst= StudentsTable.Find(x => x.EnrolmentNumber == (int)key.Value);
SetFormView(lst);
}
private void SetFormView(IList<StudentsTable> student)
{
fvSubjects.DataSource = student;
fvSubjects.DataBind();
}
protected void GridPageChanging(object sender, GridViewPageEventArgs e)
{
var p = e.NewPageIndex;
if (p > gvStudents.PageCount)
p = gvStudents.PageCount - 1;
gvStudents.PageIndex = p;
LoadData();
}
PS:- It is failing if you put id of last record.
EDIT:- Here is link to Project