Simple 'Confirm' on Link Button click

I have a simple Link Button which fires even "LinkButton1_Click" as follows:

 

Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
if("javascript:return confirm('Are you sure?');") then
'do some stuff

Response.Redirect("
another page")
else
'do some other stuff
Response.Redirect("
another page")
end if
end sub

How can I make something like this work? I'm new to web development, and this is probably a simple problem. Obviously, the site chokes on my current setup when the button is clicked. Or is this a client vs. server side problem? I searched the forums, but couldn't find a solution that seemed to fit my simple needs.

Any help greatly appreciated.

CJ

0
CJones79
7/9/2008 4:51:22 PM
asp.net.web-forms 93655 articles. 6 followers. Follow

4 Replies
607 Views

Similar Articles

[PageSpeed] 9
Get it on Google Play
Get it on Apple App Store

 Hi,

Use an AJAX confirm button extender - does it all for you and very easy to implement.

Click on AJAX at the top of the page and download the bits. there is a tutorial here http://www.asp.net/learn/ajax-videos/video-119.aspx


I hope this helps, if it answers your question, don't forget to mark it as such for those who come afterwards.

Regards,

Gareth Chidgey
Managing Director
PushInternet Ltd
0
chidge
7/9/2008 5:01:19 PM

Hi,

"Or is this a client vs. server side problem?"

Yes, because javascript runs on client and the code otherwise on the server. This is something easier to do with javascript which is attached to LinkButton's OnClientClick attribute (runs on client when LinkButton is clicked). For example:

<asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="if(confirm('Are you sure?')){...} else {...}">Click Me</asp:LinkButton>

 


Thanks,

Teemu Keiski
Finland, EU
0
joteke
7/9/2008 5:03:46 PM

You can simply use the OnClientClick property of a linkbutton like this:

 

<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click" 
                OnClientClick=" return confirm('Are you sure?');" Text="Click me..." />
  

Then in the Click event of LinkButton1, you say:

 

Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Response.Redirect("some page")
End Sub
  
0
webberman
7/9/2008 5:08:32 PM

Thanks for the fast response.  I am now using the OnClientClick for the LinkButton.  I'll look into the AJAX option once I get the project up and running.

Thanks!

CJ

 

0
CJones79
7/9/2008 5:45:41 PM
Reply: