Confirm message in ASP.NET - asp.net

I wrote this statment in my code:
Response.Write("<script language=javascript>confirm('The system not allow negative inventory,continue?');</script>");
how can I handel if the user clicked "Ok" or "Cancel" button?

You should put this confirm to your submit button like that :
btnSubmit.Attributes["onclick"] +=
"return confirm('The system not allow negative inventory,continue?');"
If user click cancel, your page won't be postback.
But if you ask you can determine user's action at server side, the answer is no, not directly. You should add some trick, to get the user's action. Maybe you should set the user's action into a hidden field and at server side get this value and continue.

Restricting my answer to Javascript and not how the result of your code would interact with the flow of the page, the Javascript confirm method returns the value of the user's selected option:
var result = confirm('The system does not allow negative inventory. Continue?');
if (result == true)
// The user wants to continue. Proceed accordingly.
else
// The user does not want to continue.
You can use the value to branch your logic accordingly.

That code will execute on the client side. To know if the user clicked OK or cancel on the server, you will need to make your script send a request back to the server. You can't handle the dialog in the same request, because your code will have ended before the user sees anything in their browser.
You could output JavaScript like this:
location.href = 'Handler.aspx?confirmed=' + confirm('Do you want to do X?');
This will send the browser to either Handler.aspx?confirmed=true or Handler.aspx?confirmed=false.

On the onClientClick tag (in the aspx file) u can write
confirm('your message');
and onClick tag reference it to function in code-behind where you can write the function which will only be executed on OK click of the messagebox.
Using a javascript confirm function it is possible to allow a user to confirm or canel a button click. If the user presses yes on the message/alert box the button click even will be triggered on the server. If the no button is click no event will be triggered. This is especially useful with buttons used for deleting.
button.Attributes("onclick") = "javascript:return " & _
"confirm('Are You Sure you want to do this operation?') "

Related

ASP.NET Multiple links validate

I have about 50 or so links similar to the following:
<asp:TableCell Font-Bold="true"><asp:Hyperlink ID="Hyperlink9" runat="server" CssClass="Hyptext" Name="HypLink1" TextDecorations="None" NavigateUrl="~/Folde1/Second.aspx" Text="Case Study 12 "/></asp:TableCell>
What I like to do is when the user clicks on the hyperlink, I like to validate that the user has permission to view the links. If so, they can view it. If not, a pop-up will come information them that they need to sign in order to view the links.
Questions
1) What is the best way when the user clicks on the HyperLink to do a server side click event. From what I seen,only a client side even can be done with the asp:Hyperlink. I like to then validate if they have permissions. If not, throw a pop-up window.
You should not do that. Instead, you should...
On server side, check to see if user is logged in...
if not logged in, provide login link and text "Login to see case studies".
if already logged in, provide links user has access to.
You need an onClick tag that points to a method. Create one event handler function and point all your hyperlinks to it. Once the event handler fires, cast the sender to Hyperlink and go from there.

I have an ASP button for delete and its 'onclientclick' returns confirm to ensure deletion

I have an ASP button for delete and its 'onclientclick' returns confirm to ensure deletion, but I do something in server and display another confirmation to ensure deletion again. How can I write the second confirm in the same handler of the button?
use ClientScriptManager.RegisterStartupScript Method to add and script to page on run-time

Re-Enable button after submit

I'm employing a solution similar to the answer of OnclientClick and OnClick is not working at the same time ?
How this currently works is that a file is generated and returned to the client, however after the file is returned the button should become active again.
so the response contains the file. But the button does not reactivate.
I'm not certain if this can be done.
Any thoughts?
Thanks
You could set the Id mode of the button to static. Then choose a unique Id for the button. Whenever you have verified that the file is successfully transmitted you can call (jquery):
$("#idOfSubmitButton").attr("disabled", false);

Setting default button in asp.net 2.0

I have 3rd party user control (a captcha control), which has a captcha image, a text box within it.
I am using the above user control in my webpage. I have a 3 submit buttons on my webpage (Validate Captcha, Submit Page, Add User). When I click the Validate Captcha submit button using the mouse, I am validating whether captcha is empty and showing a javascript alert.
The problem comes when I enter the valid captcha text in the textbox and hit enter key when the cursor is in the textbox. The page just refreshes. I am unable to add keypress event to textbox and call Validate Captcha button event as I am using the 3rd party user control which I cannot modify.
Also, Page.ClientScript.RegisterHiddenField(...) will not work in my case as I have two other submit button inside the same page.
Only option left is to enclose these in panels and set default button.
Please let me know if anyone has any better options for achieving this.
Greetings! I too use alot of third party controls. The thing to remember about these controls, it that in the end they just emit HTML. This means you can use the DOM to access and attach event handlers such as onKeyPress. The trick is to identify how your control creator named the control you are looking for, in this case a {textbox}. The easiest way to achieve this is to simply run the page and view the page source. It is there that you can find the name as it is rendered and sent to the browser, after that all you have to do us use document.getElementByID to get the object and setup your handler
Example:
<script>
//Place this AFTER your textbox control is declared in the HTML
//Get the textbox
var textbox = document.getElementById('nameOfRenderedControlHere');
//Assign the event handler and function you want it to call
textbox.onclick = function() { validateCaptcha(); };
function validateCaptcha()
{ //Do your Stuff here }
</script>
That should be it..havent tested, let me knwo if you run into questions.
Put the captcha in its own <asp:Panel> and add a DefaultButton property for the panel with the ID of the captcha's submit button.

How to control page post from javascript?

Here's what I want to do. I want the users to be able to delete their own posts but I want them to confirm this first, so when the user clicks the Delete link, I want to display a confirmation dialog with Yes and No (the standard Javascript confirmation dialog), if the user chooses 'No' nothing happens and the page won't post back but if they choose 'Yes' the page should post back to the server, how can this be done with Javascript in ASP.NET?
Thanks
Edit:
Thanks a lot guys, I guess I'll go for OnClientClick="return confirm("Are you sure?");"
You need to (in your code behind):
Use the ClientScript object to get the postback reference for your button as a string
Modify the OnClientClick property of the button to include the followign javascript and the previously retrieved reference:
btn.OnClientClick = "return confirm('are you sure?');" + btnPostbackReferenceString;
Profit.
add your confirmation to your button like that :
submitButton.Attributes["onclick"] =
"if (!confirm('Do you want to continue ?')){return false;}";
if user selects no, confirm returns false, then your code returns false and the postback will be avoided.
Note that : if you return confirm('some text'), your postback will be avoided whatever user selects.

Resources