Can't get current record ID from DetailsView (ReadOnly)

G

GoogleAccounts

I have a simple DetailsView in ReadOnly mode. I have paging enabled
because I need to show more than one record.

When user clicks next/prev page (PageIndexChanged event) I need to get
the current record ID (that is not one of the displayed fields, so
..Cell(x) won't work).

I expected DetailsView.SelectedValue to do the job but it doesn't (it's
always Nothing).

How can I solve this? My code follows.

1 %@ Page Language="VB" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <script runat="server">
6
7 Protected Sub DetailsView1_PageIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs)
8 TextBox1.Text = DetailsView1.SelectedValue & ""
9 End Sub
10 </script>
11
12 <html xmlns="http://www.w3.org/1999/xhtml" >
13 <head runat="server">
14 <title>Untitled Page</title>
15 </head>
16 <body>
17 <form id="form1" runat="server">
18 <div>
19 <asp:DetailsView ID="DetailsView1" runat="server"
AllowPaging="True" AutoGenerateRows="False"
20 DataKeyNames="DocumentID"
DataSourceID="SqlDataSource1"
21 OnPageIndexChanged="DetailsView1_PageIndexChanged"
DataMember="DefaultView">
22 <Fields>
23 <asp:BoundField DataField="Revision"
HeaderText="Revision" SortExpression="Revision" />
24 <asp:BoundField DataField="Date" HeaderText="Date"
SortExpression="Date" />
25 </Fields>
26 </asp:DetailsView>
27 <asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
28 SelectCommand="SELECT * FROM
[Documents]"></asp:SqlDataSource>
29 <br />
30 <asp:TextBox ID="TextBox1"
runat="server"></asp:TextBox></div>
31 </form>
32 </body>
33 </html>
 
F

Flinky Wisty Pomm

I tend to use

protected void SomeEventHandler(object sender, SomeEventArgsClass e)
{
MyClass someObject = (MyDetailsView.DataItem as MyClass);
if(null == someObject)
{
// DetailsView.DataItem is not castable to MyClass - it might be
null, or my typing might be wrong
}
else
{
DoSomethingWith(someObject);
}
}

private void DoSomethingWith(MyClass someObject){}


You should also be able to use the value from the DataKeyName, but I
never quite managed to get that to work for reasons which elude me.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top