How to read from Details view

E

Ed Dror

Hi there,

I'm using asp.net 2.0 vb

And I wrote this

Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles GridView1.SelectedIndexChanged
txtGNID.Text = Me.GridView1.SelectedRow.Cells(1).Text
End Sub

Every time when I slecet row my text box will show the selected cell value
(in this case is my VendorID)

Now I have a DetailsView

Protected Sub DetailsView1_PageIndexChanging(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.DetailsViewPageEventArgs) Handles
DetailsView1.PageIndexChanging

lblColorID.Text = ?

End Sub

What is the equivalent to the Grid view

I tried: lblColorID.Text = Me.DetailsView1.FindControl("ColorID").ID
but no luck

Thanks,

Ed Dror
 
S

Steven Cheng [MSFT]

Hi Ed,

As for the DetailsView cell reading question you mentioned, I've performed
some research on my side. Here are what I found:

** In DetailsView, when you change the page index, it will first fire the
PageIndexChanging/PageIndexChanged event, at that time, new data(row)
hasn't been bound to DetailsView. The DataBound happend after the
PageIndexChanged. Therefore, "PageIndexChanged" is not the proper place to
retrieve the new updated values from DetailsView. Instead, I suggest using
the "DataBound" event which will fire not only when changing index but also
at the initial databinding time.

** In addition, for retrieving values from DetailsView. if the field you
want to rettrieve is a key column(set as "DataKey" in DetailsView), you can
use DetailsView.DataKey.Value/Values to visit it. If it is not a key
column, you can access it through the
DetailsView.Row(index).Cells(index).Text

Here is a simple test page I used on my side which demonstrate using the
Databound event to retrieve the updated values(of the new selected record):

=========aspx ===========

<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testdbConnectionString %>"
DeleteCommand="DELETE FROM [persons] WHERE [id] = @id"
InsertCommand="INSERT INTO [persons] ([id], [name], [age], [title]) VALUES
(@id, @name, @age, @title)"
SelectCommand="SELECT [id], [name], [age], [title] FROM
[persons]" UpdateCommand="UPDATE [persons] SET [name] = @name, [age] =
@age, [title] = @title WHERE [id] = @id">
<DeleteParameters>
<asp:parameter Name="id" Type="Int64" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="name" Type="String" />
<asp:parameter Name="age" Type="Int16" />
<asp:parameter Name="title" Type="String" />
<asp:parameter Name="id" Type="Int64" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="id" Type="Int64" />
<asp:parameter Name="name" Type="String" />
<asp:parameter Name="age" Type="Int16" />
<asp:parameter Name="title" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<asp:DetailsView ID="DetailsView1" runat="server"
AllowPaging="True" AutoGenerateRows="False"
DataKeyNames="id" DataSourceID="SqlDataSource1" Height="50px"
Width="125px">
<Fields>
<asp:BoundField DataField="id" HeaderText="id"
ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="name"
SortExpression="name" />
<asp:BoundField DataField="age" HeaderText="age"
SortExpression="age" />
<asp:BoundField DataField="title" HeaderText="title"
SortExpression="title" />
</Fields>
</asp:DetailsView>
</div>

=-========code behind=======================


Partial Class VBNETViewPage
Inherits System.Web.UI.Page

Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DetailsView1.DataBound

Response.Write("<br/>DetailsView1_DataBound:")

Response.Write("<br/>key: " & DetailsView1.DataKey.Value)
Response.Write("<br/>name: " & DetailsView1.Rows(1).Cells(1).Text)
Response.Write("<br/>name: " & DetailsView1.Rows(2).Cells(1).Text)
Response.Write("<br/>name: " & DetailsView1.Rows(3).Cells(1).Text)

End Sub

Protected Sub DetailsView1_PageIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles DetailsView1.PageIndexChanged
Response.Write("<br/>DetailsView1_PageIndexChanged:")
End Sub
End Class

============================================

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: "Ed Dror" <[email protected]>
Subject: How to read from Details view
Date: Wed, 2 Apr 2008 13:29:20 -0700
Hi there,

I'm using asp.net 2.0 vb

And I wrote this

Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal
e
As System.EventArgs) Handles GridView1.SelectedIndexChanged
txtGNID.Text = Me.GridView1.SelectedRow.Cells(1).Text
End Sub

Every time when I slecet row my text box will show the selected cell value
(in this case is my VendorID)

Now I have a DetailsView

