Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Loop through Controls in dynamic table inside place holder

Posted on 2009-07-10
5
Medium Priority
?
603 Views
Last Modified: 2013-11-26
I am using vb.net. I have a place holder in a page that is populated by a dynamic table that is added to the place holder after the user selects a value from a drop down box. Inside this dynamic table are check boxes.

The goal is to cycle through these check boxes and figure out which ones are checked after a button is pushed. The problem seems to be when the html is rendered. I tried putting code in the init event. I have attached the code.
PH_Tables is the name of the place holder control. Inside that is a dynamic table that contains the check boxes. I included the function that returns the control causing the postback - for completeness.
Public Shared Function GetPostBackControl(ByVal page As Page) As Control
        Dim control As Control = Nothing
 
        Dim ctrlname As String = page.Request.Params.Get("__EVENTTARGET")
        If ctrlname <> Nothing And ctrlname <> String.Empty Then
            control = page.FindControl(ctrlname)
        Else
            Dim ctl As String
            For Each ctl In page.Request.Form
                Dim c As Control = page.FindControl(ctl)
                If TypeOf c Is System.Web.UI.WebControls.Button Then
                    control = c
                    Exit For
                End If
            Next
        End If
        Return control
    End Function
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Dim s As Control
        Dim cntrl As Control
        Dim cb_cntrl As CheckBox
        Dim SB_ext_desc As New StringBuilder
        Dim testme As String
 
        s = GetPostBackControl(Me)
 
        If (Not (IsNothing(s)) AndAlso s.ID = "Button1") Then
 
            For Each cntrl In PH_tables.Controls
                testme = cntrl.ID
                If TypeOf cntrl Is CheckBox Then
                    cb_cntrl = cntrl
                    If cb_cntrl.Checked = True Then
                        SB_ext_desc.Append(cntrl.ID)
                        SB_ext_desc.Append(" ")
                    End If
                End If
 
            Next
            Session("s_ext_desc") = SB_ext_desc
            Session("do_update") = True
        End If
 
    End Sub

Open in new window

0
Comment
Question by:estanton
[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
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:funwithdotnet
ID: 24828699
You  mention when, but not exactly _what_ the problem is.

I can guess that the dynamic control/table/etc. does not exist on postback. It has to be re-created or the control no longer exists on postback.
0
 

Author Comment

by:estanton
ID: 24830458
Oops
I can not get to the check boxes. I have tried looping through the tabel and place holder controls in page init and pre render. The check boxes don't show up.
0
 
LVL 12

Accepted Solution

by:
funwithdotnet earned 2000 total points
ID: 24832294
If the checkboxes are in a dynamic table created in response to user input, they will not exist on future postbacks unless the action in response to the original user input is re-created. And if it has been re-created, it would not exist at Page_Init unless it is in a user control.

I'm not exactly sure what kind of control 'dynamic table' refers to. Perhaps the checkbox controls are not in the table, but in cell controls in row controls within the table. A recursive search is required.

Something like this:

Private Sub Page_Load( ...) 
  RecreateTableInPlaceHolder()
  FindMyControl(PlaceHolder)
End Sub
 
Private Sub FindMyControl(ByVal inputControl as Control)
  If inputControl = 'whatever you're looking for
    ' Do something.
  ElseIf inputControl.HasControls Then
    Dim myControl as Control
    For Each myControl in inputControl.Controls
      FindMyControl(myControl)
    Next
  End If
End Sub

Open in new window

0
 

Author Comment

by:estanton
ID: 24836847
I will try that tomorrow.
0
 

Author Comment

by:estanton
ID: 24844202
Worked great. Thanks! The checkbox was in the TableCell.
0

Featured Post

Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

722 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