Results 1 to 2 of 2

Thread: webcontrol button with script attribute

  1. #1

    Thread Starter
    Addicted Member jewel's Avatar
    Join Date
    Jul 2003
    Location
    truly asia
    Posts
    153

    webcontrol button with script attribute

    how can I make the button_click to execute first before the script that I added in the buttons's attribute

    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         'Put user code to initialize the page here
    3.  
    4.         Dim scriptOk As String
    5.         scriptOk = "window.returnValue = true; " _
    6.         & " window.dialogArguments.strName = window.document.all.MyFile.value; " _
    7.         & " window.close();"
    8.         btnOk.Attributes.Add("onclick", scriptOk)
    9.         btnCancel.Attributes.Add("onclick", "window.returnValue = false;window.close();")
    10.     End Sub
    11.  
    12. Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
    13.         'Response.Write("hello")
    14.         'MyFile.PostedFile.SaveAs("")
    15.  
    16.     End Sub

    myFile is an htmlinputfile server control, I want the file to be uploaded first before the web page execute the script to close the window

    Thanks,

    Jewel
    xoxo

  2. #2
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: webcontrol button with script attribute

    You can't do what you are saying the method you suggest. The client-side onclick event will always fire before the page is Posted Back and the server-side click event is fired.

    What you want to do is remove the client-side onclick event and add some code to the server-side event that uses the Page's RegisterStartupScript method.

    This method executes client-side code as soon as the page reloads i.e. after your server-side Click event fires. It is used as below:
    Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            btnCancel.Attributes.Add("onclick", "window.returnValue = false; window.close();")
    End Sub
    
    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
            If MyFile.PostedFile.ContentLength > 0 Then
    		'Response.Write("hello")
            	'MyFile.PostedFile.SaveAs("")
    		RegisterStartupScript("image_uploaded", "window.returnValue = true; window.close();")
    	End If
    End Sub
    The RegisterStartupScript has two arguments - the first is a unique key so it can be referenced in your code and the second is the client-side script you want to execute.

    Let me know if you have any problems.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width