Link to home
Start Free TrialLog in
Avatar of NorthReno
NorthRenoFlag for United States of America

asked on

How do I extract data from a DetailsView control so I can pass it do a data layer?

I am using a DetailsView control on an ASPX page.  I have designed it so the user can enter 14 different fields, including three dropdown lists.  I cannot bind it directly to a database or even provide it with SQL code that might be passed via ADO or something.

We have a separate data layer where we are to provide all the information we wish to insert into a DB2 table.  It is passed to a COBOL program, which in turn accesses the DB2 database using SQL.

I am totally new to the DetailsView control.  I have become intimate with the GridView control - using it to display data retrieved from the database, allowing the user to update it or delete it, and then passing the appropriate data and commands to the above mentioned data layer.

I assumed I could create the DetailsView interface on the ASPX page, it would captue the data entered into it.  Then I could process its object in code behind... but I am getting nowhere in locating the data in the detailsview object.

Please review the code snippits and comments below and advise.  Thanks in advance!!!!!!
<!-- This is the complete DetailsView (names changed) -->        
 
<asp:DetailsView ID="dtvReceiptsEntry" 
                         runat="server" 
                         Enabled="false"
                         AutoGenerateRows="false"
                         Height="33px" 
                         Width="35%"
                         DefaultMode="Insert"
                         BackColor="#DEBA84" 
                         BorderColor="#DEBA84" 
                         BorderStyle="None" 
                         BorderWidth="1px" 
                         CellPadding="3" 
                         CellSpacing="2" GridLines="Both">
 
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <PagerStyle ForeColor="#8C4510"  />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" HorizontalAlign="Left"  />
            <Fields>
                <asp:TemplateField HeaderText="Customer Number">
                    <InsertItemTemplate>
                        <asp:TextBox ID="txtCustomerNumber"   
					                 runat="server" 
					                 Width="80px">
					    </asp:TextBox>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Customer Name">
                    <InsertItemTemplate>
                        <asp:TextBox ID="txtCustomerName" 
                                     runat="server"
                                     width="150px">
                        </asp:TextBox>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Mode Type">
                    <InsertItemTemplate>
                       <asp:DropDownList ID="ddlModeType" 
                                          runat="server" 
                                          DataSourceID="odsuf_rtdata_mfmo"
                                          DataTextField="data_txt" 
                                          DataValueField="key_txt"  
                                          SelectedValue='<%# Eval ("MODE_TY") %>' 
					                      Width="100px" 
                                          CssClass="Bod8">
                        </asp:DropDownList>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Origin City">
                    <InsertItemTemplate>
                        <asp:TextBox ID="txtOrigCity" 
					                 runat="server" 
					                 Width="100px">
					    </asp:TextBox>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Origin State">
                    <InsertItemTemplate>
                        <asp:DropDownList ID="ddlOrigState" 
			                              runat="server" 
			                              Width="100px"  
			                              DatasourceID="odsuf_rtdata_gsta" 
			                              DataTextField="data_txt" 
			                              DataValueField="key_txt" 
			                              SelectedValue='<%# Eval("Orig State") %>'>
			            </asp:DropDownList>
 
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Dest City">
                    <InsertItemTemplate>
                        <asp:TextBox ID="txtDestCity"    
					                 runat="server" 
					                 Width="100px">
					    </asp:TextBox>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Dest State">
                    <InsertItemTemplate>
                        <asp:DropDownList ID="ddlDestState" 
					                      runat="server" 
					                      Width="100px"  
					                      DatasourceID="odsuf_rtdata_gsta" 
						                  DataTextField="data_txt" 
						                  DataValueField="key_txt" 
						                  SelectedValue='<%# Eval("Dest State") %>'>
						</asp:DropDownList>
 
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Seller's Number">
                    <InsertItemTemplate>
                        <asp:TextBox ID="txtSellerNumber" 
					                 runat="server" 
					                 Width="80px">
					    </asp:TextBox>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Received From">
                    <InsertItemTemplate>
                        <asp:TextBox ID="txtRcvdFrom" 
					                 runat="server" 
					                 Width="100px">
					    </asp:TextBox>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Date Received">
                    <InsertItemTemplate>
                        <asp:TextBox ID="txtDateRcvd" 
					                 runat="server" 
					                 Width="80px">
					    </asp:TextBox>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Manifest Number">
                    <InsertItemTemplate>
                        <asp:TextBox ID="txtManifest" 
					                 runat="server" 
					                 Width="100px">
					    </asp:TextBox>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Net Product">
                    <InsertItemTemplate>
                        <asp:TextBox ID="txtNetProduct" 
					                 runat="server" 
					                 Width="80px">
					    </asp:TextBox>                    
					</InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Gross Product">
                    <InsertItemTemplate>
                        <asp:TextBox ID="txtGrossProduct" 
					                 runat="server" 
					                 Width="80px">
					    </asp:TextBox>
                    </InsertItemTemplate>                
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Billed Product">
                    <InsertItemTemplate>
                        <asp:TextBox ID="txtBillProduct" 
					                 runat="server" 
					                 Width="80px">
					    </asp:TextBox>
                    </InsertItemTemplate>                
                </asp:TemplateField>
            </Fields>
 
        </asp:DetailsView>
 
 
