ObjectDataSource & GridView for variable length input lists

Discussion in 'ASP .Net' started by =?Utf-8?B?Sm9obl9I?=, Jan 6, 2006.

  1. Re: ASP.NET 2.0
    I would like suggestions or code examples on how to collect a variable
    length list of input data (item# & item quantity specifically).

    I thought that I could accomplish this using a GridView that has ViewState
    enabled, an ObjectDataSource to process the submitted list, textboxes for
    getting new item data and an add button.

    Does this approach sound feasible or are there better alternatives?
    My problem is that I don't know yet how to make this happen.

    BTW, I intend to use the input list of item data in a web service request
    and then display the resultant web service response in another GridView.
    --
    Thanks, John_H
     
    =?Utf-8?B?Sm9obl9I?=, Jan 6, 2006
    #1
    1. Advertising

  2. I ended up using the following ASP.NET 2.0 code to implement the dynamic
    variable length collection of input items. The code behind code for this is
    also listed.
    I ended up not using the ObjectDataSource nor GridView, but I am using
    ViewState to preserve the state of the entered items between page postbacks.

    http://www.asp101.com/samples/form_dynamic_aspx.asp

    ....was also a helpful reference for this.
    ***
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
    Inherits="_Default" EnableViewState="true" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <span style="font-size: 16pt; font-family: Arial; text-decoration:
    underline">FRESTA.Net
    - Freight Estimator using the ABC Freight Calculator<br />
    </span>
    <br />
    <span style="font-size: 10pt; font-family: Arial">
    <asp:Table ID="Input_Header" runat="server" BorderWidth="2px"
    BorderColor="Gray" GridLines="Both">
    <asp:TableHeaderRow runat="server" ><asp:TableHeaderCell
    runat="server" ColumnSpan="4" HorizontalAlign="Left"
    BackColor="LightGray">Inputs:</asp:TableHeaderCell></asp:TableHeaderRow>
    <asp:TableRow runat="server">
    <asp:TableCell runat="server">Ship-from:</asp:TableCell>
    <asp:TableCell runat="server">
    <asp:DropDownList ID="DL_dc" runat="server"
    DataSourceID="SqlDataSource1"
    DataTextField="dc_name"
    DataValueField="dc_num_str"></asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:ABC_INTRANETConnectionString %>"
    SelectCommand="SELECT [dc_name], [dc_num_str] FROM
    [DistributionCenters]"></asp:SqlDataSource>
    </asp:TableCell>
    <asp:TableCell runat="server">Ship-to Zip
    Code:</asp:TableCell>
    <asp:TableCell runat="server">
    <asp:TextBox ID="TB_Zip" runat="server" MaxLength="5"
    Width="50"></asp:TextBox>
    &nbsp<asp:RegularExpressionValidator ID="RE_valZip"
    runat="server" ControlToValidate="TB_Zip" ErrorMessage="Invalid Zip Code"
    ValidationExpression="[0-9]{5}"></asp:RegularExpressionValidator>
    </asp:TableCell>
    </asp:TableRow>

    <asp:TableRow runat="server">
    <asp:TableCell ColumnSpan="2">&nbsp</asp:TableCell>
    <asp:TableCell runat="server">Address Type:</asp:TableCell>
    <asp:TableCell runat="server">
    <asp:DropDownList ID="DL_AdrTyp" runat="server">
    <asp:ListItem Value="R">Residence</asp:ListItem>
    <asp:ListItem Value="C">Business</asp:ListItem>
    </asp:DropDownList>

    </asp:TableCell>
    </asp:TableRow>

    <asp:TableRow runat="server">
    <asp:TableCell runat="server" ColumnSpan="2"
    VerticalAlign="Top">Ship Date:</asp:TableCell>
    <asp:TableCell runat="server" ColumnSpan="2">
    <asp:Calendar ID="Calendar1"
    runat="server"></asp:Calendar>
    </asp:TableCell>
    </asp:TableRow>

    <asp:TableRow runat="server">
    <asp:TableCell runat="server" ColumnSpan="2">Ship
    Time:</asp:TableCell>
    <asp:TableCell runat="server" ColumnSpan="2">
    <asp:DropDownList ID="DL_hours" runat="server">
    <asp:ListItem Value="1" Text="1"></asp:ListItem>
    <asp:ListItem Value="2" Text="2"></asp:ListItem>
    <asp:ListItem Value="3" Text="3"></asp:ListItem>
    <asp:ListItem Value="4" Text="4"></asp:ListItem>
    <asp:ListItem Value="5" Text="5"></asp:ListItem>
    <asp:ListItem Value="6" Text="6"></asp:ListItem>
    <asp:ListItem Value="7" Text="7"></asp:ListItem>
    <asp:ListItem Value="8" Text="8"></asp:ListItem>
    <asp:ListItem Value="9" Text="9"></asp:ListItem>
    <asp:ListItem Value="10" Text="10"></asp:ListItem>
    <asp:ListItem Value="11" Text="11"></asp:ListItem>
    <asp:ListItem Value="12" Text="12"></asp:ListItem>
    </asp:DropDownList>
    <asp:Label runat="server" Text="Label" Font-Bold="True"
    Font-Size="Larger">&nbsp:&nbsp</asp:Label>
    <asp:DropDownList ID="DL_minutes" runat="server">
    <asp:ListItem Value="0" Text="00"></asp:ListItem>
    <asp:ListItem Value="30" Text="30"></asp:ListItem>
    </asp:DropDownList>&nbsp
    <asp:DropDownList ID="DL_am_pm" runat="server">
    <asp:ListItem Value="0" Text="AM"></asp:ListItem>
    <asp:ListItem Value="1" Text="PM"></asp:ListItem>
    </asp:DropDownList>
    </asp:TableCell>
    </asp:TableRow>

    </asp:Table>

    <br />

    <asp:Table ID="Input_Detail" runat="server" BorderWidth="2px"
    BorderColor="Gray" GridLines="Both">
    <asp:TableHeaderRow runat="server"><asp:TableHeaderCell
    runat="server" ColumnSpan="5" HorizontalAlign="Left"
    BackColor="LightGray">Item Inputs:</asp:TableHeaderCell></asp:TableHeaderRow>
    <asp:TableHeaderRow runat="server">
    <asp:TableCell runat="server" Font-Underline="true">Item
    #</asp:TableCell>
    <asp:TableCell runat="server"
    Font-Underline="true">Quantity</asp:TableCell>
    <asp:TableCell runat="server">&nbsp</asp:TableCell>
    </asp:TableHeaderRow>

    <asp:TableRow runat="server">
    <asp:TableCell runat="server">
    <asp:TextBox ID="TB_item"
    runat="server"></asp:TextBox>
    </asp:TableCell>
    <asp:TableCell runat="server">
    <asp:TextBox ID="TB_qty" runat="server"></asp:TextBox>
    </asp:TableCell>
    <asp:TableCell runat="server">
    <asp:Button ID="Btn_add" runat="server" Text="Add
    New Item" OnCommand="btnAddItem_Click"/>
    </asp:TableCell>
    </asp:TableRow>

    </asp:Table>

    <asp:panel ID="pnlItemList" runat="Server">
    <asp:placeHolder ID="phText" runat="Server"></asp:placeHolder>
    </asp:panel>
    <asp:Table ID="ItemsTable" BorderStyle="Solid" BorderWidth="1"
    GridLines="Both" runat="Server">
    <asp:TableHeaderRow runat="server">
    <asp:TableHeaderCell Text=""></asp:TableHeaderCell>
    <asp:TableHeaderCell Text="Item #"></asp:TableHeaderCell>
    <asp:TableHeaderCell Text="Item
    Description"></asp:TableHeaderCell>
    <asp:TableHeaderCell
    Text="Quantity"></asp:TableHeaderCell>
    <asp:TableHeaderCell Text=""></asp:TableHeaderCell>
    </asp:TableHeaderRow>
    </asp:Table>
    <asp:Button ID="Btn_GetABCFreight" runat="server" Text="Get ABC
    Freight" OnCommand="btnGetABCFreight_Click" />
    </span>
    </div>
    </form>
    </body>
    </html>

    ***
    Imports System.Collections.Generic
    Imports System.Text.RegularExpressions
    Imports System.Configuration.ConnectionStringSettings
    Imports System.IO
    Imports System.Data.SqlClient
    Imports System.Web.UI.WebControls

    Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
    Handles MyBase.Load

    If Page.IsPostBack = True Then
    Dim inum As Integer
    Dim ilist As List(Of LineItem)

    inum = ViewState("ItemCount")

    If inum > 0 Then
    ilist = ViewState("ItemList") 'Get previously entered item
    list
    Call Load_ItemsTable(ilist, sender, e)
    End If
    End If

    End Sub '**Page_Init

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Handles MyBase.Load

    Dim dt_now As DateTime 'Current date & time
    Dim new_hour As Integer 'New hour value for ship time

    If Page.IsPostBack = False Then 'If 1st time page is posted

    dt_now = System.DateTime.Now

    '== Set selected date to current date
    Calendar1.SelectedDate = dt_now.Date

    '== Set the ship-time close to the current time
    new_hour = dt_now.Hour
    If dt_now.Minute > 0 And dt_now.Minute <= 30 Then
    DL_minutes.SelectedIndex = 1 'Set ship minutes default as 30
    Else
    DL_minutes.SelectedIndex = 0 'Set ship minutes default as 00
    new_hour += 1
    If new_hour > 23 Then
    new_hour = 0
    End If
    End If

    If new_hour >= 12 Then
    DL_am_pm.SelectedIndex = 1 'Set PM as select default
    new_hour -= 12
    Else
    DL_am_pm.SelectedIndex = 0 'Set AM as select default
    End If

    If new_hour = 0 Then
    DL_hours.SelectedIndex = 11
    Else
    DL_hours.SelectedIndex = new_hour - 1
    End If

    ViewState("ItemCount") = 0 'Set initial item count to
    zero
    End If

    'If sender.ToString.Substring(1, 13) = "btnRemoveItem" Then
    ' Call btnRemoveItem_Click(sender, e)
    'End If

    If ViewState("ItemCount") = 0 Then
    ItemsTable.Visible = False 'Hide the Items Table
    Btn_GetABCFreight.Visible = False 'Hide the Get ABC Freight
    button
    Else
    ItemsTable.Visible = True 'Display the Items Table
    Btn_GetABCFreight.Visible = False 'Display the Get ABC Freight
    button
    End If

    End Sub '**Page_Load

    Protected Sub btnAddItem_Click(ByVal sender As Object, ByVal e As
    CommandEventArgs)
    Dim inum As Integer
    Dim ilist As New List(Of LineItem)
    Dim idsc As String 'Item description
    Dim err_msg As String
    Dim bItm_Ok As Boolean
    Dim bQty_Ok As Boolean

    inum = ViewState("ItemCount") 'Get #items presviously entered
    If inum > 0 Then
    ilist = ViewState("ItemList") 'Get previously entered item list
    End If
    idsc = ""
    err_msg = ""

    bItm_Ok = Validate_Item(TB_item.Text, ilist, idsc, err_msg)
    bQty_Ok = Validate_Qty(TB_qty.Text)

    If Not bItm_Ok Or Not bQty_Ok Then
    If Not bItm_Ok Then
    If err_msg = "" Then
    err_msg = "Item # is invalid, please re-enter."
    End If
    End If
    If Not bQty_Ok Then
    err_msg = "Item Quantity is invalid, please re-enter."
    End If
    Call ShowMsg(err_msg) 'Display the error message
    Else
    inum += 1
    Dim item = New LineItem(TB_item.Text, idsc, CInt(TB_qty.Text))

    ilist.Add(item) 'Add item just entered to the item list
    Call Load_ItemsTableRow(inum, item, sender, e) 'Add new item
    to table

    TB_item.Text = "" 'Clear input item# textbox
    TB_qty.Text = "" 'Clear input quantity textbox
    End If

    'If ilist.Count <= 0 Then
    ' ItemsTable.Visible = False
    'Else
    ' ItemsTable.Visible = True
    'End If

    ViewState("ItemCount") = inum
    ViewState("ItemList") = ilist
    End Sub 'btnAddItem_Click

    Protected Sub Load_ItemsTable(ByVal ilist As List(Of LineItem), ByVal
    sender As Object, ByVal e As EventArgs)

    Dim i As Integer

    If ilist.Count > 0 Then
    For i = 0 To ilist.Count - 1 'Row loop - start
    Call Load_ItemsTableRow((i + 1), ilist.Item(i), sender, e)
    Next 'Row loop - end
    Else
    ItemsTable.Visible = False 'Hide ItemsTable if no items
    Btn_GetABCFreight.Visible = False 'Hide the Get ABC Freight
    button
    End If

    End Sub 'Load_ItemsTable

    Protected Sub Load_ItemsTableRow(ByVal inum As Integer, ByVal li As
    LineItem, ByVal sender As Object, ByVal e As EventArgs)
    Dim row As New TableRow
    Dim ic As Integer

    For ic = 0 To 4 'Cell loop - start
    Dim cell As New TableCell
    Select Case ic
    Case 0 'Sequence#
    cell.Text = CStr(inum) & ")" & "&nbsp"
    Case 1 'Item number
    cell.Text = li.ItemNum
    Case 2 'Item description
    cell.Text = li.ItemName
    Case 3 'Item quantity
    cell.Text = CStr(li.ItemQty)
    cell.HorizontalAlign = HorizontalAlign.Right
    Case 4 'Remove button
    Dim rBtn As New Button
    rBtn.Text = "Remove"
    rBtn.ID = "btnRemoveItem" & CStr(inum)
    rBtn.Attributes.Add("CommandName", "rBtn_" & CStr(inum))
    AddHandler rBtn.Click, AddressOf btnRemoveItem_Click
    cell.Controls.Add(rBtn)
    End Select
    row.Cells.Add(cell) ' Add next cell
    Next 'Cell loop - end
    ItemsTable.Rows.Add(row) 'Add next row
    ItemsTable.Visible = True 'Display the items in the
    ItemsTable
    Btn_GetABCFreight.Visible = True 'Display the Get ABC Freight
    button

    End Sub 'Load_ItemsTableRow

    Protected Sub btnRemoveItem_Click(ByVal sender As Object, ByVal e As
    EventArgs)

    Dim ir As Integer 'Item row# to remove
    Dim i As Integer 'Index
    Dim inum As Integer 'Item count from ViewState
    Dim it As Integer 'Rows in ItemsTable
    Dim ilist As New List(Of LineItem)
    Dim rBtn As Button
    Dim strID As String
    Dim strItem As String 'Item number string
    Dim re As New Regex("(\D+)(\d+)") 'Get 1 or more numeric digits, $2
    '--------------------($1 )($2 )----'(preceded by 1 or more
    non-numeric digits, $1)

    rBtn = sender
    strID = re.Replace(rBtn.ID, "$2") 'Get row/sequence number from
    button ID

    If IsNumeric(strID) Then
    ir = CInt(strID)
    Else
    ir = 0
    End If

    inum = ViewState("ItemCount") 'Get #items presviously entered
    If inum > 0 Then
    If ir > 0 And ir <= inum Then
    ilist = ViewState("ItemList") 'Get previously entered item
    list
    inum -= 1 'Decrement #items entered
    Else
    Exit Sub 'Exit, invalid item selected for
    removal
    End If
    Else
    Exit Sub 'Exit, no items in item table
    End If

    strItem = ilist.Item(ir - 1).ItemNum 'Get the item number being
    removed
    ilist.RemoveAt(ir - 1) 'Remove selected item from the item
    list

    it = ItemsTable.Rows.Count - 1 'Count of non-header rows
    For i = 1 To it 'Clear non-header rows
    ItemsTable.Rows.RemoveAt(1) 'Delete 1st row, remaining rows
    bubble up
    Next
    Call Load_ItemsTable(ilist, sender, e) 'Re-Load the ItemsTable
    'Note: re-load is done to re-number the sequence numbers in column #1

    ViewState("ItemCount") = inum
    ViewState("ItemList") = ilist

    Call ShowMsg("Item " & strItem & " was removed.")

    End Sub 'btnRemoveItem_Click


    Protected Function Validate_Item(ByVal item As String, ByVal ilist As
    List(Of LineItem), ByRef name As String, ByRef err As String) As Boolean

    name = ""
    err = ""
    If item = Nothing Then
    Return False
    End If
    If item = "" Then
    Return False
    End If

    Dim i As Integer

    If ilist.Count > 0 Then
    For i = 0 To ilist.Count - 1
    If ilist.Item(i).ItemNum = item Then
    err = "This item (" & item & ") has already been added
    to the item list."
    Return False
    End If
    Next
    End If

    Dim dbConn As String
    dbConn =
    ConfigurationManager.ConnectionStrings("abc_data_filesConnectionString").ConnectionString
    Dim selStr As String
    Dim sqlConn As New SqlConnection(dbConn)
    Dim sqlCmd As SqlCommand
    Dim sqlRdr As SqlDataReader
    Dim obsDate As Date
    Dim obsFlag As Char

    Try
    sqlConn.Open()
    Catch ex As Exception
    err = "SQL connection error: |" & ex.Message & "|, on " & dbConn
    Return False
    End Try

    '** Get the item's description, additional description, obsolete
    flag, and obsolete date
    selStr = "SELECT DISTINCT itmdsc,itmdsa,itmobs,itmobd FROM
    dai_itmmst WHERE itmnmb = '" & item & "'"
    sqlCmd = New SqlCommand(selStr, sqlConn)

    Try
    sqlRdr = sqlCmd.ExecuteReader
    Do While sqlRdr.Read()
    obsFlag = sqlRdr.Item("itmobs")
    If obsFlag = "Y" Then
    obsDate = CDate(sqlRdr.Item("itmobd"))
    If obsDate <= Now.Date Then
    err = "Entered item (" & item & ") is obsolete."
    Exit Do
    End If
    End If
    name = sqlRdr.Item("itmdsc") & sqlRdr.Item("itmdsa")
    Exit Do
    Loop
    Catch ex As Exception
    name = ""
    err = "SQL Select error: |" & ex.Message & "|."
    End Try

    sqlConn.Close()
    If err = "" Then
    Return True
    Else
    Return False
    End If
    End Function 'Validate_Item

    Protected Function Validate_Qty(ByVal qty As String) As Boolean
    If qty = Nothing Then
    Return False
    End If
    If qty = "" Then
    Return False
    End If
    If Not Regex.IsMatch(qty, "\d{1,3}") Then 'If Not in range 0-999
    Then
    Return False
    End If
    If CInt(qty) <= 0 Then
    Return False
    End If
    Return True 'Valid quantity input
    End Function 'Validate_Qty

    Protected Sub ShowMsg(ByVal msg As String)
    Dim lblText As New Label

    lblText.Text = msg & "<BR />"
    phText.Controls.Add(lblText)
    phText.Visible = True

    End Sub 'ShowMsg

    Protected Sub btnGetABCFreight_Click(ByVal sender As Object, ByVal e As
    CommandEventArgs)

    End Sub 'btnGetABCFreight_Click

    End Class '_Default

    --

    - John_H


    "John_H" wrote:

    > Re: ASP.NET 2.0
    > I would like suggestions or code examples on how to collect a variable
    > length list of input data (item# & item quantity specifically).
    >
    > I thought that I could accomplish this using a GridView that has ViewState
    > enabled, an ObjectDataSource to process the submitted list, textboxes for
    > getting new item data and an add button.
    >
    > Does this approach sound feasible or are there better alternatives?
    > My problem is that I don't know yet how to make this happen.
    >
    > BTW, I intend to use the input list of item data in a web service request
    > and then display the resultant web service response in another GridView.
    > --
    > Thanks, John_H
     
    =?Utf-8?B?Sm9obl9I?=, Jan 19, 2006
    #2
    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. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    448
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  2. Jonathan Burd

    Help with Variable-Length Argument Lists

    Jonathan Burd, Dec 14, 2004, in forum: C Programming
    Replies:
    5
    Views:
    383
    Keith Thompson
    Dec 15, 2004
  3. Tomás
    Replies:
    3
    Views:
    384
  4. Spencer Pearson

    Tuples vs. variable-length argument lists

    Spencer Pearson, Mar 20, 2010, in forum: Python
    Replies:
    2
    Views:
    485
    Jean-Michel Pichavant
    Mar 22, 2010
  5. Libra
    Replies:
    9
    Views:
    240
Loading...

Share This Page