Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating and Accessing Web Controls at Runtime

Status
Not open for further replies.

Deleco

Programmer
Feb 25, 2002
109
0
0
GB
Hi all,

I am having a bit of a nightmare i am creating controls at runtime in a panel, in another panel and then inside a table. The only way i can figure out how to access these dynamically created controls is with this massive loop/if structure. Does anyone know of an easier method.


Code:
For Each ctrl In pnlParent.Controls
            If ctrl.GetType.FullName = "System.Web.UI.WebControls.Panel" Then
                For Each ctrl2 In ctrl.Controls
                    If ctrl2.GetType.FullName = "System.Web.UI.WebControls.Table" Then
                        tbl = ctrl2
                        For Each ctrl3 In tbl.Rows
                            If ctrl3.Cells(1).Controls.Count > 0 Then
                                chk = ctrl3.Cells(1).Controls(0)
                                If chk.Checked = True Then
                                    Dim dr As DataRow
                                    Dim sSplit() As String
 
                                    sSplit = Split(chk.ID, "^")

 
                                    dr.Item("PROLE_ID") = sSplit(0).ToString

                                    dr.Item("COURSE_ID") = sSplit(1).ToString

                                    dr.Item("DELEGATE_NAME") = sSplit(2).ToString

 
                                    dt.Rows.Add(dr)

                                    dt.AcceptChanges()
                                End If
                            End If
                        Next
                    End If
                Next
            End If

       Next

Many thanks in advance for any assistance
Deleco


Deleco
 
Declare the controls as private member variables of the page before adding them to the Controls collection, that way you can reference them by name something like this
Code:
private Panel myPanel = new Panel();
private Table myTable = new Table();

void Page_Load(object sender, EventArgs e){
  myPanel.Controls.Add(myTable);
  this.Controls.Add(myPanel);
  myTable.CssClass = "cssclass";
}

Rob

i'm a boy, called Bert, and I may not be crazy, but if i'm not the rest of you are...
 
ooops sorry you think in VB and I did C# - Doh! Oh well is simple enough code that it shouldn't be a problem. Shout if you want a VB example :)

Rob


i'm a boy, called Bert, and I may not be crazy, but if i'm not the rest of you are...
 
Hi Deleco,
My 2 cents... since you created those controls at run-time, perhaps you can declare a 'holding' container and add the reference of the controls to that container IN ADDITION to adding the control to the tables? Probably an array or a NameValue collection (depending on which is more efficient for you).

So, subsequently, you can reference the Checkboxes via that container insead of looping the Panels. A simple For Each on the container will do the job.

Hope this helps. cheers.

regards,
- joseph
==================


~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
 
Thanks i'll give these things a try... I'll let you know

Deleco
 
Hi All,

I think i used fhlee's technique. I added the checkboxes to a collection object and named them relevantly. I then looped round the collection did some string manipulation with the names and whooped -de-doo. It worked with only one loop and one if statement. A lot more tidier and readable. Thanks for the help

Deleco
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top