DetailsView - accessing fields

C

C Silva

I would like to know how can I get a value from a DetailsView field when it
is in Edit or Insert mode.

I have this code that works fine when it is in ReadOnly mode. If the
DefaultMode of the DetailsView is set to “Editâ€, it will recover no value
from the DetailsView field.

Can somebody please give any suggestion to accomplish this? Thanks, Cloves
Silva.

<%@ Page language="VB" %>
<script runat="server">
Sub StoresDetailView_ItemCommand(ByVal sender As Object, ByVal e As
DetailsViewCommandEventArgs)
' Use the CommandName property to determine which button
' was clicked.

If e.CommandName = "Add" Then
' Add the current store to the contact list.
' Get the row that contains the store name. In this
' example, the store name is in the second row (index 1)
' of the DetailsView control.

Dim row As DetailsViewRow = StoresDetailView.Rows(1)

' Get the store's name from the appropriate cell.
' In this example, the store name is in the second cell
' (index 1) of the row.

Dim name As String = row.Cells(1).Text

' Create a ListItem object with the store's name.
Dim item As New ListItem(name)

' Add the ListItem object to the ListBox, if the
' item doesn't already exist.

If Not ContactListBox.Items.Contains(item) Then
ContactListBox.Items.Add(item)
End If
End If
End Sub
</script>

<html>
<body>
<form id="Form1" runat="server">
<h3>DetailsView ItemCommand Example</h3>
<asp:detailsview id="StoresDetailView"
datasourceid="StoresDetailsSqlDataSource"
autogeneraterows="false"
datakeynames="stor_id"
allowpaging="true"
onitemcommand="StoresDetailView_ItemCommand"
runat="server">
<Fields>
<asp:boundfield datafield="stor_id" headertext="Store ID"/>
<asp:boundfield datafield="stor_name" headertext="Store Name"/>
<asp:boundfield datafield="city" headertext="City"/>
<asp:ButtonField CommandName="Add" Text="Add Contact"/>
</fields>
</asp:detailsview>
<hr/>

Contacts:<br/>
<asp:ListBox id="ContactListBox" runat="server"/>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Pubs sample database. -->
<asp:sqldatasource id="StoresDetailsSqlDataSource"

selectcommand="SELECT [stor_id], [stor_name], [stor_address],
[city], [state], [zip] FROM [stores]"

connectionstring="server=localhost;database=pubs;integrated
security=SSPI"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top