Confirmation message from the ASP.NET code behind file - javascript

I have a login screen Login.aspx after login I want to show a confirmation message saying
"You are being redirected to the Demo page. Do you want to continue?",
if the user clicked yes, I need to redirect them to a demo page. Other wise redirect to another home page. How can I do this?
I want something like the following...
protected void Login_Click(object sender, ImageClickEventArgs e)
{
//some validation and calculations..
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "confirm('You are redirecting to Demo page ". Do you want to continue" ?');", true);
//My doubt is this
If(ok button clicked)
{
//redirect to demo page
}
else
{
//redirect to home page
}
}

You wont get the result of confirm on server side. You can call a javascript function RegisterStartupScript and put this confirm dialog in that function.
Although you can somehow send value of confirm on server side using postback of some ajax call but I do not see any reason for that.
Code behind
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "RedirectDecision();", true);
Html
<script type="text/javascript">
function RedirectDecision()
{
if(confirm('You are redirecting to Demo page ". Do you want to continue" ?'))
window.location = "http://www.yoururlFirst.com";
else
window.location = "http://www.yoururlSecond.com";
}
</script>

You can't do that. You can call a client side function by setting OnClientClick. If user will press confirm than code behind function will be called. Have a look at below example
<asp:Button ID="Button2" OnClick="Login_Click" OnClientClick="return UserConfirmation()" runat="server" Text="Login" />
function UserConfirmation() {
if(confirm('You are redirecting to Demo page ". Do you want to continue" ?'))
return true;
else
return false;
}
Here if user will press ok than code behind function will be called other wise it won't

Related

redirect the page on Java script function alert box (OK) click

below is my java script function on aspx page
<script>
function alert6() {
alert("You Can't Delete This Record");
}
</script>
and use the function on my cs page, i want to redirect the page after the user clicks on OK what i Need to do?
Page.ClientScript.RegisterStartupScript(this.GetType(), "alert5", "alert6();", true);
Simply if you want to redirect the page just after clicking on Ok button of alert box then try this
<script>
function alert6() {
alert("You Can't Delete This Record");
window.location="Your page";
}
</script>
and call it
Page.ClientScript.RegisterStartupScript(this.GetType(), "alert5", "alert6();", true);
I don't have much knowledge on ASP .NET. But as I understand, you are trying to print out a javascript function to the page which would redirect the page when the OK button is clicked.
As you said you are using JQuery you can use the function below. I am assuming the id of the OK button is 'btnOk'.
$("#btnOk").click(function(){
window.location = "http://www.yoururl.com";
});

Confirm Box in Asp.Net

CODE
protected void btnSelectInvioces_Click(object source, EventArgs
{
try
{
// some code here
if(a == b)
{
// open confirmation box
if(OK) // i click ok button of confirm box
{
// proceed further...
}
}
}
catch()
{ }
}
I need to have a Confirm box only when satisfying a condition from server side.
I tried using RegisterClientScript.
What is happening is, it is executing my whole code and then giving me pop up.
BUT, I want to open popup at certain point. When it opens, further code should not be executed unless and until I click OK/Cancel button of that confirmation box
Your confirm box is client side. And the code that you shown is server side.
You need ajax for this purpose. If you need help there then let me know.
Remember alert, confirm and other things that are offered by your browser is client side. In your javascript you can do something like this,
if(confirm("press OK"))
{ ajax call }
in ajax call you can execute your server side code that starts after your condition if(OK)
You can use Ajax Confirm button extender with full customization http://www.ajaxcontroltoolkit.com/ConfirmButton/ConfirmButton.aspx
Or if you want to call in between button click process with code condition then u can use
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ajax", "callconfirm()", true);
if you get ok then call with your ajax
if(confirm("OK")) { ajax call }
First add a hidden field, and set its value based on user pressed confirmation button
Html Markup:
<asp:HiddenField ID="HiddenField1" runat="server" />
Client side: Set the result in hidden field
function ConfirmMessage() {
if (confirm("Are you sure ?")) {
$("#HiddenField1").val("Yes");
} else {
$("#HiddenField1").val("No");
}
}
Code-behind: Retrieve the hidden field value and make your call
string confirmValue = HiddenField1.Value
if (confirmValue == "Yes")
{
// logic code here
}
else
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You pressed NO!')", true);
}
using javascript like this:
<script type="text/javascript">
function ConfirmBox() {
if (confirm("Continue?")) {
alert("Yes");
} else {
alert("No");
}
}
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
And in the code behind you can use ScriptManager.RegisterStartupScript like this:
protected void Button1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "ConfirmBox", "ConfirmBox();", true);
}
Implement Confirmation Dialog Box Using Javascript
Please refer given URL
http://www.freshcodehub.com/Article/47/implement-confirmation-dialog-box-using-javascript

ASP.NET redirect from event handler hot by button clicked by JavaScript

