Solved

Access Dynamically Created Texbox within a Datalist

Posted on 2014-02-26
4
308 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JSON  parse help 7 40
"Emulate" TAB key when press Enter Key 3 45
Finding the IIS version 5 21
Retrieve Active Directory Groups a User belongs to in VB.NET 3 18
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 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