Jump to content
Xtreme .Net Talk

Recommended Posts

  • 2 weeks later...
Posted

If you're still working on this one - I've spent days trying to figure out the same problem only to find that the answer is horribly simple...

 

use the AddHandler method after creating your new control to wire an event to a handler exactly the same as you would in garden variety VB .net

 

eg.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

 

Dim btnTest As New Button()

btnTest.Text = "Click Me!"

AddHandler btnTest.Click, AddressOf OnButtonClick

 

PlaceHolder1.Controls.Add(btnTest)

 

End Sub

 

Private Sub OnButtonClick(ByVal sender As Object, ByVal e As EventArgs)

Response.Write("Button Clicked")

End Sub

 

if you need to create more than one button and handle each separately, you can set the CommandName or Command Argument properties of the button when you create it and then check for it in the handler

 

If ctype(sender, button).CommandArgument = "blah" etc

  • 3 weeks later...
Posted
Hi guys. When adding controls to a page dynamically (for instance checkboxes), how do you test the .checked property on postback. I have read that the controls need to be recreated on postback, but when they are the default .checked property it initialized to false, which overrides what the user selected. Can you provide any help?
Posted

dynamic controls

 

I used attributes to get data from controls. Here is the example.

 

Private Sub chkGlue_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkGlue.CheckedChanged

If chkGlue.Checked Then

Me.Attributes.Add("name", "Y")

Me.Attributes.Add("descr", "YES")

Else

Me.Attributes.Remove("name")

Me.Attributes.Remove("descr")

End If

End Sub

 

Then in my parent form, I read this attributes. It works fine.

Posted

Pretty cool trick. Actually, the problem I found after doing a google search and registering for about 500 discussion forums was that I was recreating the controls "too late" on postback. When I create them in the page_load event, their values remain intact, without the need to use attributes. Although I like that idea.

 

Thanks for the help.

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...