I have a problem with a page not redirecting during a postback.
I have an aspx-page with this content being part of it:
<td class="tdinput" align="right">
<asp:Button ID="ButtonFrigiv" runat="server" Text="Frigiv sag" CssClass="button"
OnClientClick="verifyReleaseOtherEmployeesCase()" />
<asp:Button runat="server" ID="ButtonFrigivHidden" OnClick="ButtonFrigiv_Click"
Style="display: none;" UseSubmitBehavior="true" />
</td>
The page also contains various UpdatePanels, however the above code is not included in one.
As it is seen there's a button being displayed, which calls the following JavaScript:
function verifyReleaseOtherEmployeesCase() {
var logonUser = document.getElementById('<%= HiddenLogonUser.ClientID %>');
var lockedBy = document.getElementById('<%= HiddenLockedBy.ClientID %>');
var button = document.getElementById('<%= ButtonFrigivHidden.ClientID %>');
if (logonUser.value == lockedBy.value) {
button.click();
}
else if (confirm("Danish confirm message")) {
button.click();
}
else {
return;
}
}
So basically this JavaScript locates the 2nd button above (which is not being displayed to the user) and clicks it.
When the button is clicked the following event handler is reached on the server:
protected void ButtonFrigiv_Click(object sender, EventArgs e)
{
NyeSagerDAC.FrigivSag(SagID, LogonUser);
Response.Redirect(LastList);
}
Where LastList is a string containing the page which was the entry point for the current page (there are multiple entry points).
The server method is reached and the first line in the method is executed perfectly, however the page is not redirected.
I have done some testing regarding whether it might have been considered an AJAX-callback (which doesn't allow redirects) using the following code in the event handler:
HttpRequest request = HttpContext.Current.Request;
string header = request.Headers["X-MicrosoftAjax"];
The content of the string header is empty and the Page_Load is called after the event handler is done. So I don't think this is the problem.
I have also tried to register a startup script to have the page redirect itself instead. This has been tried in various ways:
Page.ClientScript.RegisterStartupScript(GetType(),"redirect",
"window.location.href='" + LastList + "'");
And
ScriptManager.RegisterStartupScript(this, this.GetType(), "redirect",
"window.location.href='" + LastList + "';", true);
However none of this seems to work.
The only thing I have got to work was when I removed the confirm() part of the JavaScript and had it just click the button. This resulted in the page being correctly reloaded. However, I need to ask the user actually wants to unlock another employee's task.
So I think my question is: How can I get the page to redirect? Alternatively, how can I modify the JavaScript to still ask the user to confirm and not stopping the redirect from working?
The solution is coded in .NET 4.0
Thanks in advance.
Your js function is named verifyReleaseOtherEmployeesCase but in the OnClientClick it says verifyReleaseOtherWorkersCase, assuming that is a typo you also need to prevent the first button from performing a postback.
Notice the return of false in the js function and the return in the OnClientClick
function verifyReleaseOtherEmployeesCase() {
var logonUser = document.getElementById('<%= HiddenLogonUser.ClientID %>');
var lockedBy = document.getElementById('<%= HiddenLockedBy.ClientID %>');
var button = document.getElementById('<%= ButtonFrigivHidden.ClientID %>');
if (logonUser.value == lockedBy.value) {
button.click();
}
else if (confirm("Danish confirm message")) {
button.click();
}
return false;
}
<asp:Button ID="ButtonFrigiv" runat="server" Text="Frigiv sag" CssClass="button"
OnClientClick="return verifyReleaseOtherEmployeesCase()" />

Alert message on button click event

I have a button and textbox on my page aspx and on button click event I am displaying alert message.
Now when I run website and click on button it display message but after that when I press F5 (Refresh) it again display message. So my question how to remove message when I click F5.
Below is my code for button click:
protected void btnExport_Click(object sender, EventArgs e)
{
DisplayMessage("There is no data to Export.", this);
}
and
public void DisplayMessage(String strMessage, Control name)
{
string script = "<script language='javascript'>alert('" + strMessage + "');</script>";
ScriptManager.RegisterStartupScript(name, name.GetType(), "JSCR", script, false);
}
Change your script as follows
string script = "alert('hello');window.location.href='Default.aspx'";
where i assumed Default.aspx is your page
You are instigating a client side event from server side which usually does not make sense.
The behaviour you are noticing is correct, when you clicked that button a request is posted to the server which calls your button click handler which then registers the client side script when the response is returned. When you press F5 to refresh your browsers is posting that exact same request again to the server, so the button handler is fired again.
I advise you read a little about how Post And Gets
work as well as how postbacks work in ASP.

Alert Message is not showing

I am new in development, I want to show the alert message on button click after that I want to redirect the on another page. my button code is like below:
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
Response.Write("<script type='text/javascript'> alert('Please wait for approval!');</script>");
Response.Redirect("~/profile/scrapbook.aspx?uid=" + Request.QueryString["uid"], false);
}
in this case alert is not working, If I remove the Response.Redirect then it will works properly.
Please suggest me how to call he alert message after that redirect on another page.
You are mixing server side and client side code.
The Response.Redirect("~/profile/scrapbook.aspx?uid= is server side and the Alert won't wait the action of the user to execute it...
What you will need is to do you redirect in Javascript or to use something else than an Alert for the message.
Solution 1
You display a Alert message in Javascript (client side) when the user press okay you do a redirect in Javascript.
Solution 2
You display a message in the HTML with a button in ASP with an event that will do a call to the server and redirect your user to the page you desire.
Add your script like this:
const string scriptString = "<script type='text/javascript'> alert('Your friend request sent to the user! Please wait for approval!');</script>";
ClientScriptManager script = Page.ClientScript;
script.RegisterClientScriptBlock(GetType(), "randomName", scriptString);
Do not use Response.Write to render your JavaScript. Use RegisterClientScriptBlock or RegisterStartupScript instead.
Your example should be using RegisterStartupScript, since it's rendering executing script, and not function declarations.

Categories

Resources