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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Controls to a Page Programmatically 2

Status
Not open for further replies.

jupiter8

Programmer
Nov 22, 2002
72
0
0
GB
I just come to a page where I need to add a series of checkboxes programmatically from a database and although I've figured out how to add an ASP.NET control dynamically and place it within a panel, it seems a real drag to have to add literal controls to add all the surrounding HTML.

I add controls like this

Dim myInput As TextBox = New TextBox()
myInput.Text = "Label1 "
myInput.ID = "Label1"
FormPanel.Controls.Add(myInput)

A) Is there a better way to add controls dynamically and possition the corresponding HTML around them?

B) If I don't need the functionality of an asp server control is it a sin to just write out the HTML for an old sytle HTML Input element enclosed in the other HTML needed. Also will an ASP.Net server control still function to post back the form?

C) As I will need to loop through the contents of the form when posted back does either of the above two provide an major problems?

I can see that this is an area worth investing some time in to get right, so any help would be appreciated.
 
A) You are adding the controls correctly but I don't understand why you would then add a Literal to "add all the surrounding HTML". What you should do is assign each control a CSS class and use that class to position your elements.

B) If you don't need the functionality of a server control, then simply create a new instance of a HTMLControl and add that - it's not bad practice and it makes sense to use the right control that fit your needs. I'm not sure what you mean by this though:
Also will an ASP.Net server control still function to post back the form

C) Not really, although it's easier to iterate through server controls that HTML controls.

C)


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Thanks for that.

A) I would like to add an HTM label for accessibility purposes, although I think the style sheet idea is a neat one for layout purposes.

B) You understood correctly, just working out what the best practice is
 
Oh, so you mean you would be adding something like a label before the textbox to explain it's purpose? In that case, then yes, you would have to add a Literal (or a Label) control as well but again I'd use CSS for it's positioning.

Here's a simple example of what I mean:

1) Add this style to your page or put the details in a css file and create a link to it:
Code:
    <style type="text/css">
    .panel {
        width: 300px;
    }
    .row {
        clear: both;
    }
    .dynamicLabel {
        width: 150px;
        float: left;
    }
    .dynamicTextBox {
        float: right;
    }
    </style>

2) Next addd a panel to your page:
Code:
        <asp:Panel ID="Panel1" runat="server" CssClass="panel">
        </asp:Panel>

3) Add some controls to the page:
Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AddPageControls()
    End Sub

    Private Sub AddPageControls()
        Dim myLabel As Label
        Dim myTextBox As TextBox
        Dim myLiteralStart As Literal
        Dim myLiteralEnd As Literal

        For i As Integer = 1 To 10

            myLiteralStart = New Literal
            myLiteralStart.Text = "<div class=""row"">"
            Panel1.Controls.Add(myLiteralStart)

            myLabel = New Label
            myLabel.Text = "Dynamic Textbox " & i
            myLabel.ID = "lblDynamic" & i
            myLabel.CssClass = "dynamicLabel"

            myTextBox = New TextBox
            myTextBox.Text = ""
            myTextBox.ID = "txtDynamic" & i
            myTextBox.CssClass = "dynamicTextBox"

            Panel1.Controls.Add(myLabel)
            Panel1.Controls.Add(myTextBox)

            myLiteralEnd = New Literal
            myLiteralEnd.Text &= "</div>"
            Panel1.Controls.Add(myLiteralEnd)
        Next
    End Sub


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
This is exactly the answer I was looking for, many thanks.
 
OK how do you iterate through server controls the ASP.Net 2 way?

I can see how you could loop through request.form, but this is messy as you have to extract the bit of the form ID you want as ASP.NET appends other information to the ID field.

For Each Item In Request.Form

next

So while this would work, what's the proper way to do it?
 
Code:
For Each ctl As Control In Me.Controls
...
Next
but remember that controls may not necessarily belong to the page (i.e. if they are in a panel) so you'll have to create a recursive function to deal with any child controls.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Yes they are in a single panel, except for the submit button which I don't want anyway.

So can you say?

For Each ctl As Control in FormPanel.Controls
...
 
Yes you can...


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Ok I can add code in Page Load events as follows

myCheckBox = New CheckBox()
myCheckBox.Text = dr("Privilege_Desc")
myCheckBox.ID = "priv" & dr("PrivilegeID")
FormPanel.Controls.Add(myCheckBox)

But in the event fired on clicking the submit button

For Each ctl As Control In FormPanel.Controls
Response.Write(ctl.ID)
Next

brings back nothing what do you use to get a reference for the control I need the usual id and value
 
That should return the ID's for each control. Are you re-creating the controls on the Page Load event?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Ok I'd wrapped the code with

If Not Page.IsPostBack Then

end if

which seemed quite logical, obviously not
 
Not with dynamic controls as they will need to be re-created on each page load. Once you remove that you should be set...


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Yes I can loop through the checkboxes and get their ID's, however I can't figure the right syntax to figure out whether they are checked or not.

I want to write

For Each ctl As Control In FormPanel.Controls

If Left(ctl.ID, 4) = "priv" Then

MyRef = GET SOME REFERENCE TO CONTROL

If MyRef.Checked then
...
End If

End If
Next

 
You have to get the control and they cast the type. In your For loop do something like this:
Code:
   If TypeOf ctl Is CheckBox Then
      Dim cb as New CheckBox
      cb = CType(cb, CheckBox)

      If cb.Checked Then
         ...  Do Somehting ...
      End If
   End If

Jim
 
Thanks, its taken me all day to do what would have taken 10 minutes in classic ASP, but I've learnt quite a lot today.

Oh there was one tiny error in your code

cb = CType(cb, CheckBox)

should have been

cb = CType(ctl, CheckBox)

Don't get me wrong I'm not trying to be clever, its just any case anyone else looks at this.



 
Yes you are correct, my mistake.. typo ..

Glad it is working for you ...

Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top