Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Just a simple sample

Status
Not open for further replies.

bont

Programmer
Sep 7, 2000
200
US
I have been look desparately for a code behind example which dynamically generates controls via a separate class. I have been able to do some of it; however, I don't understand how to initiate my class to work during the life of the form, as what I would like to do is have a series of controls that are optional, and allow a user to "add another row of control options" via a button press. Eventually he will hit the submit button and a complex query will be played on a DB built from the possible many rows of controls.

Anyone have any examples / tutorials on this? I guess just initialization of a class for a webform's lifespan (with re-initializing it) would aid me as well.
 
I think this code was from this forum. Anyway, here is a sample:

Code:
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
        'This sub will build the table and put the controls in the table
        LoadFormUI()
    End Sub

and here is the LoadFormUI subroutine called from the page_init sub....

Code:
Private Sub LoadFormUI()
'Dim all the stuff we need for the table of controls
        Dim x As Integer
        Dim Formtable As New Table
        Dim HeadingRow As New TableRow
        Dim HeadingCell1 As New TableHeaderCell
        Dim HeadingCell2 As New TableHeaderCell
        Dim HeadingCell3 As New TableHeaderCell
        Dim HeadingCell4 As New TableHeaderCell
        Dim HeadingCell5 As New TableHeaderCell
        Dim HeadingCell6 As New TableHeaderCell
        Dim HeadingCell7 As New TableHeaderCell

        'Set the table border and name
        Formtable.BorderWidth = WebControls.Unit.Pixel(1)
        Formtable.ID = "Batchtable"

        'add the table to the placeholder
        phTable.Controls.Add(Formtable)

        'set the text in the cell heading controls
        HeadingCell1.Text = "Invoice #"
        HeadingCell2.Text = "Incentive"
        HeadingCell3.Text = "Quantity"
        HeadingCell4.Text = "Total Pay"
        
        'Add the headingrow control to the table that was addedd to the placeholder control
        Formtable.Controls.Add(HeadingRow)

        'Now add the column heading controls to the headingrow control
        HeadingRow.Controls.Add(HeadingCell1)
        HeadingRow.Controls.Add(HeadingCell2)
        HeadingRow.Controls.Add(HeadingCell3)
        HeadingRow.Controls.Add(HeadingCell4)

        'this for-next loop will create 40 rows with the controls for each row
        For x = 0 To 40
            'Create a new tablerow control
            Dim FormRow As New TableRow
            'create the cell controls for the tablerow control
            Dim Cell1 As New TableCell
            Dim Cell2 As New TableCell
            Dim cell3 As New TableCell
            Dim cell4 As New TableCell
           
            'create textbox controls for the tablecells
            Dim txtCol1 As New TextBox              'Invoice#
            Dim txtCol2 As New eWorld.UI.NumericBox 'Incentive
            Dim txtCol3 As New eWorld.UI.NumericBox 'Quantity
            Dim txtCol4 As New eWorld.UI.NumericBox 'Total Pay
            'eWorld.UI.Numericbox is a custom serverside control that only allows numeric values and currency values, it does not allow for any alpha text. This control is propietary, and belongs to [URL unfurl="true"]http://www.eworldui.net/[/URL] the control was provided freely at the time we downloaded the control, as it turns out the control may not be free in the future, we hope so. 
            
            'Set the Row border and color
            FormRow.BorderWidth = WebControls.Unit.Pixel(3)
            FormRow.BorderColor = Color.Black
            'add the new Tablerow to the table
            Formtable.Controls.Add(FormRow)
            'add the textbox controls to the newly added tablerow
            FormRow.Controls.Add(Cell1)
            FormRow.Controls.Add(Cell2)
            FormRow.Controls.Add(cell3)
            FormRow.Controls.Add(cell4)
           

            'set the textbox control ID's so we can reference them later in code we use the format controlname_rownumber. ie txtCol1_1 = textbox in column 1 row 1
            txtCol1.ID = "txtCol1_" & x.ToString : txtCol1.Columns = 8
            txtCol2.ID = "txtCol2_" & x.ToString : txtCol2.Columns = 7 : txtCol2.DecimalPlaces = 2 : txtCol2.AutoFormatCurrency = True
            txtCol3.ID = "txtCol3_" & x.ToString : txtCol3.Columns = 10
            txtCol4.ID = "txtCol4_" & x.ToString : txtCol4.Columns = 7 : txtCol4.DecimalPlaces = 2 : txtCol4.AutoFormatCurrency = True
           
            'now add the textbox controls to each of the cells in the new tablerow
            Cell1.Controls.Add(txtCol1)
            Cell2.Controls.Add(txtCol2)
            cell3.Controls.Add(txtCol3)
            cell4.Controls.Add(txtCol4)
           
            ' This adds a handler to the form so we can trap the textchanged event of the textbox in column 2
            'AddHandler txtCol2.TextChanged, AddressOf txtCol2_TextChanged

        Next

    End Sub

________________
Hope you find it useful.

________________
Put your code in a button's click event. To handle and know what the new series' controls names will be (also the top value where the new series will start), use session variables.
 
This is on the right track, but not quite the architecture I am looking for.

I would like define a completely separate class

MyClass
{
- AddButton
- RemoveButton
- etc
}

My problem is that I can't figure out how to declare it in the webform, so that I can:

A) Only have to declare MyClass once, as I don't want to lose any of the non-permament settings

B) Allow the user to add a row one at a time.

I have found examples where I can pass the number of rows to create; however, I can only count on 1 row at a time, each time I would need to keep the previous row's settings as well as those in MyClass.

 
>> My problem is that I can't figure out how to declare it in the webform

-- Just a class in the app_code or whatever folder. Create your subs, functions etc.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top