how to reach a control in a dynamically extended HTML Table

Discussion in 'ASP .Net Web Controls' started by buran, Dec 20, 2004.

  1. buran

    buran Guest

    Dear ASP.NET Programmers,

    I have a HTML table (running as server control with the control ID:
    tblInsertSP). The table has 16 rows with textboxes. Depending on the value
    of the ddlSPType, which is a dropdownlist control on the page, I add
    dynamically extra rows to the table. For instance, if the ddlSPType selected
    item is Aviation Company, an extra row containing a cell with the textbox
    control txtAircrafts is added to the HTML table tblInsertSP (which now has
    17 controls). I use the following code:

    Dim k, totalRowCount as Int16

    Select Case ddlSPType.SelectedValue

    Case 1

    Dim tRow As New HtmlTableRow
    For k = 0 to To 2
    tRow.Cells.Add(New HtmlTableCell)
    Next

    totalRowCount = tblInsertSP.Rows.Count

    tblInsertSP.Rows(totalRowCount - 1).Cells(0).InnerText = "Aircrafts"

    Dim txtAircrafts As New TextBox
    tblInsertSP.Rows(totalRowCount -
    1).Cells(1).Controls.Add(txtAircrafts)

    Case 2

    ....

    End Select


    After adding extra rows dynamically, I want to save the entered information
    into the database tables, SP and SPAviationCompany (the latter is used for
    specific data of the SP, namely Aircrafts). I have to reach the txtAircrafts
    control in the dynamically added row. I tried it with the following code but
    it did not work:


    Dim str As String
    str = CType(Page.FindControl("tblInsertSP").FindControl("txtAircrafts"),
    TextBox).Text

    I receive an error stating that there's no such an object. How can I reach
    this object? Thanks in advance.

    Burak
     
    buran, Dec 20, 2004
    #1
    1. Advertising

  2. Try with this

    Dim str As String
    str = CType(Page.FindControl("txtAircrafts"),TextBox).Text

    I hope that helps you,

    Atte. Elmer Carías
    El Salvador, CA



    "buran" <> wrote in message
    news:...
    > Dear ASP.NET Programmers,
    >
    > I have a HTML table (running as server control with the control ID:
    > tblInsertSP). The table has 16 rows with textboxes. Depending on the value
    > of the ddlSPType, which is a dropdownlist control on the page, I add
    > dynamically extra rows to the table. For instance, if the ddlSPType

    selected
    > item is Aviation Company, an extra row containing a cell with the textbox
    > control txtAircrafts is added to the HTML table tblInsertSP (which now has
    > 17 controls). I use the following code:
    >
    > Dim k, totalRowCount as Int16
    >
    > Select Case ddlSPType.SelectedValue
    >
    > Case 1
    >
    > Dim tRow As New HtmlTableRow
    > For k = 0 to To 2
    > tRow.Cells.Add(New HtmlTableCell)
    > Next
    >
    > totalRowCount = tblInsertSP.Rows.Count
    >
    > tblInsertSP.Rows(totalRowCount - 1).Cells(0).InnerText =

    "Aircrafts"
    >
    > Dim txtAircrafts As New TextBox
    > tblInsertSP.Rows(totalRowCount -
    > 1).Cells(1).Controls.Add(txtAircrafts)
    >
    > Case 2
    >
    > ....
    >
    > End Select
    >
    >
    > After adding extra rows dynamically, I want to save the entered

    information
    > into the database tables, SP and SPAviationCompany (the latter is used for
    > specific data of the SP, namely Aircrafts). I have to reach the

    txtAircrafts
    > control in the dynamically added row. I tried it with the following code

    but
    > it did not work:
    >
    >
    > Dim str As String
    > str = CType(Page.FindControl("tblInsertSP").FindControl("txtAircrafts"),
    > TextBox).Text
    >
    > I receive an error stating that there's no such an object. How can I reach
    > this object? Thanks in advance.
    >
    > Burak
    >
    >
    >
    >
    >
    >
    >
    >
    >
     
    Elmer Carías, Dec 20, 2004
    #2
    1. Advertising

  3. "buran" <> wrote in message
    news:...
    > Dear ASP.NET Programmers,
    >
    > I have a HTML table (running as server control with the control ID:
    > tblInsertSP). The table has 16 rows with textboxes. Depending on the value
    > of the ddlSPType, which is a dropdownlist control on the page, I add
    > dynamically extra rows to the table. For instance, if the ddlSPType
    > selected
    > item is Aviation Company, an extra row containing a cell with the textbox
    > control txtAircrafts is added to the HTML table tblInsertSP (which now has
    > 17 controls). I use the following code:
    >
    > Dim k, totalRowCount as Int16
    >
    > Select Case ddlSPType.SelectedValue
    >
    > Case 1
    >
    > Dim tRow As New HtmlTableRow
    > For k = 0 to To 2
    > tRow.Cells.Add(New HtmlTableCell)
    > Next
    >
    > totalRowCount = tblInsertSP.Rows.Count
    >
    > tblInsertSP.Rows(totalRowCount - 1).Cells(0).InnerText =
    > "Aircrafts"
    >
    > Dim txtAircrafts As New TextBox
    > tblInsertSP.Rows(totalRowCount -
    > 1).Cells(1).Controls.Add(txtAircrafts)
    >
    > Case 2
    >
    > ....
    >
    > End Select
    >
    >
    > After adding extra rows dynamically, I want to save the entered
    > information
    > into the database tables, SP and SPAviationCompany (the latter is used for
    > specific data of the SP, namely Aircrafts). I have to reach the
    > txtAircrafts
    > control in the dynamically added row. I tried it with the following code
    > but
    > it did not work:
    >
    >
    > Dim str As String
    > str = CType(Page.FindControl("tblInsertSP").FindControl("txtAircrafts"),
    > TextBox).Text
    >
    > I receive an error stating that there's no such an object. How can I reach
    > this object? Thanks in advance.


    First of all, when you start getting NullReferenceException's like this, you
    should break the expression down so that you can see which part of it
    returned Nothing:

    Dim tbl As Control = Page.FindControl("tblInsertSP")
    Dim txt as Control = tbl.FindControl("txtAircrafts")

    Secondly, you already have a tblInsertSP, so why not use that directly?

    Dim txt As Control = tblInsertSP.FindControl("txtAircrafts")

    Finally, I believe the actual problem is that you never gave an ID to
    txtAircrafts, so you can't use the ID to look it up later:

    Dim txtAircrafts As New TextBox
    txtAircrafts.ID = "txtAircrafts"
    tblInsertSP.Rows(totalRowCount -
    1).Cells(1).Controls.Add(txtAircrafts)


    John Saunders
     
    John Saunders, Dec 20, 2004
    #3
    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. Alex
    Replies:
    2
    Views:
    579
  2. buran
    Replies:
    2
    Views:
    828
    buran
    Dec 29, 2004
  3. Geary
    Replies:
    2
    Views:
    721
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Jul 12, 2007
  4. Replies:
    0
    Views:
    1,015
  5. T.G.
    Replies:
    2
    Views:
    1,074
Loading...

Share This Page