Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Access Dynamically Created Texbox within a Datalist

Posted on 2014-02-26
4
Medium Priority
?
317 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 2000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

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.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Integration Management Part 2

885 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