Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi there!

 

I'm not sure if this has already been discussed. There are a few examples on control arrays around there, but nothing seems to fit my needs.

 

I'm making a calendar-like web application, where you can schedule events and watch what has already been scheduled. I already made it, and works fine, but I don't like the way the code looks like, it looks like newbie's code (well, I'm a newbie in vb.net, otherwise i wouldn't be asking :p ) and I really miss the control array functionality, I think it would improve the way my application works.

To resume this, what I need is the control array functionality, wehre I can -at run time- create controls (let's say a button) wherever I want over my web page with a dynamically generated ID (let's say mybutton(1)), and access -at run time also- it's properties (let's say mybutton(1).text="hello world").

I know it can be done (at least that's what MSDN says), but not sure how. Any ideas?.:confused:

 

Thanks!

 

PD: most (all maybe?:( ) examples around are for WINDOWS forms, which is not what I need. I need code for WEB forms.

  • *Experts*
Posted

You can use an ArrayList:

Dim i As Integer
Dim ctrls As New ArrayList()

For i = 0 to 10
 Dim b As New Button()

 '.. position the button and stuff
 ctrls.Add(b)

 AddHandler b.Click, AddressOf OnButtonClicked 'you need to make this sub yourself
Next i

You can also access an arraylist with indicies like regular arrays, too --

ctrls(2).BackColor = Color.Red

 

[edit]Oh, I just realized you want a webforms example; well, I think the same logic should apply[/edit]

Posted

Tath's a great idea, but seems to not work as it should.

 

I tried this code, in a form with a button (button1) and a panel (panel1) -also a hidden textbox (textbox1) to be used as a temporary storage.

 

In the button1 code, i used this:

 

   Dim test As New ArrayList

   Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim i As Int16 = 0
       Dim b As New Button
       test.Add(b)
       i = Val(TextBox1.Text)
       test(0).id = i
       Label1.Text = test(0).uniqueid
       test(0).style("position") = "absolute"
       test(0).style("left") = i * 10
       Panel1.Controls.Add(test.Item(0))
       i = i + 1
       TextBox1.Text = i
   End Sub

 

First you should wonder why I didn't just added the button over the web page. Well, I did, but an error returned:

 

Server Error in '/WebApplication1' Application.

--------------------------------------------------------------------------------

 

Control '0' of type 'Button' must be placed inside a form tag with runat=server.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

 

Exception Details: System.Web.HttpException: Control '0' of type 'Button' must be placed inside a form tag with runat=server.

 

Source Error:

 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

 

Stack Trace:

 

 

[HttpException (0x80004005): Control '0' of type 'Button' must be placed inside a form tag with runat=server.]

System.Web.UI.Page.VerifyRenderingInServerForm(Control control)

System.Web.UI.WebControls.Button.AddAttributesToRender(HtmlTextWriter writer)

System.Web.UI.WebControls.WebControl.RenderBeginTag(HtmlTextWriter writer)

System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)

System.Web.UI.Control.RenderControl(HtmlTextWriter writer)

System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)

System.Web.UI.Control.Render(HtmlTextWriter writer)

System.Web.UI.Control.RenderControl(HtmlTextWriter writer)

System.Web.UI.Page.ProcessRequestMain()

 

 