Protected Sub DetailsView1_PageIndexChanging(ByVal sender As Object, ByVal
e
 
E

Ed Dror

Steven,

I'm getting
Index was out of range. Must be non-negative and less than the size of the
collection.
Parameter name: index

for Databound event
lblColorID.Text = DetailsView1.Rows(0).Cells(1).Text

Thanks,

Ed Dror







Steven Cheng said:
Hi Ed,

As for the DetailsView cell reading question you mentioned, I've performed
some research on my side. Here are what I found:

** In DetailsView, when you change the page index, it will first fire the
PageIndexChanging/PageIndexChanged event, at that time, new data(row)
hasn't been bound to DetailsView. The DataBound happend after the
PageIndexChanged. Therefore, "PageIndexChanged" is not the proper place to
retrieve the new updated values from DetailsView. Instead, I suggest using
the "DataBound" event which will fire not only when changing index but
also
at the initial databinding time.

** In addition, for retrieving values from DetailsView. if the field you
want to rettrieve is a key column(set as "DataKey" in DetailsView), you
can
use DetailsView.DataKey.Value/Values to visit it. If it is not a key
column, you can access it through the
DetailsView.Row(index).Cells(index).Text

Here is a simple test page I used on my side which demonstrate using the
Databound event to retrieve the updated values(of the new selected
record):

=========aspx ===========

<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testdbConnectionString %>"
DeleteCommand="DELETE FROM [persons] WHERE [id] = @id"
InsertCommand="INSERT INTO [persons] ([id], [name], [age], [title]) VALUES
(@id, @name, @age, @title)"
SelectCommand="SELECT [id], [name], [age], [title] FROM
[persons]" UpdateCommand="UPDATE [persons] SET [name] = @name, [age] =
@age, [title] = @title WHERE [id] = @id">
<DeleteParameters>
<asp:parameter Name="id" Type="Int64" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="name" Type="String" />
<asp:parameter Name="age" Type="Int16" />
<asp:parameter Name="title" Type="String" />
<asp:parameter Name="id" Type="Int64" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="id" Type="Int64" />
<asp:parameter Name="name" Type="String" />
<asp:parameter Name="age" Type="Int16" />
<asp:parameter Name="title" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<asp:DetailsView ID="DetailsView1" runat="server"
AllowPaging="True" AutoGenerateRows="False"
DataKeyNames="id" DataSourceID="SqlDataSource1" Height="50px"
Width="125px">
<Fields>
<asp:BoundField DataField="id" HeaderText="id"
ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="name"
SortExpression="name" />
<asp:BoundField DataField="age" HeaderText="age"
SortExpression="age" />
<asp:BoundField DataField="title" HeaderText="title"
SortExpression="title" />
</Fields>
</asp:DetailsView>
</div>

=-========code behind=======================


Partial Class VBNETViewPage
Inherits System.Web.UI.Page

Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DetailsView1.DataBound

Response.Write("<br/>DetailsView1_DataBound:")

Response.Write("<br/>key: " & DetailsView1.DataKey.Value)
Response.Write("<br/>name: " & DetailsView1.Rows(1).Cells(1).Text)
Response.Write("<br/>name: " & DetailsView1.Rows(2).Cells(1).Text)
Response.Write("<br/>name: " & DetailsView1.Rows(3).Cells(1).Text)

End Sub

Protected Sub DetailsView1_PageIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles DetailsView1.PageIndexChanged
Response.Write("<br/>DetailsView1_PageIndexChanged:")
End Sub
End Class

============================================

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
From: "Ed Dror" <[email protected]>
Subject: How to read from Details view
Date: Wed, 2 Apr 2008 13:29:20 -0700
Hi there,

I'm using asp.net 2.0 vb

And I wrote this

Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles GridView1.SelectedIndexChanged
txtGNID.Text = Me.GridView1.SelectedRow.Cells(1).Text
End Sub

Every time when I slecet row my text box will show the selected cell value
(in this case is my VendorID)

Now I have a DetailsView

Protected Sub DetailsView1_PageIndexChanging(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.DetailsViewPageEventArgs) Handles
DetailsView1.PageIndexChanging

lblColorID.Text = ?

End Sub

What is the equivalent to the Grid view

I tried: lblColorID.Text = Me.DetailsView1.FindControl("ColorID").ID
but no luck

Thanks,

Ed Dror
 
S

Steven Cheng [MSFT]

Hi Ed,

Since you get "Index out of range exception", it is likely that you've
referenced the wrong cell in the DetailsView. Generally, you can turn on
page output trace as below(so as to inspect the control tree structure of
all the controls on page):

<%@ Page Language="C#" Trace="true" ...%>

Also, you can post a simplified aspx page template here so that I can have
a look to see what's the structure of your Detailsview control.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
From: "Ed Dror" <[email protected]>
References: <[email protected]>
Subject: Re: How to read from Details view
Date: Fri, 4 Apr 2008 13:56:39 -0700
Steven,

I'm getting
Index was out of range. Must be non-negative and less than the size of the
collection.
Parameter name: index

for Databound event
lblColorID.Text = DetailsView1.Rows(0).Cells(1).Text

Thanks,

Ed Dror







Steven Cheng said:
Hi Ed,

As for the DetailsView cell reading question you mentioned, I've performed
some research on my side. Here are what I found:

** In DetailsView, when you change the page index, it will first fire the
PageIndexChanging/PageIndexChanged event, at that time, new data(row)
hasn't been bound to DetailsView. The DataBound happend after the
PageIndexChanged. Therefore, "PageIndexChanged" is not the proper place to
retrieve the new updated values from DetailsView. Instead, I suggest using
the "DataBound" event which will fire not only when changing index but
also
at the initial databinding time.

** In addition, for retrieving values from DetailsView. if the field you
want to rettrieve is a key column(set as "DataKey" in DetailsView), you
can
use DetailsView.DataKey.Value/Values to visit it. If it is not a key
column, you can access it through the
DetailsView.Row(index).Cells(index).Text

Here is a simple test page I used on my side which demonstrate using the
Databound event to retrieve the updated values(of the new selected
record):

=========aspx ===========

<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testdbConnectionString %>"
DeleteCommand="DELETE FROM [persons] WHERE [id] = @id"
InsertCommand="INSERT INTO [persons] ([id], [name], [age], [title]) VALUES
(@id, @name, @age, @title)"
SelectCommand="SELECT [id], [name], [age], [title] FROM
[persons]" UpdateCommand="UPDATE [persons] SET [name] = @name, [age] =
@age, [title] = @title WHERE [id] = @id">
<DeleteParameters>
<asp:parameter Name="id" Type="Int64" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="name" Type="String" />
<asp:parameter Name="age" Type="Int16" />
<asp:parameter Name="title" Type="String" />
<asp:parameter Name="id" Type="Int64" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="id" Type="Int64" />
<asp:parameter Name="name" Type="String" />
<asp:parameter Name="age" Type="Int16" />
<asp:parameter Name="title" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<asp:DetailsView ID="DetailsView1" runat="server"
AllowPaging="True" AutoGenerateRows="False"
DataKeyNames="id" DataSourceID="SqlDataSource1" Height="50px"
Width="125px">
<Fields>
<asp:BoundField DataField="id" HeaderText="id"
ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="name"
SortExpression="name" />
<asp:BoundField DataField="age" HeaderText="age"
SortExpression="age" />
<asp:BoundField DataField="title" HeaderText="title"
SortExpression="title" />
</Fields>
</asp:DetailsView>
</div>

=-========code behind=======================


Partial Class VBNETViewPage
Inherits System.Web.UI.Page

Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DetailsView1.DataBound

Response.Write("<br/>DetailsView1_DataBound:")

Response.Write("<br/>key: " & DetailsView1.DataKey.Value)
Response.Write("<br/>name: " & DetailsView1.Rows(1).Cells(1).Text)
Response.Write("<br/>name: " & DetailsView1.Rows(2).Cells(1).Text)
Response.Write("<br/>name: " & DetailsView1.Rows(3).Cells(1).Text)

End Sub

Protected Sub DetailsView1_PageIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles DetailsView1.PageIndexChanged
Response.Write("<br/>DetailsView1_PageIndexChanged:")
End Sub
End Class

============================================

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
From: "Ed Dror" <[email protected]>
Subject: How to read from Details view
Date: Wed, 2 Apr 2008 13:29:20 -0700
Hi there,

I'm using asp.net 2.0 vb

And I wrote this

Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object,
ByVal
e
As System.EventArgs) Handles GridView1.SelectedIndexChanged
txtGNID.Text = Me.GridView1.SelectedRow.Cells(1).Text
End Sub

Every time when I slecet row my text box will show the selected cell value
(in this case is my VendorID)

Now I have a DetailsView

Protected Sub DetailsView1_PageIndexChanging(ByVal sender As Object,
ByVal
e
As System.Web.UI.WebControls.DetailsViewPageEventArgs) Handles
DetailsView1.PageIndexChanging

lblColorID.Text = ?

End Sub

What is the equivalent to the Grid view

I tried: lblColorID.Text = Me.DetailsView1.FindControl("ColorID").ID
but no luck

Thanks,

Ed Dror
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top