Access Programmatically created controls

Discussion in 'ASP .Net Web Controls' started by comp974@comp974.com, Sep 20, 2006.

  1. Guest

    ok, here is the situation, I am trying to construct a table with
    several ASP.net 2.0 controls in it during execution time in an VB.net
    enviroment. For starters, I have a textbox and a linkbutton per row of
    the table. I have easily constructed the table, but on the postback, I
    am unable to access the controls... Here is an example of the code:

    ''' <summary>Page Load</summary>
    ''' <remarks>
    ''' <para>Ensure that the querystring has a client ID or else it
    resends it to <see cref="Payment"/>.</para>
    ''' </remarks>
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.Load
    'If Request.QueryString(Navigation.CLIENT) Is Nothing Or
    Val(Request.QueryString(Navigation.CLIENT)) = 0 Then
    ' Dim Nav As New Navigation(Navigation.Webpages.WebPayment)
    ' Response.Redirect(Nav.toString)
    'End If
    hdnClientID.Value = 44
    'Val(Request.QueryString(Navigation.CLIENT))
    If Not Page.IsPostBack Then
    lblClientName.Text = New
    DataAccessLayer.Client(Val(hdnClientID.Value)).Client_Name
    End If
    ConfigureTable()
    End Sub

    ''' <summary>Redirect to Client Information</summary>
    ''' <remarks>
    ''' <para>Redirects to <see cref="Clients"/></para>
    ''' </remarks>
    Protected Sub lnkClient_Click(ByVal sender As System.Object, ByVal
    e As System.EventArgs) Handles lnkClient.Click
    Dim Nav As New Navigation(Navigation.Webpages.WebClient)
    Nav.ClientID = Val(hdnClientID.Value)
    Response.Redirect(Nav.toString)
    End Sub

    ''' <summary>Create Invoice Table</summary>
    ''' <remarks>
    ''' <para>construct an amount of rows to be determined by the
    number of invoices the client has.</para>
    ''' </remarks>
    Protected Sub ConfigureTable()
    Dim Data As IList(Of DataAccessLayer.Invoices) =
    DataAccessLayer.Invoices.getUnPaidInvoicesByClientID(Val(hdnClientID.Value))

    tblInvoices.Rows.Add(createHeaderRow)

    For Each Record As DataAccessLayer.Invoices In Data
    Dim Rows As New TableRow
    For Counter As Integer = 0 To 6
    Rows.Cells.Add(createCell(Counter, Record))
    Next
    Rows.EnableViewState = True
    tblInvoices.Rows.Add(Rows)
    Next
    End Sub

    ''' <summary>On Transfer Button Click</summary>
    ''' <remarks>
    ''' <para>When the user clicks the button to transfer the
    information to the other columns, this function grabs the row that was
    selected, and then takes it and applies it to the other labels.</para>
    ''' </remarks>
    Protected Sub TransferButton(ByVal sender As System.Object, ByVal e
    As System.EventArgs)
    Dim myButton As LinkButton = sender
    Dim txt As New TextBox
    txt.id = "txtApply" & myButton.CommandName
    MessageBox(txt.Text)
    End Sub

    ''' <summary>Construct the individual cells</summary>
    ''' <remarks>
    ''' <para>Determine what cell is being created using the CellID
    number and then return the constructed cell with the controls already
    connected.</para>
    ''' </remarks>
    Protected Function createCell(ByVal CellID As Integer, ByRef data
    As DataAccessLayer.Invoices) As TableCell
    Const CELL_WIDTH As Integer = 13
    Dim Cell As New TableCell

    Select Case CellID
    Case 0
    Dim lbl As New Label
    lbl.Text = data.Inv_Number
    lbl.BorderStyle = BorderStyle.Inset
    lbl.EnableViewState = True

    Cell.Width = Unit.Percentage(CELL_WIDTH)
    Cell.Controls.Add(lbl)
    Case 1
    Dim lbl As New Label
    lbl.Text = data.Inv_Date
    lbl.BorderStyle = BorderStyle.Inset
    lbl.EnableViewState = True
    Cell.Width = CELL_WIDTH
    Cell.Controls.Add(lbl)
    Case 2
    Dim lbl As New Label
    lbl.Text = data.Inv_Total - data.Inv_AmountPaid
    lbl.BorderStyle = BorderStyle.Inset
    lbl.EnableViewState = True
    Cell.Width = Unit.Percentage(CELL_WIDTH)
    Cell.Controls.Add(lbl)
    Case 3
    Dim txt As New TextBox
    txt.EnableViewState = True
    txt.ID = "txtApply" & data.Inv_ID
    Cell.Width = Unit.Percentage(CELL_WIDTH)
    Cell.Controls.Add(txt)
    Case 4
    Cell.Width = Unit.Percentage(CELL_WIDTH)
    Cell.Text = " "
    Case 5
    Dim Link As New LinkButton
    AddHandler Link.Click, AddressOf TransferButton
    Link.Text = "X"
    Link.CommandName = data.Inv_ID
    Link.EnableViewState = True
    Cell.EnableViewState = True
    Cell.Controls.Add(Link)
    Case 6
    Dim lbl As New Label
    lbl.Text = data.Inv_Number
    lbl.BorderStyle = BorderStyle.Inset
    lbl.EnableViewState = True
    Cell.Width = Unit.Percentage(CELL_WIDTH)
    Cell.Controls.Add(lbl)
    End Select

    Return Cell
    End Function

    ''' <summary>Create Header Row for the dynamic table</summary>
    ''' <remarks></remarks>
    Protected Function createHeaderRow() As TableHeaderRow
    Dim DataRow As New TableHeaderRow
    Dim DataCell As TableHeaderCell

    DataCell = New TableHeaderCell
    DataCell.Text = "Invoice Number"
    DataRow.Cells.Add(DataCell)
    DataCell = New TableHeaderCell
    DataCell.Text = "Date"
    DataRow.Cells.Add(DataCell)
    DataCell = New TableHeaderCell
    DataCell.Text = "Balance"
    DataRow.Cells.Add(DataCell)
    DataCell = New TableHeaderCell
    DataCell.Text = "Discount"
    DataRow.Cells.Add(DataCell)
    DataCell = New TableHeaderCell
    DataCell.Text = "Cash Applied"
    DataRow.Cells.Add(DataCell)
    DataCell = New TableHeaderCell
    DataRow.Cells.Add(DataCell)
    DataCell = New TableHeaderCell
    DataCell.Text = "Total Applied"
    DataRow.Cells.Add(DataCell)

    Return DataRow
    End Function


    For simplicity, Messagebox sends up a javascript alert with what ever
    is passed to it.

    Please Help, I have tried, page.findcontrol(), findcontrol(), creating
    a new textbox with the same id, tblInvoices.findcontrol(),
    cell.findcontrol() and all sorts of other idiotic steps to access this
    information. I know the answer is something simple, but it simply
    eludes me.

    Thank You in advance.
    Eric Forkrud
     
    , Sep 20, 2006
    #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. Rob Meade
    Replies:
    9
    Views:
    584
    Rob Meade
    Aug 10, 2004
  2. TB
    Replies:
    0
    Views:
    414
  3. Replies:
    0
    Views:
    418
  4. Ramesh
    Replies:
    1
    Views:
    468
    Ramesh
    Aug 17, 2006
  5. Replies:
    3
    Views:
    189
Loading...

Share This Page