So I decided to use an alternative "parent" object to hold the buttons: a panel (since I'm not suppossed to mess with the html code).

 

So far so good. Bad news is the buttons created by the code at run time dissappears!!!.... just like that.. I clicked button1, and I was jumping around happily because it worked, when I cliked again, the new button showed up, in the correct position (10px right of the first button), just the FIRST BUTTON WAS MISSING!!:mad:.... I clicked again... and again... and always the same... the buttons created does not remain visible over the page. What could be wrong? :(

 

Thx!!

  • *Experts*
Posted

That's because you're just changing the position of test(0), not the newly created button.

 

Try this:

Dim b As New Button
test.Add(b)

Dim n As Integer = test.GetUpperBound(0)
test(n).Text = "Moo!"
text(n). 'other stuff....

Posted

DUH!!!!.... that was stupid LOL

 

Well, I fixed that, but a new error came up. I've been trying different approaches to it, but is always the same... I think I just don't know how to handle an arraylist.

 

This is the code I'm using now:

 

   Dim test As New ArrayList(10)

   Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim b As New Button
       Dim i As Int16 = 0
       i = Val(TextBox1.Text)
       test.Add(b)

       test.Item(i).id = i
       Label1.Text = test.Item(i).uniqueid
       test.Item(i).style("position") = "absolute"
       test.Item(i).style("left") = i * 20
       Panel1.Controls.Add(test.Item(i))
       i = i + 1
       TextBox1.Text = i
       TextBox2.Text = test.Capacity
   End Sub

 

this supposedly should create an arraylist able to hold up to 10 objects (index from 0 to 9).

It works fine for the first time you press the button, but the second time (index value is 1 then) returns this error:

 

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

 

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

 

Source Error:

 

 

Line 39: test.Add(b)

Line 40:

Line 41: test.Item(i).id = i

Line 42: Label1.Text = test.Item(i).uniqueid

Line 43: test.Item(i).style("position") = "absolute"

 

 

Source File: C:\Inetpub\wwwroot\OPTcalendar\WebApplication1\WebForm1.aspx.vb Line: 41

 

It says index value=1 is out of range, but I'm creating an arraylist which size is 10!!.... now I'm lost

 

I even tried this -with the same results-:

 

instead

test.Add(b)

 

I used

test.insert(i, b)

 

PS:added a textbox (textbox2) to check the capacity of the arraylist.

  • *Experts*
Posted

I don't think the capacity allocates 10 items right off the bat; you need to start at 0 and go up. Instead of getting the value from the textbox, just do what i did and get the UpperBound of the array (after you've added the button).

 

And don't forget, it starts at 0. Also, just try not specifying the capacity right off the bad, and leave the constructor as ().

 

Sorry, I don't know much about webforms and ASP.NET like that, so I probably won't be able to help much more. :-\

Posted

I can't access the method GetUpperBound, it doesn't show up in the list of available methods and properties of my test array :(

Can you tell me what GetUpperBound does? maybe there is another method doing the same or similar.

 

Thx!

Posted

Ok, GetUpperBound is for array only, not for arraylist, that I checked out. Anyway it does exactly what you say, so I tried with LastIndexOf+1 and Count-1, but the result is still the same. I also tried with hard coded index values (ie: test(0).text="hello" test(1).text="world").... same error.... this is weird.

Maybe array methods are not intended to be used in a silly webform?:p :rolleyes:

 

anyway... still lost here :(

Posted

Cool.... I think I got something at last... seems to work, but there is a problem...

 

   Dim text1(2) As TextBox

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim i As Int16
       i = Val(TextBox1.Text)
       text1(i) = New TextBox
       text1(i).Text = "hello world number " & i
       text1(i).Style("position") = "absolute"
       text1(i).Style("top") = i * 30
       Button1.Parent.Controls.Add(text1(i))
       TextBox1.Text = i + 1
   End Sub

 

This code works great, it makes an array of textboxes (number inside parenthesis indicates size of array) and I can make the new control to appear in the webform (used a button -button1- to trigger the code and a hidden textbox -textbox1- for temporal storage).

 

Ok, now the problem is every time I click the button, a new control (textbox in this case) appears over the webform, but the textbox created just before dissappears!... any ideas why this is happening?

 

My guess is the button triggers the webform to reload itself thus clearing whatever change has been made in run-time. Is there any way to avoid this, making the changes more *permanent*?

Posted

Cool!!... now I got it working.

The problem of the controls *dissappearing* also made the handler not to work, so nothing happened after pressing one of this *dynamic* buttons.

 

The key to solve this is to redraw the controls every time the webform reloads. And a reload is made every time a control triggers an event such as click, textchange, etc. so this is why the control was dissappearing and no code was executing in the handler: the click event first made a postback and then tries to execute the handler... but if there is no button after the postback, then no code is executed.

 

Hope this helps someone else too

 

Thx a lot!!!:D

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...