Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Calling Javascript function on server control click

Calling Javascript function on server control click

Scheduled Pinned Locked Moved ASP.NET
javascriptdesignsysadminperformance
12 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Mankayarkarasi

    Hi, I have written a function in javacript and I have created an ASP Button server Control.I want to call the function written in javascript as well as execute the method for buttonclick. There are two ways. A) 1. OnClick Event call the function 2. Onserverclick assign the Servermethod This is done in design time. B) 1.Call the javascript function in codebehind page - btnID.Attributes.Add("onclick","SetFocus();") 2.in design time call the Onserverclick the appropriate Servermethod. Which is the best way to handle.I need the answer in performance wise too. Thanks in Advance. Happy Coding MankayarKarasi

    S Offline
    S Offline
    saud_a_k
    wrote on last edited by
    #2

    I have a similar problem. I want to run a CodeBehind function (OnClick) as well as run a javascript to open a pop up window. in the A) option in your post you have said that: 1. onclick Event call the function 2. Onserverclick assign the Servermethod How can I do this? please help. Thank you. _____________________________________________________ Yea! I could be wrong...

    P R S 3 Replies Last reply
    0
    • S saud_a_k

      I have a similar problem. I want to run a CodeBehind function (OnClick) as well as run a javascript to open a pop up window. in the A) option in your post you have said that: 1. onclick Event call the function 2. Onserverclick assign the Servermethod How can I do this? please help. Thank you. _____________________________________________________ Yea! I could be wrong...

      P Offline
      P Offline
      PSK_
      wrote on last edited by
      #3

      Method 1: Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) button1.Attributes.Add("onclick","window.open(""b.aspx"")") End Sub Method 2: function openNew() { window.open("b.aspx"); } ... ... < asp :Button ID="Button1" Runat="server" Text="Button" OnClientClick="openNew();" /> Method3: Dim JS As String JS = "{" JS += "window.open(""somepage.aspx"")}" JS += "</" JS += "script>" Literal1.Text = JS </code> like this in different way u can open a popup window but i don't think that from server side code we can pop a window coz its a client side event, and we have to open it using client side scripting only. <CENTER><A href="HTTP://PRAKASH-K.BLOGSPOT.COM"> <DIV style="WIDTH:225PX;BACKGROUND-IMAGE: url(http://www.prakashshipping.org/image/prakash.gif); BACKGROUND-REPEAT: no-repeat; BACKGROUND-COLOR: #ffffff"><BR><BR><br></DIV></A></CENTER> </x-turndown>

      S 1 Reply Last reply
      0
      • P PSK_

        Method 1: Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) button1.Attributes.Add("onclick","window.open(""b.aspx"")") End Sub Method 2: function openNew() { window.open("b.aspx"); } ... ... < asp :Button ID="Button1" Runat="server" Text="Button" OnClientClick="openNew();" /> Method3: Dim JS As String JS = "{" JS += "window.open(""somepage.aspx"")}" JS += "</" JS += "script>" Literal1.Text = JS </code> like this in different way u can open a popup window but i don't think that from server side code we can pop a window coz its a client side event, and we have to open it using client side scripting only. <CENTER><A href="HTTP://PRAKASH-K.BLOGSPOT.COM"> <DIV style="WIDTH:225PX;BACKGROUND-IMAGE: url(http://www.prakashshipping.org/image/prakash.gif); BACKGROUND-REPEAT: no-repeat; BACKGROUND-COLOR: #ffffff"><BR><BR><br></DIV></A></CENTER> </x-turndown>

        S Offline
        S Offline
        saud_a_k
        wrote on last edited by
        #4

        there is no method called OnClientClick=".." please help _____________________________________________________ Yea! I could be wrong...

        P 1 Reply Last reply
        0
        • S saud_a_k

          there is no method called OnClientClick=".." please help _____________________________________________________ Yea! I could be wrong...

          P Offline
          P Offline
          PSK_
          wrote on last edited by
          #5

          OOPS.. I did this example in Version 2.0 (Visula Web Developer 2005). Better you use HTML Input button instead of using asp:button function Button1_onclick() { window.open("newwindow.aspx"); } < input id="Button1" type="button" value="button" language="javascript" onclick="return Button1_onclick();" /> </code> <CENTER><A href="HTTP://PRAKASH-K.BLOGSPOT.COM"> <DIV style="WIDTH:225PX;BACKGROUND-IMAGE: url(http://www.prakashshipping.org/image/prakash.gif); BACKGROUND-REPEAT: no-repeat; BACKGROUND-COLOR: #ffffff"><BR><BR><br></DIV></A></CENTER> </x-turndown>

          S 1 Reply Last reply
          0
          • P PSK_

            OOPS.. I did this example in Version 2.0 (Visula Web Developer 2005). Better you use HTML Input button instead of using asp:button function Button1_onclick() { window.open("newwindow.aspx"); } < input id="Button1" type="button" value="button" language="javascript" onclick="return Button1_onclick();" /> </code> <CENTER><A href="HTTP://PRAKASH-K.BLOGSPOT.COM"> <DIV style="WIDTH:225PX;BACKGROUND-IMAGE: url(http://www.prakashshipping.org/image/prakash.gif); BACKGROUND-REPEAT: no-repeat; BACKGROUND-COLOR: #ffffff"><BR><BR><br></DIV></A></CENTER> </x-turndown>

            S Offline
            S Offline
            saud_a_k
            wrote on last edited by
            #6

            If I use a html link then I cannot put in server side code. I use a button like button1_OnClick(....) { ...some validation... //..and probable code to open a new aspx page in a new window } I cannot use a HTML link!:(( Help. _____________________________________________________ Yea! I could be wrong...

            P 1 Reply Last reply
            0
            • S saud_a_k

              I have a similar problem. I want to run a CodeBehind function (OnClick) as well as run a javascript to open a pop up window. in the A) option in your post you have said that: 1. onclick Event call the function 2. Onserverclick assign the Servermethod How can I do this? please help. Thank you. _____________________________________________________ Yea! I could be wrong...

              R Offline
              R Offline
              Rocky Moore
              wrote on last edited by
              #7

              You might see the tip on adding a conformation popup to buttons: http://www.hintsandtips.com/ShowTip/107/hat.aspx[^] Rocky <>< www.HintsAndTips.com - Includes Developer Tips www.MyQuickPoll.com - 2004 Election poll is #33

              S 1 Reply Last reply
              0
              • R Rocky Moore

                You might see the tip on adding a conformation popup to buttons: http://www.hintsandtips.com/ShowTip/107/hat.aspx[^] Rocky <>< www.HintsAndTips.com - Includes Developer Tips www.MyQuickPoll.com - 2004 Election poll is #33

                S Offline
                S Offline
                saud_a_k
                wrote on last edited by
                #8

                This only shows how to show a confirmation box... How can I use it to make a pop up window? _____________________________________________________ Yea! I could be wrong...

                R 1 Reply Last reply
                0
                • S saud_a_k

                  This only shows how to show a confirmation box... How can I use it to make a pop up window? _____________________________________________________ Yea! I could be wrong...

                  R Offline
                  R Offline
                  Rocky Moore
                  wrote on last edited by
                  #9

                  From the server side you can: Response.Write("window.open('mypopuppage.html');"); Response.End(); or on client side you can add in your Page_Load on the server: ImageButton1.Attributes["onclick"]="window.open('mypopup.html'); return true;"; This will open the popup window and then call your OnClick event on the server for the original page. Change the "return true" to "return false" if you do not wish the server side onClick to be called. Rocky <>< www.HintsAndTips.com - Includes Developer Tips www.MyQuickPoll.com - 2004 Election poll is #33

                  S 1 Reply Last reply
                  0
                  • S saud_a_k

                    If I use a html link then I cannot put in server side code. I use a button like button1_OnClick(....) { ...some validation... //..and probable code to open a new aspx page in a new window } I cannot use a HTML link!:(( Help. _____________________________________________________ Yea! I could be wrong...

                    P Offline
                    P Offline
                    PSK_
                    wrote on last edited by
                    #10

                    hey don't cry ... its really very simple .. u r making it complex void Button1_Click(object sender, ImageClickEventArgs e) { // do some validation whatever u want if (validate==true) { //open a new window //this'll open a child popup window for u String JS=""; JS = "{"; JS += "window.open('somepage.aspx')}"; JS += "</"; JS += "script>"; Literal1.Text = JS; } else { // do what ever u want //display some error message } Note: For smooth effects.. place the literal control at the bottom of the page so that the javascript will be fired when whole page is downloaded at client sied. <CENTER><A href="HTTP://PRAKASH-K.BLOGSPOT.COM"> <DIV style="WIDTH:225PX;BACKGROUND-IMAGE: url(http://www.prakashshipping.org/image/prakash.gif); BACKGROUND-REPEAT: no-repeat; BACKGROUND-COLOR: #ffffff"><BR><BR><br></DIV></A></CENTER> </x-turndown>

                    1 Reply Last reply
                    0
                    • R Rocky Moore

                      From the server side you can: Response.Write("window.open('mypopuppage.html');"); Response.End(); or on client side you can add in your Page_Load on the server: ImageButton1.Attributes["onclick"]="window.open('mypopup.html'); return true;"; This will open the popup window and then call your OnClick event on the server for the original page. Change the "return true" to "return false" if you do not wish the server side onClick to be called. Rocky <>< www.HintsAndTips.com - Includes Developer Tips www.MyQuickPoll.com - 2004 Election poll is #33

                      S Offline
                      S Offline
                      saud_a_k
                      wrote on last edited by
                      #11

                      I tried what you've told. ImageButton1.Attributes["onclick"]="window.open('mypopup.html'); return true;"; But I have to open a page with a QueryString. Please see the QueryString problem in the above post. _____________________________________________________ Yea! I could be wrong...

                      1 Reply Last reply
                      0
                      • S saud_a_k

                        I have a similar problem. I want to run a CodeBehind function (OnClick) as well as run a javascript to open a pop up window. in the A) option in your post you have said that: 1. onclick Event call the function 2. Onserverclick assign the Servermethod How can I do this? please help. Thank you. _____________________________________________________ Yea! I could be wrong...

                        S Offline
                        S Offline
                        saud_a_k
                        wrote on last edited by
                        #12

                        Firstly thanx for putting up with a novice like me... I have a form Form1.aspx on it are 3 DropDowns I have to build a QuerSting depending on the values of the Dropdowns. I also have an ImageButton, OnClick builds the QueryString AND Opens a new page Form2.aspx. Like:

                        ImageButton_Click(...)
                        {
                        string qs = "Form2.aspx?";
                        qs = qs + "s1=" + DorpDown1.SelectedValue.ToString();
                        qs = qs + "&s2=" + DorpDown2.SelectedValue.ToString();
                        qs = qs + "&s3=" + DorpDown3.SelectedValue.ToString();

                        Response.Redirect(qs);//This opens Form2 in the same page...
                        // I want to open it in a popup....
                        }

                        Sounds simple eh?... But plese help!! I tried your option by giving qs in

                        JS = "{";
                        JS += "window.open('qs')}";
                        JS += " JS += "script>";</pre>
                        It does not work!
                        I think the whole literal thing doesn't work.
                        YELP!

                        PLEASE HELP ME GUYS

                        </x-turndown>

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups