Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0dynamically adding controls errordynamically adding controls error
Previous
 
Next
New Post
10/6/2006 3:45 AM
 

I'm trying to dynamically add 5 buttons to my custom module but I get an error saying that they have the same ID, but they don't, I notice that the page_load is called a lot of times, why is it called so many times?

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

Try

Dim objModules As ModuleController = New ModuleController

If Not Page.IsPostBack Then

If (Not (CType(Settings("showform"), String)) Is Nothing) Then

If (CType(Settings("showform"), String) = "No") Then

' Do not allow messages to be added

FormView1.Visible = False

lblAddMessage.Visible = False

End If

End If

Dim n As Integer = 0

For n = 0 To 5

Dim b As Button = New Button()

b.ID = "MyBtn" & n

b.Text = n.ToString

BtnPlaceHolder.Controls.Add(b)

Next

Else

Me.GridView1.DataBind()

End If

Catch ex As Exception

Exceptions.ProcessModuleLoadException(Me, ex)

End Try

End Sub

 
New Post
10/6/2006 10:16 AM
 
onishiro wrote

For n = 0 To 5

Dim b As Button = New Button()

b.ID = "MyBtn" & n



Maybe try this:

b.ID = "MyBtn" & str(n)


    - Doug Vogel     

 
New Post
10/6/2006 11:48 AM
 

So if you always want five buttons, I'm not sure why you wouldn't just put five buttons on your design surface.  If you want to do it in code, though, you certainly can.

Remember, VB is zero based, so in your example, you are setting n=0 and then looping from 0 to 5.  This will actually give you six buttons since 0 counts.  I presume you really want the buttons to be 1-5 so here's how you do it.

     Dim n As Integer = 1
     For n = 1 To 5
         Dim b As Button = New Button()
         b.ID = "MyBtn" & CType(n, String)
         b.Text = n.ToString
         BtnPlaceHolder.Controls.Add(b)
     Next

You could also do this, but if you do, your button text will start with 0.

     Dim n As Integer = 0
     For n = 0 To 4
 
Previous
 
Next
HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0dynamically adding controls errordynamically adding controls error


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out