Javascript alert message not showing up - javascript

I have a page written in asp.net, in which I am calling Javascript alert message and it is not showing up, if I clear cache memory of browser then the alert message is showing up.
Can anybody tell the reason and solution?

Use onClientClick like this
<asp:Button ID="btnSubmit" OnClick="btnSubmit_click()" OnClientClick="return check()"
runat="server"/>
<script type="text/javascript">
function check() {
if (confirm('Are you sure you want to submit ?')) {
// do further form validation here..
return true;
}
else {
return false;
}
}
</script>

Related

How to handle SweetAlert confirmation button using asp:button

I am using js alert for delete confirmation but i would like to use SweetAlert2 for new confirmations. I just want to know how can i use SweetAlert2 confirmation alert and handle confirm button on asp.net webform.
Here is button:
<asp:Button ID="deleteBtn" runat="server" Text="Delete" OnClick="deleteBtn_Click" />
Code Behind: (Working JS)
if(!IsPostBack)
{
deleteBtn.Attributes.Add("onclick", "return confirm('are you sure');");
}
I found a solution for displaying basic message with SweetAlert2:
if(!IsPostBack)
{
deleteBtn.Attributes.Add("onclick", "return swal('are you sure?');");
}
Ask to the user if you want to delete record:
if(!IsPostBack)
{
deleteBtn.Attributes.Add("onclick", "return swal({title:'are you sure?', buttons:{confirm:{text:'i am sure'}, cancel{text:'cancel'}});");
}
How can we handle confirmation button from code-behind? How can we use cancel button? I can delete a record even if i can click on cancel button.

How do I trigger runat="server" without having access to add this code to the HTML button?

I am adding a Google reCAPTCHA to a form, but doing it through a CMS system (Luminate Online- which is awful). I have no access to edit the HTML button code, so I am unable to add the code runat="server" into the button, so the form does not validate. Is there another way to do this?
Here is my code:
<head runat="server">
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script type="text/javascript">
function get_action() {
var v = grecaptcha.getResponse();
console.log("Resp" + v);
if (v == '') {
document.getElementById('captcha').innerHTML = "You can't leave
Captcha Code empty";
return false;
}
else {
document.getElementById('captcha').innerHTML = "Captcha
completed";
return true;
}
}
</script>
HTML: (I can add HTML through a caption in the CMS, but am just unable to edit the button directly)
<div class="g-recaptcha" data-
sitekey="SITEKEYHERE"></div>
Basically, I have this working on a test page when I put runat="server" into the submit button. However, I need to get this working through the CMS and I am unable to edit the submit button. So, right now, anyone can submit the form without checking the reCAPTCHA.
runat="server" is not a standard HTML attribute. It only applies to certain ASP.NET systems.
If your CMS does not support reCaptcha, you're out of luck. There's no way to add that support through HTML alone.

Disconnect warning message in my master page

I have to ask the user if he realy want to logout when he click on "logout".
My logout is on my navbar on my masterpage so I must do that with a link. Before I called an another page and on the page_load I put the session variable at Null then I redirect to the log Page.
But It's not realy good :
1- I can't call my javaScript function just with an onload
2- If the user cancel the disconnect I redirect him on a page but he loose the actual work if he didn't save
The JS code to ask :
<script type = "text/javascript">
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Êtes vous bien sûr de vouloir vous deconnecter ?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
The best solution is a solution who permit to stay on the actual page, ask to the user with the js code ( or an other solution ) and if he say Yes, disconnect him.
There is a LoginStatus Control in aspnet. You can add a confirm dialog there.
<asp:LoginStatus ID="LoginStatus1" runat="server" onclick="return confirm('Really logout?')"
OnLoggedOut="LoginStatus1_LoggedOut" />
It also works with a normal LinktButton
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="Button1_Click"
OnClientClick="return confirm('Really logout?')">Logout</asp:LinkButton>
I have found a very simply solution, I didn't know that was possible, but it's very usefull !
<li><a href="../act_deconnexion.aspx" onclick="return confirm('Are you sure?')" >Deconnexion</a></li>

Search keywords with enter key - BUG

So, i'm developing a search feature in my website, and i want to redirect the user to a page with the keywords he is trying to search.
The following code works, but there is a bug: when i click enter in my homepage it works. The user is successfully redirected to my search page with the results. But when i'm in that page (or any other page) the enter key does not work! Only the button works all the time! It's a strange error and i dont know what to do...
This code is in my masterpage (.net 4.0) and it is the parent of all my webforms (including home). Can anyone help me?
<asp:TextBox ID="TBPesquisa" runat="server" placeholder="Pesquise produtos aqui" onkeypress="return runScript(event)"></asp:TextBox>
<button id="BTPesquisa" class="button-search" type="button" onClick="javascript:window.location.assign('/Pesquisa?val='+encodeURIComponent(document.getElementById('TBPesquisa').value));">Pesquisar</button>
<script>
function runScript(e) {
if (e.keyCode == 13) {
$("#BTPesquisa").click();
}
}
</script>
Probably, the reason is that the TBPesquia field wasn't found, as this is an asp.net textbox control, and the ID might not be what you expect it to be.
Now you could either go for setting the ClientID property, like:
<asp:TextBox ID="TBPesquisa" ClientID="TBPesquisa" runat="server" placeholder="Pesquise produtos aqui" onkeypress="return runScript(event)"></asp:TextBox>
or you could rewrite your handler slightly, by doing the following
<asp:TextBox ID="TBPesquisa" runat="server" placeholder="Pesquise produtos aqui" onkeypress="runScript(event)"></asp:TextBox>
and change the runscript to:
function runScript(e) {
var source = e.target;
if (e.keyCode === 13) {
searchFor( source.value );
e.preventDefault();
}
}
function searchFor( textValue ) {
window.location.assign('/Pesquisa?val=' + encodeURIComponent(textValue));
}
I would btw rather put the code for searching outside of the searchbutton itself, it only makes sense to have this outside of your html element

Show confirmation and redirection on same time

I have few questions,
I want to show the alert window on my Update record button click.But
i already i have onClientClick event set for button.So is there
any other way to do that.
Using Response.Write() can i do some thing which is show me
message of updation as well as redirect to my other page.
Can i use ClientScript.RegisterStartupScript methode to do that
functionality? and how
thanks in advance
use OnClientClick and confirm() js
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" OnClientClick="return confirm('Are You Sure You Want to Update?')" />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" OnClientClick="return Validate()" />
<script>
function Validate()
{
//do validation here
if(notPassValidation)
{
alert(validationMessage);
return false;
}
//valifation passed, do confirmation
if(!confirm('Are You Sure You Want to Update?') )
{
return false;
}
else
{
return true;
}
}
</script>
I solved this question by handling the client and server events :)
thanks for all response.

Categories