Button server control w/ onClick attribute

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello All,

I am dynamically adding a Button web server control to my page with the
following code:

Dim btnView As New Button
btnView.Text = " View Form "
btnView.Attributes.Add("onClick", "javascript:ShowPdfWindow()")
AddHandler btnView.Click, AddressOf ShowPdf
plcButtons.Controls.Add(btnView)

ShowPdfWindow is defined in a script tag in the web page's <head> section.

The code does what I want it to do, except for one thing: the client script
(ShowPdfWindow) executes before the server code (ShowPdf).

Is there a way for me to reverse the order of execution?

TIA,
 
Yes and no. Not at all the way you are doing it.

You need to remove the client-Side on click. And during the server-side
event handler, output the javascript that you want (likely using
Page.RegisterStartupScript).
This will make your page postback, your server side handler to be processed,
the response to be output including the javascript you want.

Karl
 
All you would do is in your server-side event handler for the button do:

Page.RegisterStartupScript("ShowPdfWindow", "<script
language=""JavaScript"">" & System.Environment.NewLine & "ShowPdfWindow();"
& System.Environment.NewLine & "</script>")

if you are using 2.0, that's deprecated in favor of
Page.ClientScript.RegisterStartupScript with some slighly different
overloads..

Karl
 
Thank you.
--
Joe


Karl Seguin said:
All you would do is in your server-side event handler for the button do:

Page.RegisterStartupScript("ShowPdfWindow", "<script
language=""JavaScript"">" & System.Environment.NewLine & "ShowPdfWindow();"
& System.Environment.NewLine & "</script>")

if you are using 2.0, that's deprecated in favor of
Page.ClientScript.RegisterStartupScript with some slighly different
overloads..

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top