Good Morning,
I am developing a quote request functionality as part of a website. The idea here is for the user to search for parts, update the textbox field in the first gridview for items they wish to select, click Add to Quote embedded in the first gridview, and send those rows to a second gridview wherein they can edit quantities or remove rows completely. The second gridview will then need to be encapsulated in a Wizard Control or something similar (on different page?) where in contact information is supplied and all data saved to the quote table in the database.
The issue I'm having is that the data is not being passed from the first gridview to the second. I'm new to data access controls, and have read that I needed to use a data table to store the results so they can be sent to the second gridview. Here's a snippet of my ASPX page:
<div id="searchbox">
<fieldset id="Fieldset1">
<asp:Label runat="server" ID="lblPartName" Text="Search by Part Name"></asp:Label>
<asp:TextBox runat="server" ID="txtSearch"></asp:TextBox>
<asp:Label runat="server" ID="lblNSN" Text="Search by NSN"></asp:Label>
<asp:TextBox runat="server" ID="txtNSN"> </asp:TextBox>
<asp:Button runat="server" ID="btnSubmit" Text="Search" />
</fieldset>
<asp:Label runat="server" ID="lblItems" Text="Selected Parts"></asp:Label>
<asp:GridView runat="server" ID="gvItems" AutoGenerateColumns="False">
</asp:GridView>
</div>
<div id="results">
<asp:GridView ID="MySearch" runat="server"
AllowSorting="True" AutoGenerateColumns="False"
AllowPaging="True" DataSourceID="DSParts" ShowFooter="True"
DataKeyNames="PARTNUMBER,NSN,PARTNAME">
<Columns>
<asp:BoundField DataField="PARTNUMBER" HeaderText="PARTNUMBER"
SortExpression="PARTNUMBER" />
<asp:BoundField DataField="NSN" HeaderText="NSN" SortExpression="NSN" />
<asp:BoundField DataField="PARTNAME" HeaderText="PARTNAME"
SortExpression="PARTNAME" />
<asp:TemplateField HeaderText="Qty">
<ItemTemplate >
<asp:TextBox runat="server" ID="txtQty"></asp:TextBox></ItemTemplate>
<FooterTemplate>
<asp:Button runat="server" Text="Add to Quote" ID="btnAddQuote" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource runat="server" ID="DSParts"
ConnectionString="<%$ ConnectionStrings:Diel_inventoryConnectionString %>"
SelectCommand="PartSproc" SelectCommandType="StoredProcedure"
CancelSelectOnNullParameter="False">
<SelectParameters>
<asp:ControlParameter ControlID="txtNSN" Name="NSN" PropertyName="Text"
Type="String" />
<asp:ControlParameter ControlID="txtSearch" Name="PartName" PropertyName="Text"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</div>
Here's the contents of my codebehind file:
Imports MyApp.WebControls.SearchGridView
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Partial Class PartSearch
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not (IsPostBack) Then
'Define DataTable Columns as incoming gridview fields
Dim dtSelParts As DataTable = New DataTable
Dim dr As DataRow = dtSelParts.NewRow()
Dim gvr As GridViewRow
dtSelParts.Columns.Add("PartNumber")
dtSelParts.Columns.Add("NSN")
dtSelParts.Columns.Add("PartName")
dtSelParts.Columns.Add("Qty")
'Select those gridview rows that have txtQty <> 0
For Each row As GridViewRow In MySearch.Rows
Dim textboxText As String = _
CType(row.FindControl("txtQty"), TextBox).Text
If textboxText <> "0" Then
'Create the row
dr = dtSelParts.NewRow()
'Fill the row with data
dr("PartNumber") = MySearch.DataKeys(row.RowIndex)("PartNumber")
dr("NSN") = MySearch.DataKeys(row.RowIndex)("NSN")
dr("PartName") = MySearch.DataKeys(row.RowIndex)("PartName")
dr("Qty") = MySearch.DataKeys(row.RowIndex)("Qty")
'Add the row to the table
dtSelParts.Rows.Add(dr)
End If
Next
'Need to send items to Gridview control gvItems
For Each gvr In gvItems.Rows
dr = dtSelParts.NewRow()
dr("PartNumber") = gvr.Cells(0).Text
dr("NSN") = gvr.Cells(1).Text
dr("PartName") = gvr.Cells(2).Text
dr("Qty") = gvr.Cells(3).Text
dtSelParts.Rows.Add(dr)
Next
End If
End Sub
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
MySearch.DataBind()
End Sub
Questions:
1. Please help me understand why the second gridview gvItems is not receiving the selected rows from gridview MySearch. What needs to be corrected/added to make this work?
2. Is there another "best practice" for this type of process? That is, am I making correct use of a second gridview to store selected items, or is there another control that works better/more efficiently for this task?
3. Given that the customer needs to provide their contact information etc. as part of the quote process, should gvItems be encapsulated in a Wizard Control as step 1? Should it stay on the same page as gridview MySearch or should it be on a page of it's own?
4. Please send links/samples of similar quote request processes that you have done and are willing to share.
5. The code has been placed in Page_Load, should it be moved to the click event of btnAddQuote instead?
6. If I need to move the code to the click event of btnAddQuote, should I still wrap it in If Not (isPostBock) then block?
Thanks much, Sid