|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
how to use Click event with programmatically defined button?I'm working with VWD and i defined programmatically a button (in code-behind) with an ID. So I expect to see beside "Page Events" and "Form1" my button "b1" in order to use the Click event. But it doesnt' appear. After saving the code-behind file, i only see "Page Events" and "Form1". If i define the button declarativally, i see it. But i need to define it programmatically. Can anybody tell me how to do? I also tried with Attributes.add but that's only for client script. Thanks Bob The code: --------- Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim b As New Button b.ID = "b1" form1.Controls.Add(b) End Sub Hi,
Bob wrote: Show quoteHide quote > I'm working with VWD and i defined programmatically a button (in first of all, the page's Init-event actually is the place where dynamic> code-behind) with an ID. So I expect to see beside "Page Events" and "Form1" > my button "b1" in order to use the Click event. But it doesnt' appear. After > saving the code-behind file, i only see "Page Events" and "Form1". > If i define the button declarativally, i see it. But i need to define it > programmatically. > [...] > The code: > --------- > Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) > Handles Me.Load > Dim b As New Button > b.ID = "b1" > form1.Controls.Add(b) > End Sub controls should be created. That is because this way you can actually use the so created controls in the course of events afterwards. With a button, this will not matter much though as you don't need to retrieve any information from clients during postbacks. In order to be able to consume your button's click-event, you'll need to define a handler and, once the control has been added to the page, tell your app to actually fire it when your button is clicked. In other words, create a handler ... Protected Sub MyButton_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) 'Your code goes here End Sub .... and then link your dynamic button to that handler: '... form1.Controls.Add(b) AddHandler b.Click, AddressOf MyButton_Click Cheers, Olaf Great. Thanks
Show quoteHide quote "Olaf Rabbachin" <Olaf_NoSpam@IntuiDev.com> schreef in bericht news:e0TW7apKHHA.5000@TK2MSFTNGP03.phx.gbl... > Hi, > > Bob wrote: > >> I'm working with VWD and i defined programmatically a button (in >> code-behind) with an ID. So I expect to see beside "Page Events" and >> "Form1" >> my button "b1" in order to use the Click event. But it doesnt' appear. >> After >> saving the code-behind file, i only see "Page Events" and "Form1". >> If i define the button declarativally, i see it. But i need to define it >> programmatically. >> [...] >> The code: >> --------- >> Protected Sub Page_Load(ByVal sender As Object, ByVal e As >> System.EventArgs) >> Handles Me.Load >> Dim b As New Button >> b.ID = "b1" >> form1.Controls.Add(b) >> End Sub > > first of all, the page's Init-event actually is the place where dynamic > controls should be created. That is because this way you can actually use > the so created controls in the course of events afterwards. With a button, > this will not matter much though as you don't need to retrieve any > information from clients during postbacks. > In order to be able to consume your button's click-event, you'll need to > define a handler and, once the control has been added to the page, tell > your app to actually fire it when your button is clicked. In other words, > create a handler ... > > Protected Sub MyButton_Click(ByVal sender As Object, _ > ByVal e As System.EventArgs) > 'Your code goes here > End Sub > > ... and then link your dynamic button to that handler: > > '... > form1.Controls.Add(b) > AddHandler b.Click, AddressOf MyButton_Click > > Cheers, > Olaf > -- > My .02: www.Resources.IntuiDev.com You'll never see your new button added to the intellisense when you create
it dynamically in this fashion. The button only exists at run-time and therefore cannot be accessed at designtime, especially since it's scope as a variable is limited to the pageload. Why not add the click event handler at the same time you are adding the new button control tot he form. You don't have to have it in the designer to add the click event to it. You can add the event in the line before you add b to the the form controls, or right after. I've heard a number or recomendations for setting things like a click event after you add it to the page's control hierarchy so that it is better tracked in the viewstate. Now, your big problem is going to be timing. The real issue here is since the button isn't created until the page_load event is called you won't be able to receive a click event if the postback occurs before the control is recreated. What you'll need to do is experiment with placing the button creation in an earlier event such as the init or preload. That way they button will be created before the postback event is called. -- Show quoteHide quoteHope this helps, Mark Fitzpatrick Former Microsoft FrontPage MVP 199?-2006 "Bob" <.> wrote in message news:Og5CESpKHHA.1252@TK2MSFTNGP02.phx.gbl... > Hi, > > I'm working with VWD and i defined programmatically a button (in > code-behind) with an ID. So I expect to see beside "Page Events" and > "Form1" my button "b1" in order to use the Click event. But it doesnt' > appear. After saving the code-behind file, i only see "Page Events" and > "Form1". > If i define the button declarativally, i see it. But i need to define it > programmatically. > > Can anybody tell me how to do? I also tried with Attributes.add but that's > only for client script. > Thanks > Bob > > The code: > --------- > Protected Sub Page_Load(ByVal sender As Object, ByVal e As > System.EventArgs) Handles Me.Load > Dim b As New Button > b.ID = "b1" > form1.Controls.Add(b) > End Sub > > "Mark Fitzpatrick" <markf***@fitzme.com> wrote in message Dynamic controls should always be created in Page_Init...news:ucELocpKHHA.3560@TK2MSFTNGP02.phx.gbl... > What you'll need to do is experiment with placing the button creation in > an earlier event such as the init
How to change the font style and size on the entire Aspx page programatically
Ajax Triggers for DropDownList don't fire Newbie Question: How Does One Access an Individual Field in a Record? Embeding html controls in Server Control Pass parameter from pages aspx Regular Expression Validator question Generating the *.aspx.designer.vb Files Merry Christmas from Windward launch page and redirect with LinkButton How to access templated controls in user control? |
|||||||||||||||||||||||