0

I have a Submit button with code behind that evaluates stuff in c#, the problem is that I also want to add a function in javascript that asks "This file already exists, do you want to replace it?

So my question is how can I trigger both?

OnClick="myC#mehod" and also OnClientClick =  return (some javascript);

Can I do this?...

1
  • Please don't put "ASP.NET C#" and such in your title. Leave it in the tags, where it belongs. Commented Jun 28, 2011 at 15:16

3 Answers 3

1
deleteBtn.Attributes["onClick"] = 
    "return confirm('This file already exists, do you want to replace it?');"

Updated for clarification;

<asp:Button ID="btnReplace" runat="server" onclick="btnReplace_Click" Text="Button" OnClientClick="return confirm('This file already exists, do you want to replace it?');" />

protected void btnReplace_Click(object sender, EventArgs e)
{
//Replace the File
}
Sign up to request clarification or add additional context in comments.

8 Comments

Great and how do I evaluate if they clicked yes or no? something like if they confirmed = do this if not do nothing
if it goes to OnClick is which "myC#mehod" then user pressed "OK" otherwise it will stop processing if user pressed "Cancel".
have you defined the eventhandler for server_click as well? You need both.
I have one event handler for OnClick="on_Submit", what else do I need? can you elaborate.. I am kind of a newbie thank you
Thank you Chris, the problem with this is that we are always assuming we want to replace the file but it is based on a condition, if the old file name equals the new file name then it should pop that up....
|
0
OnClientClick="return confirm('This file already exists, do you want to replace it?');" 

Comments

-2
OnClientClick = "if(!confirm('This file already exists, do you want to replace it?')) return false;"

this is more safer than return direct

3 Comments

How so? If they click 'Yes', it returns true. If they click 'No', it returns false; if(!true) return false just adds an additional evaluation and no extra functionality
I've read it before that if you add return at the beginning might makes some problems with some old browsers. I'm just returning based on a condition. If we use return at the beginning this means I should return something either false or true and don't continue to the next statement which is the default behavior/code for the button. So some old browsers doesn't understand that then it breaks the method even if it returned true.
check this post answer to validate my words stackoverflow.com/questions/3730518/… it is returning in all cases

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.