Results 1 to 10 of 10

Thread: JS and ASP.net

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    JS and ASP.net

    I have a button in my user control:
    <asp:Button ID=si Text=SI Runat=server cssclass=knap Enabled=False Width=50px OnClick="openNewWindow"></asp:Button>

    And the js-function is defined at the beginning of the ascx-file:
    <script language=javascript type="text/javascript">
    function openNewWindow()
    {
    var winName = "w";
    var winURL = "gf2004.aspx";
    var w;
    w = window.open(winURL,winName);
    w.focus();
    }
    </script>

    I have two questions:

    1) Why does this code not work? Has it something to do with the user control? The error message is:
    Compiler Error Message: BC30456: 'openNewWindow' is not a member of 'ASP.Buttons_ascx'.

    2) Is it possible in JS to use this url winURL = "gf2004.aspx"; when the path is the same? Or do I have to use absolute paths (not preferred!)

  2. #2
    Addicted Member
    Join Date
    Aug 2004
    Location
    Cape Town, South Africa
    Posts
    149

    Re: JS and ASP.net

    not sure if i'm right here, but for your question 1 I remember that user controls do not have a form tag, and JS needs a form tag to work. Could be wrong, search it on google :/

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: JS and ASP.net

    This:

    OnClick="openNewWindow();"

    should work.

    If it doesn't, post back, I'll show you another way of doing this.

  4. #4
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Re: JS and ASP.net

    Yeah you have to put the () the ; is optional.
    Magiaus

    If I helped give me some points.

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: JS and ASP.net

    Quote Originally Posted by Magiaus
    Yeah you have to put the () the ; is optional.
    I've had a few problems when I didn't include that, especially when the button's onClick event contains other javascript function put in there by ASP.NET, so it's best practice to include it, although as you said, optional.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Re: JS and ASP.net

    Quote Originally Posted by Patch21
    not sure if i'm right here, but for your question 1 I remember that user controls do not have a form tag, and JS needs a form tag to work. Could be wrong, search it on google :/
    Now I have tried to include "openNewWindow();", but it doesn't make any change. I think it has something to do with the user control then. How can I implement js in a user control? Does the js-code need to be in the aspx-file, which includes the user control or...?

  7. #7
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Re: JS and ASP.net

    VB Code:
    1. Button1.Attributes.Add("onClick", "openNewWindow()")

  8. #8
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Re: JS and ASP.net

    Quote Originally Posted by mendhak
    I've had a few problems when I didn't include that, especially when the button's onClick event contains other javascript function put in there by ASP.NET, so it's best practice to include it, although as you said, optional.
    I know what you mean. Have you ever had it not work in a browser because of the ;? I have. I try to use a function if I got more than on statement. It was like NN4 I think....
    Magiaus

    If I helped give me some points.

  9. #9
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: JS and ASP.net

    That just makes me glad I don't touch NN. In fact, it's been years since I've seen anyone using NN!

  10. #10
    New Member
    Join Date
    Feb 2005
    Posts
    9

    Re: JS and ASP.net

    Strider is right!

    Your button runs on the server and openNewWindow() is considered to be code that runs on the server. That's why it cannot be found.

    Remove the onClick="openNewWindow()" in your HTML tag and put the line and add the line of code Strider posted in you code behind page_load event.

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