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

  • Thread starter Thread starter AAaron123
  • Start date Start date
A

AAaron123

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
 
Mark Rae said:
<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!');";


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
 
Mark Rae said:
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")) + "';");

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 said:
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!
 
Back
Top