Can't get onclick with window.location.href to work

Discussion in 'ASP .Net' started by AAaron123, Apr 29, 2009.

  1. AAaron123

    AAaron123 Guest

    I was told the following should work but I get an error message.
    I probably have something setup wrong but I can't imagine what.
    I've tried every variation I can think of but can't get it to work.

    Here is a copy/paste from my aspx file:
    <asp:button id="Button2" runat="Server" text="Remove" onclick='<%#
    "window.location.href='Events_view.aspx?Eventid=" & truncate(Eval("id")) &
    "'" %>' />

    Along with the error message it generates:

    Error 5 The server tag is not well formed. I:\My Documents\Visual Studio
    2008\WebSites\Test\Events\Events_List.aspx 39

    I'd appreciate even a suggestion of something that might work.
    Or a verification that the above does or does not give the message shown.

    Thanks
     
    AAaron123, Apr 29, 2009
    #1
    1. Advertising

  2. AAaron123

    AAaron123 Guest

    "Mark Rae [MVP]" <> wrote in message
    news:...
    > "AAaron123" <> wrote in message
    > news:...
    >
    >> I was told the following should work but I get an error message.
    >> I probably have something setup wrong but I can't imagine what.
    >> I've tried every variation I can think of but can't get it to work.
    >>
    >> Here is a copy/paste from my aspx file:
    >> <asp:button id="Button2" runat="Server" text="Remove" onclick='<%#
    >> "window.location.href='Events_view.aspx?Eventid=" & truncate(Eval("id"))
    >> &
    >> "'" %>' />
    >>
    >> Along with the error message it generates:
    >>
    >> Error 5 The server tag is not well formed. I:\My Documents\Visual Studio
    >> 2008\WebSites\Test\Events\Events_List.aspx 39
    >>
    >> I'd appreciate even a suggestion of something that might work.
    >> Or a verification that the above does or does not give the message shown.

    >
    > <asp:Button /> is a WebControl, not an HTMLControl. As such, if you give
    > it an OnClick tag, it will look for a server-side method to run. If you
    > want a piece of client-side JavaScript to run when an <asp:Button /> is
    > clicked, you need to either use the server-side Attributes.Add method, or
    > the OnClientClick tag:
    >
    > <asp:Button ID="cmdHelloWorld" runat="server" Text="Hello world!"
    > OnClientClick="alert('Hello world!');" />
    >
    > However, you're trying to inject server-side text into the webcontrol, so
    > you might be better off doing this server-side:
    >
    > cmdHelloWorld.Attributes.Add("onclick", "alert('Hello world!');";
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net



    I've been trying to do this by myself asking questions as I go but maybe it
    would be better to ask for basic directions first.
    What I doing is trying to replace a HyperLink with a button. This HyperLink
    works:
    <asp:HyperLink id="RemoveBtn" runat="server" Text="Remove" NavigateUrl='<%#
    "Events_Edit.aspx?Action=Remove&id=" & truncate(Eval("id")) %>' />


    This does not open the page (Page_Load never runs)
    <asp:button id="Button3" runat="Server" text="Remove" onclick='<%#
    "window.location.href=" & Chr(39) & "Events_Edit.aspx?Action=Remove&id=" &
    truncate(Eval("id")) & Chr(39) %>' />



    Can you point me in a better direction?



    Thanks
     
    AAaron123, Apr 30, 2009
    #2
    1. Advertising

  3. AAaron123

    AAaron123 Guest

    "Mark Rae [MVP]" <> wrote in message
    news:%...
    > "AAaron123" <> wrote in message
    > news:OXnw%...
    >
    >> I've been trying to do this by myself asking questions as I go but maybe
    >> it would be better to ask for basic directions first.
    >> What I doing is trying to replace a HyperLink with a button. This
    >> HyperLink works:
    >> <asp:HyperLink id="RemoveBtn" runat="server" Text="Remove"
    >> NavigateUrl='<%# "Events_Edit.aspx?Action=Remove&id=" &
    >> truncate(Eval("id")) %>' />
    >>
    >> This does not open the page (Page_Load never runs)
    >> <asp:button id="Button3" runat="Server" text="Remove" onclick='<%#
    >> "window.location.href=" & Chr(39) & "Events_Edit.aspx?Action=Remove&id="
    >> & truncate(Eval("id")) & Chr(39) %>' />

    >
    >> Can you point me in a better direction?

    >
    > As I've already explained...
    >
    > <asp:HyperLink />, as its name suggests, creates a client-side hyperlink
    > i.e. <a href="...." /> That's why it works.
    >
    > <asp:Button />, as its name suggests, creates a button. If you give it an
    > OnClick tag, it will try to postback and run the server-side method
    > specified in the OnClick tag. That's why it doesn't work as you've written
    > it.
    >
    > If you want it to run a client-side function when you click an
    > <asp:Button>, you have to either give it an OnClientClick tag:
    > http://www.google.co.uk/search?sour...LL_en-GBGB323GB323&q=asp:Button OnClientClick
    >
    > <asp:Button ID="cmdRemove" runat="server" Text="Remove"
    > OnClientClick="window.location.href='Events_Edit.aspx?Action=Remove&id=<%=Truncate(Eval("id"))
    > %>';" />
    >
    > or generate its client-side onclick method via the server-side
    > Attributes.Add method:
    > http://www.google.co.uk/search?hl=en&rlz=1T4GGLL_en-GBGB323GB323&q=asp:Button Attributes&meta=
    >
    > cmdRemove.Attributes.Add("onclick",
    > "window.location.href='Events_Edit.aspx?Action=Remove&id=" +
    > Truncate(Eval("id")) + "';");
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net


    Here is what happened and why I asked for confirmation.

    I changed to OnClientClick and it still didn't open the page.
    I had been working on this a couple of days so I had already tried
    everything I could think of.
    Actually, I had tried OnClientClick before I posted.

    During those couple of days I learned a lot.
    However, I still had one more thing to find out.
    The question mark in my querystring had spaces around it.
    When I removed them the OnClientCliclk worked OK.

    Thanks

    BTW

    I know you have nothing to do with the documentation but I want to mention
    why the Button.OnClientClick doc didn't help me.
    It refers only to client-side script. Never mentioning the term
    server-side.
    Although the example does help.
    The doc says:
    "Gets or sets the client-side script that executes when a Button control's
    Click event is raised. "
    Which I don't understand.

    I think I just saw, maybe, what that means: it add script to the page when
    the event is raised.
    But that is not all it can do, is it.
     
    AAaron123, May 1, 2009
    #3
  4. AAaron123

    AAaron123 Guest

    "AAaron123" <> wrote in message
    news:%...
    >
    > "Mark Rae [MVP]" <> wrote in message
    > news:%...
    >> "AAaron123" <> wrote in message
    >> news:OXnw%...
    >>
    >>> I've been trying to do this by myself asking questions as I go but maybe
    >>> it would be better to ask for basic directions first.
    >>> What I doing is trying to replace a HyperLink with a button. This
    >>> HyperLink works:
    >>> <asp:HyperLink id="RemoveBtn" runat="server" Text="Remove"
    >>> NavigateUrl='<%# "Events_Edit.aspx?Action=Remove&id=" &
    >>> truncate(Eval("id")) %>' />
    >>>
    >>> This does not open the page (Page_Load never runs)
    >>> <asp:button id="Button3" runat="Server" text="Remove" onclick='<%#
    >>> "window.location.href=" & Chr(39) & "Events_Edit.aspx?Action=Remove&id="
    >>> & truncate(Eval("id")) & Chr(39) %>' />

    >>
    >>> Can you point me in a better direction?

    >>
    >> As I've already explained...
    >>
    >> <asp:HyperLink />, as its name suggests, creates a client-side hyperlink
    >> i.e. <a href="...." /> That's why it works.
    >>
    >> <asp:Button />, as its name suggests, creates a button. If you give it an
    >> OnClick tag, it will try to postback and run the server-side method
    >> specified in the OnClick tag. That's why it doesn't work as you've
    >> written it.
    >>
    >> If you want it to run a client-side function when you click an
    >> <asp:Button>, you have to either give it an OnClientClick tag:
    >> http://www.google.co.uk/search?sour...LL_en-GBGB323GB323&q=asp:Button OnClientClick
    >>
    >> <asp:Button ID="cmdRemove" runat="server" Text="Remove"
    >> OnClientClick="window.location.href='Events_Edit.aspx?Action=Remove&id=<%=Truncate(Eval("id"))
    >> %>';" />
    >>
    >> or generate its client-side onclick method via the server-side
    >> Attributes.Add method:
    >> http://www.google.co.uk/search?hl=en&rlz=1T4GGLL_en-GBGB323GB323&q=asp:Button Attributes&meta=
    >>
    >> cmdRemove.Attributes.Add("onclick",
    >> "window.location.href='Events_Edit.aspx?Action=Remove&id=" +
    >> Truncate(Eval("id")) + "';");
    >>
    >>
    >> --
    >> Mark Rae
    >> ASP.NET MVP
    >> http://www.markrae.net

    >
    > Here is what happened and why I asked for confirmation.
    >
    > I changed to OnClientClick and it still didn't open the page.
    > I had been working on this a couple of days so I had already tried
    > everything I could think of.
    > Actually, I had tried OnClientClick before I posted.
    >
    > During those couple of days I learned a lot.
    > However, I still had one more thing to find out.
    > The question mark in my querystring had spaces around it.
    > When I removed them the OnClientCliclk worked OK.
    >
    > Thanks
    >


    Please ignore the following BTW.
    I think I finally see what is confusing me.
    I do execucte a client side function.
    window.location.href

    Somehow it looked like a postback to me because the the server-side load
    event ran.

    I know it makes no sense.

    Thanks a lot!




    > BTW
    >
    > I know you have nothing to do with the documentation but I want to mention
    > why the Button.OnClientClick doc didn't help me.
    > It refers only to client-side script. Never mentioning the term
    > server-side.
    > Although the example does help.
    > The doc says:
    > "Gets or sets the client-side script that executes when a Button control's
    > Click event is raised. "
    > Which I don't understand.
    >
    > I think I just saw, maybe, what that means: it add script to the page when
    > the event is raised.
    > But that is not all it can do, is it.
    >
    >
    >
    >
    >
    >
    >
    >
    >
     
    AAaron123, May 1, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joshua Beall
    Replies:
    26
    Views:
    222,657
    AndreiKaa
    Jun 29, 2007
  2. Chuck

    onclick for button and window.location.href

    Chuck, Feb 2, 2010, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    6,681
    Chuck
    Feb 2, 2010
  3. Jiong Feng
    Replies:
    9
    Views:
    1,511
    Lasse Reichstein Nielsen
    Jun 24, 2004
  4. saiho.yuen
    Replies:
    3
    Views:
    472
    kaeli
    Sep 14, 2004
  5. Replies:
    2
    Views:
    542
Loading...

Share This Page