Hi im looking to find out after I do this:
c#
div.Attributes.Add("onclick", "return confirm_delete();");
javascript:
function confirm_delete()
{
if (confirm("Are you sure you want to delete this comment?")==true)
return true;
else
return false;
}
It pops up with a message box with yes and no if I press yes how can I add a function into my c# how can I call it?
If yes go to c# code behind
and add in confirm_delete
if yes was clicked
do this
I need it so when some one clickes yes I can (from code behind in asp.net c#) add in a method to delete from mysql database.
Just dont know how I refrence to the javascript
public static string confirm_delete()
{
return something (dont know how to)
}
How about this add a button with the clientside click event instead of adding it in the code behind. Check to see if the popup returns true. If you return false it should prevent the server side event from firing.
<asp:Button ID="btn" OnClientClick="if(confirm_delete()){
/* post back*/
}else{
return false;
};" OnClick="btnDelete_Click" runat="server" Text="delete"/>
protected void btnDelete_Click(object sender, EventArgs e)
{
//run your serverside code if confirm was pressed.
}
Your JavaScript will need to call a page on your server. This is typically accomplished with AJAX and a web service.
This page on MSDN has a good overview of what's involved.
If you're interested in using jQuery, this page has a simple example.
You may be able to use a PageMethod to call your codebehind function, check out the following link for an example
ASP.NET Ajax PageMethods
Wondering why you are using a div? Can't you use asp.net Button / LinkButton and limiting it's postback based on confirm value?
e.g.
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete"
OnClientClick="return confirm('Are you certain you want to delete this
product?');">
</asp:LinkButton>
Reference.
Related
I try using href and posbackturl but it doesn't work . I started learning c# since last week so im still weak on it .Can anyone teach me how to solve it ?
<button class="button" style="vertical-align:middle"><span>Add Contact </span></button>
If you want to simply redirect to to the page, then simply you can forward it by just providing the page to href.
Add Contact
If you simply want to redirect you can use LinkButton.
<asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="~/addcontact.aspx">Add Contact</asp:LinkButton>
And if you want to perform some functionality on server side, then you may have to use OnClick event to bind with button which will look like this.
Web Form
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">Add Contact</asp:LinkButton>
C# Server Side
protected void LinkButton1_Click(object sender, EventArgs e)
{
//Functionaltiy
Response.Redirect("/addcontact.aspx");
}
I hope this will solve your problem.
I have an asp.net linkbutton, which contains the OnClientClick property, however the function within the OnClientClick never gets called, it directly jumps to OnClick function.
Below are the 2 ways I am using LinkButton as:
<asp:LinkButton ID="btn" CssClass="button" Text="Browse Thumbnail" runat="server"
OnClientClick="return confirm('Are you sure you want to delete?');">
</asp:LinkButton>
and:
<asp:LinkButton ID="lnkDelete" runat="server"
OnClientClick="return confirm('Are you sure you want to delete this slide?');"
CommandName="DeleteThumbnail" CommandArgument='<%# Container.DataItemIndex %>'>
<asp:Image ImageUrl="~/images/delete.gif" ID="imgDelete" runat="server"></asp:Image>
</asp:LinkButton>
Both of the approaches does not works.
Can anyone please provide some solution for the same.
OnClientClick="javascript:return confirmAction();"
function confirmAction() {
if(confirm('Are you sure you want to delete?')) {
// you clicked the OK button.
// you can allow the form to post the data.
return true;
}
else {
return false;
}
}
implement the Onclick on the server side
protected void lnkdelete_Click(object sender, EventArgs e)
{
}
and if you dnt want to call server method use this
OnClientClick="javascript:confirmAction(); return false;"
There is most probably some other page element that is preventing this event from being fired.
Do you have any other page elements that might interfere? Have you tried removing all other page elements but this one? Do you have some AJAX calls that might interfere? Have you tried this with a simple html element (not asp.net)?
You are most probably doing everything fine in your link button but it seems like problem is elsewhere.
Place it in single quotes like below,
<asp:LinkButton ID="btn" CssClass="button" Text="Browse Thumbnail" runat="server" OnClientClick="return confirm('Are you sure you want to delete?');"></asp:LinkButton>
Use like this
function Navigate()
{
javascript: window.open("microsoft.com");
return false;
}
and on clientclick as follows
OnClientClick="return javascript:Navigate()"
or even
function Navigate()
{
window.open("microsoft.com");
return false;
}
OnClientClick="return Navigate()"
Seems like you have Disabled the javascript in IE. Just enable it & you are good to go. You can follow this post to enable/disable the javascript in IE:
http://browsers.about.com/od/internetexplorertutorials/ss/disable-javascript-ie9.htm
There is no problem with your OnClientClick method it should prompt confirm window. but you haven't specify the onclick event of the link button. So you will not able to get the event from code behind.
You may need to enable java scripts for your browser
How to enable JavaScript in a web browser?
Are you using Ajax toolkit? Update panel? then you need to register script by using script manager
To inject a confirm script from a AJAX postback,
ScriptManager.RegisterOnSubmitStatement(btn, Page.GetType(), "confirm", "return confirm('Are you sure');");
I have a webpage (ASP.NET C#) that has a button:
<asp:Button ID="btnHide" runat="server" OnClick="HidePanel" Text="Hide"/>
and I'm doing a JavaScript alert like:
public void HidePanel(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(this.GetType(),"Hello","<script type=text/javascript> alert('Hello') </script>");
}
If I modify the function to not have object sender and EventArgs e then I can call it from Page_Load, and it works fine, I get the alert.
As written above I expect to see the alert when I click the button, but it's not happening. I'm sure it's something obvious, but I don't see it.
Use OnClientClick instead of OnClick. And add a return false to avoid a postback on the page.
<asp:Button ID="btnHide" runat="server" OnClientClick="alert('Hello'); return false;" Text="Hide"/>
You could try using,
ClientScript.RegisterClientScriptBlock(GetType(), "nameOfScript", scriptString);
I'ved used that before in a click event.
edit:
I can't edit posts yet, but your code works fine for me as well.
And depending on the situation he might want to do some server side stuff as well on the click event, or build up the script dynamically.
Edit:
If you already registered a script with the same name, it won't run, eg.
Page.ClientScript.RegisterStartupScript(this.GetType(), "Hello", "<script type=text/javascript> var i = 0;</script>");
Page.ClientScript.RegisterStartupScript(this.GetType(), "Hello", "<script type=text/javascript> alert('other hello') </script>");
You can remove the code to register the JavaScript code and instead do this:
<asp:Button ID="btnHide" runat="server"
OnClick="HidePanel" Text="Hide" OnClientClick="alert('Hello');"
UseSubmitBehavior="false" />
This:
UseSubmitBehavior="false"
will cause the button to fire the client-side script, and it will run the server-side code (post-back).
The question is simple but I can't find a simple answer to it! .. btw I'll need to pass a QueryString to the page to be open.
Any ideas ?
You can actually link a javascript code into .NET with C#, below is an example, you could replace with your info, and push the parameters.
Response.Write("<script type='text/javascript'>window.open('Page.aspx?ID=" + YourTextField.Text.ToString() + "','_blank');</script>");
You can append on the end of it ?Field=your value passing&nextField=another value.
Is the answer to do this in javascript. As you make the underlying page in asp.net, provide it with the javascript to catch the buttons onclick event and call window.open(URL)
It depends on what you're trying to do but the simplest is to use the OnClientClick property of the Button. Take a look at http://msdn.microsoft.com/en-us/library/7ytf5t7k.aspx, in particular the details bout this property a little bit down.
Basically you'd do something like
<asp:Button ID="Button1" Runat="server"
OnClientClick="ShowPopup();"
Text="Test Client Click" />
With the JS to do your popup
<script type="text/javascript">
function ShowPopup() {
window.open('ThankYou.aspx');
}
</script>
You can also do both an OnClientClick and an OnClick if you need as well.
<asp:Button ID="Button1" Runat="server"
OnClick="Button1_Click"
OnClientClick="ShowPopup();"
Text="Test Client Click" />
Code behind
protected void Button1_Click(Object sender, EventArgs e)
{
Label1.Text = "Server click handler called.";
}
Hi
I have one submit button to submit the page. When i click on submit button then it should give prompt message like "are you sure to submit that page really" if yes then it should redirect the another page.
I used code as follows:
RegisterStartupScript("myAlert", "<script>alert('Are you sure about to submit the test?')</script>");
Response.Redirect("Result.aspx");
But its directly redirecting the page without promting to user with message.
How its possible?
Asp.net c#
you can do like..
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="javascript:return
confirm('Are you sure about to submit the test?');" OnClick="Button1_Click" />
and then in code behind...
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Result.aspx");
}
There's a modalpopup in Ajax Control Toolkit, maybe that would work for you?
Sample : http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/ModalPopup/ModalPopup.aspx
Dont remember the exact syntax but you need to capture the clicked value and based on that you can do either a return true or a return false. True will submit the page