DetailsView - accessing fields

Discussion in 'ASP .Net Web Controls' started by C Silva, Nov 15, 2004.

  1. C Silva

    C Silva Guest

    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>
     
    C Silva, Nov 15, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve
    Replies:
    0
    Views:
    570
    Steve
    Sep 19, 2005
  2. Replies:
    7
    Views:
    3,142
  3. =?Utf-8?B?UXVhbiBOZ3V5ZW4=?=

    Databind problem with DetailsView's template fields

    =?Utf-8?B?UXVhbiBOZ3V5ZW4=?=, Aug 27, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    541
    =?Utf-8?B?UXVhbiBOZ3V5ZW4=?=
    Aug 27, 2007
  4. gerry
    Replies:
    3
    Views:
    2,046
    Walter Wang [MSFT]
    Sep 26, 2007
  5. Feege
    Replies:
    0
    Views:
    480
    Feege
    Dec 20, 2005
Loading...

Share This Page