Solved

Access Dynamically Created Texbox within a Datalist

Posted on 2014-02-26
4
311 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

628 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