Solved

Access Dynamically Created Texbox within a Datalist

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now