Solved

Access Dynamically Created Texbox within a Datalist

Posted on 2014-02-26
4
307 Views
Last Modified: 2014-02-26
Hi,

I'm trying to access a textbox thats being added to a panel within a datalist, see below

            Dim connString As String = ConfigurationManager.ConnectionStrings("SitedbSQLConnectionString").ConnectionString
            Dim myConn As New SqlConnection(connString)
            Dim strQuery As String = "SELECT * FROM viewtrade_related WHERE MasterID = " & PID.Value
            Dim myCommand As New SqlCommand(strQuery, myConn)
            myConn.Open()
            Dim myReader As SqlDataReader = myCommand.ExecuteReader()
            If myReader.HasRows Then

                Dim table1 = New Table()
                table1.ID = "table1"
                table1.CssClass = "table1"

                While (myReader.Read())

                    Dim tRow As New TableRow
                    Dim tCell1 As New TableCell
                    Dim tCell2 As New TableCell
                    Dim tCell3 As New TableCell

                    Dim tbox As New TextBox
                    tbox.ID = "txt" & myReader("RelID")
                    tbox.Text = "1"
                    tbox.Style("Width") = "50px"
                    tCell1.Controls.Add(tbox)
                    tRow.Cells.Add(tCell1)

                    Dim btn As New Button
                    btn.CommandName = myReader("RelID")
                    btn.Text = myReader("Colour") & " | " & myReader("Size")
                    btn.Style("Width") = "200px"
                    AddHandler btn.Click, AddressOf btn_Click
                    tCell2.Controls.Add(btn)
                    tRow.Cells.Add(tCell2)

                    Dim ltl As New Literal
                    ltl.Text = "Availability : " & strAvail
                    tCell3.Controls.Add(ltl)
                    tRow.Cells.Add(tCell3)

                    table1.Rows.Add(tRow)
                End While

                PnlPages.Controls.Add(table1)

            Else
            End If
            myReader.Close()
            myConn.Close()

        End If
    End Sub

Open in new window


and then access the button ID and textbox value such as below

    Protected Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim btn As Button = TryCast(sender, Button)
        MsgBox(btn.CommandName)

        Dim PnlPages As Panel = TryCast(DataListSlippers.FindControl("PnlPages"), Panel)
        Dim txt As New TextBox()
        txt = TryCast(PnlPages.FindControl("txt" & btn.CommandName), TextBox)
        MsgBox(txt.Text)

        'tradecode.AddBasket(btn.CommandName, Session("UserSess"), 1)

    End Sub

Open in new window


but to no avail, any help appreciatted?

Cheers
Karl
0
Comment
Question by:Kbba04527
  • 2
  • 2
4 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39888929
Try using FindControl on btn.Parent property (by casting it to TableCell).
0
 

Author Comment

by:Kbba04527
ID: 39888982
Hi,

Apologies, would you be able to elaborate as I dont seem to find much useful info when googling keywords around.parent asp.net etc?

Cheers
Karl
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39888995
*Untested code


    Protected Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim btn As Button = TryCast(sender, Button)
        MsgBox(btn.CommandName)

        Dim Prnt As TableCell = TryCast(btn.Parent, TableCell)
        Dim txt As TextBox
        txt = TryCast(prnt.FindControl("txt" & btn.CommandName), TextBox)
        MsgBox(txt.Text)

        'tradecode.AddBasket(btn.CommandName, Session("UserSess"), 1)

    End Sub

Open in new window

0
 

Author Closing Comment

by:Kbba04527
ID: 39889019
Thank you so much, that worked like a charm.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question