------------------------- VB 2005 Code Behind -------------------------
 
 
This is where I am really stuck!  I don't know what methodology to access what was entered.  The data entry screen works like a champ.  Just before it is displayed, this code takes place:
 
    Private Sub addColumnHeaders()
        Dim dtbColumnHeaders = New DataTable
        Dim drBlankRows As DataRow
        Dim i As Integer = 0
 
        dtbColumnHeaders.Columns.Add(New DataColumn("Customer Number", GetType(String)))
        dtbColumnHeaders.Columns.Add(New DataColumn("Customer Name", GetType(String)))
        dtbColumnHeaders.Columns.Add(New DataColumn("Mode Type", GetType(String)))
        dtbColumnHeaders.Columns.Add(New DataColumn("Orig City", GetType(String)))
        dtbColumnHeaders.Columns.Add(New DataColumn("Orig State", GetType(String)))
        dtbColumnHeaders.Columns.Add(New DataColumn("Dest City", GetType(String)))
        dtbColumnHeaders.Columns.Add(New DataColumn("Dest State", GetType(String)))
        dtbColumnHeaders.Columns.Add(New DataColumn("Seller's Number", GetType(String)))
        dtbColumnHeaders.Columns.Add(New DataColumn("Received From", GetType(String)))
        dtbColumnHeaders.Columns.Add(New DataColumn("Date Received", GetType(String)))
        dtbColumnHeaders.Columns.Add(New DataColumn("Manifest", GetType(String)))
        dtbColumnHeaders.Columns.Add(New DataColumn("Net Product", GetType(String)))
        dtbColumnHeaders.Columns.Add(New DataColumn("Gross Product", GetType(String)))
        dtbColumnHeaders.Columns.Add(New DataColumn("Billed Product", GetType(String)))
 
     
        drBlankRows = dtbColumnHeaders.NewRow()
        dtbColumnHeaders.Rows.Add(drBlankRows)
 
 
        Session("dtColumnHeaders") = dtbColumnHeaders
 
    End Sub
 
 
Then when the user clicks the Save button after entering the data...
 
  Private Sub btn_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_save.Click
 
        Try
            dtvReceiptsEntry.DataBind()
 
' I have tried a number of things here.  I will list a couple of 
' examples
 
            Dim txtCustomerNumber As TextBox = CType(dtvReceiptsEntry.Rows(0).FindControl("txtCustomerNumber"), TextBox)
            Dim txtCustomerName As TextBox = CType(dtvReceiptsEntry.Rows(0).FindControl("txtCustomerName"), TextBox)
            Dim ddlModeType As DropDownList = CType(dtvReceiptsEntry.Rows(0).FindControl("ddlModeType"), DropDownList)
            Dim txtOrigCity As TextBox = CType(dtvReceiptsEntry.Rows(0).FindControl("txtOrigCity"), TextBox)
            Dim ddlOrigState As DropDownList = CType(dtvReceiptsEntry.Rows(0).FindControl("ddlOrigState"), DropDownList)
            Dim txtDestCity As TextBox = CType(dtvReceiptsEntry.Rows(0).FindControl("txtDestCity"), TextBox)
            Dim ddlDestState As DropDownList = CType(dtvReceiptsEntry.Rows(0).FindControl("ddlDestState"), DropDownList)
            Dim txtSellerNumber As TextBox = CType(dtvReceiptsEntry.Rows(0).FindControl("txtSellerNumber"), TextBox)
            Dim txtRcvdFrom As TextBox = CType(dtvReceiptsEntry.Rows(0).FindControl("txtRcvdFrom"), TextBox)
            Dim txtDateRcvd As TextBox = CType(dtvReceiptsEntry.Rows(0).FindControl("txtDateRcvd"), TextBox)
            Dim txtManifest As TextBox = CType(dtvReceiptsEntry.Rows(0).FindControl("txtManifest"), TextBox)
            Dim txtNetProduct As TextBox = CType(dtvReceiptsEntry.Rows(0).FindControl("txtNetProduct"), TextBox)
            Dim txtGrossProduct As TextBox = CType(dtvReceiptsEntry.Rows(0).FindControl("txtGrossProduct"), TextBox)
            Dim txtBillProduct As TextBox = CType(dtvReceiptsEntry.Rows(0).FindControl("txtBillProduct"), TextBox)        
 
' The above code does not get errors, but there is no data in those locations either.
 
Without going into code detail, I tried accessing DataKeys collection as well as the DataItem collection.  I futzed with a few others, but I am blundering in the dark at this point.
 
Perhaps the object is not instantiated correctly... the state of the data is out of scope... I am in real need of help.

Open in new window

Avatar of skiltz
skiltz
Flag of New Zealand image

MsgBox(CType(dtvReceiptsEntry.Controls(0).FindControl("txtCustomerName"), TextBox).Text)
Avatar of NorthReno

ASKER

'MsgBox(CType(dtvReceiptsEntry.Controls(0).FindControl("txtCustomerName"), TextBox).Text)"

No, that does not contain the data... any other ideas?

ASKER CERTIFIED SOLUTION
Avatar of skiltz
skiltz
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
or maybe try 'MsgBox(CType(dtvReceiptsEntry.Items(0).FindControl("txtCustomerName"), TextBox).Text)"
Thanks for your input.  I made a discovery earlier.  All I had to do was to change my index from 0 to 1 on the rows collection, and it worked great.  I was using the correct collection all along.
skiltz:  I gave you the points anyway.  I really appreciated the time you took to at least make an effort.  No one else offered anything.