[.net] Strange problem with adding buttons dynamically to webforms

Started by
15 comments, last by Zul 19 years, 6 months ago
4GuysFrom Rolla has a great article on just this subject. There's an <asp:placeholder> which you can use when you add the control to the page to tell the page where to render the control. That would be even easier than using the panel solution I mentioned.
oh hai
Advertisement
I got a solution, but it gives me a problem as well.

I just make a usercontrol, place it on the page, and then add the controls to it rather than the page. Since it is inbetween the form tags, it works.

The problem? Before I inherited the page class for my class, and then my class in the .aspx page (my class basically was an extension of the page class). I then have a function you override to handle your button command events.

Now I need some way to pass this to my function to use in addhandler, rather than having a handler that you override. Anybody know a good way to go about this?

OnSelect (the function you override) looked like this:

Public Overridable Sub OnSelect(ByVal obj As Object, ByVal e As CommandEventArgs)End Sub


So basically, you inherit my class and override the function. That is no longer the case. I have my function using the addhandler keyword, they look like this:

Public Sub DrawSingleSystemWithButton(ByVal Position As Integer, ByVal Systems1 As MainSystems, ByVal NestedSystems1 As NestedSystems, ByVal ButtonText As String)	Dim SystemTable As System.Web.UI.WebControls.Table = New System.Web.UI.WebControls.Table	Dim ButtonRow As System.Web.UI.WebControls.TableRow = New System.Web.UI.WebControls.TableRow	Dim ButtonCell As System.Web.UI.WebControls.TableCell = New System.Web.UI.WebControls.TableCell	Dim MyButton As System.Web.UI.WebControls.Button = New System.Web.UI.WebControls.Button	DrawHeaderRow(SystemTable)	DrawSystem(Position, Systems1, NestedSystems1, SystemTable)	MyButton.Text = ButtonText	MyButton.Width = Unit.Percentage(100)	ButtonCell.ColumnSpan = 2	ButtonCell.Controls.Add(MyButton)	ButtonRow.Cells.Add(ButtonCell)	SystemTable.Rows.Add(ButtonRow)	Me.Controls.Add(SystemTable)	AddHandler MyButton.Command, AddressOf OnSelectEnd Sub


I need a way to pass the function I want to use to AddressOf. I tried delegates and all that stuff, but I just can't seem to figure it out.

EDIT: Nevermind, I got it.

[Edited by - xg0blin on October 12, 2004 10:17:09 AM]
Out of curiousity, was it:

AddHandler MyButton.ServerClick, AddressOf OnSelect

?
oh hai
Quote:Original post by Zul
Out of curiousity, was it:

AddHandler MyButton.ServerClick, AddressOf OnSelect

?


No. My code is at work. I'll show it to you tomorrow. Just check this thread in the morning.
*waiting*
oh hai
Ok, here is what I did:
Public Sub DrawSingleSystemWithButton(ByVal Position As Integer, ByVal Systems1 As MainSystems, ByVal NestedSystems1 As NestedSystems, ByVal ButtonText As String, ByVal CommandClicked As System.Web.UI.WebControls.CommandEventHandler)	Dim SystemTable As System.Web.UI.WebControls.Table = New System.Web.UI.WebControls.Table	Dim ButtonRow As System.Web.UI.WebControls.TableRow = New System.Web.UI.WebControls.TableRow	Dim ButtonCell As System.Web.UI.WebControls.TableCell = New System.Web.UI.WebControls.TableCell	Dim MyButton As System.Web.UI.WebControls.Button = New System.Web.UI.WebControls.Button	DrawHeaderRow(SystemTable)	DrawSystem(Position, Systems1, NestedSystems1, SystemTable)	MyButton.Text = ButtonText	MyButton.Width = Unit.Percentage(100)	ButtonCell.ColumnSpan = 2	ButtonCell.Controls.Add(MyButton)	ButtonRow.Cells.Add(ButtonCell)	SystemTable.Rows.Add(ButtonRow)	Me.Controls.Add(SystemTable)	AddHandler MyButton.Command, CommandClickedEnd Sub


Notice I pass in a System.Web.UI.WebControls.CommandEventHandler and then that is what gets called in the AddHandler section. Then I make a method (in whatever class I want, it doesn't matter) and pass AddressOf NameOfMethodIWantToUseForHandler as the argument for the function.
neato.
oh hai

This topic is closed to new replies.

Advertisement