Asp.Net AlertBox code behind page? - asp.net

I have a buttonfield in a gridview. When the button is clicked the RowCommand function gets called.
I need to pop up an alert box to make the user confirm their choice. I am pretty sure there is an HtmlInputButton involved but I kind of just need the syntax.
After that, how do you know whether they confirmed?

In your aspx, put this on your button :
OnClientClick="javascript:return confirm('are you sure ?')"

You can find the button, via the ItemDataBound event and then add a confirmation box.
button1.Attributes.Add("OnCLick", "javascript: return confirm('Are You Sure You Want to Delete");")
That should get you what you need.

You can also use the ConfirmButtonExtender if you're using the Ajax Control Toolkit
If you're using jQuery (which I'd totally recommend) you should check out jQuery UI and the modal confirmation demo which is very slick.
It should be noted that the above confirmations are purely client-side, so if you need to support clients that have javascript turned off, you will need to handle all of this server-side as well. e.g. the javascript, when OK is clicked, could set a hidden form field that says the equivalent of 'user confirmed' so if you get the delete button pressed without the hidden form field being filled you can determine that you need to show a server-generated confirmation field.

try this
ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "Alert", "alert('" + message+ "')", true);

Related

ASP .Net Textbox Textchanged event

I have a webpage. I show records from table, lets say, students in my page. I query all the students and show them in grid. I want to use a textbox for filtering the datagridview results. For example if the user types a in the textbox the grid will show only the students who has "a" in his/her name. I want to refresh the grid at the same time while textbox is being edited.
i have set the autopostback property of textbox to true, and i refresh the grid in textbox's textchanged event.But the textchanged event fires only after the textbox loses focus. How can I make it fire after user types just one character ? thanks.
You have to use the onKeyDown event. However, I'd advise you to use ASP.NET AJAX or jQuery to load the results with Ajax.
Here is one example from asp.net: http://www.asp.net/ajaxlibrary/AjaxControlToolkitSampleSite/AutoComplete/AutoComplete.aspx
Another one, from Code project:
http://www.codeproject.com/Articles/38803/Google-Like-Search-TextBox
You might want to show some your present code, if there is a particular method you want to go with for this. Otherwise your going to get a people telling you the way they would do it.
Does it look something like this right now?
<asp:Textbox id="myTextbox" runat="server" onChange="txtChanged" AutoPostBack="true"/>
public void txtChanged(object sender, EventArgs e)
{
//Get text from textbox
string text = ((TextBox)sender).Text;
//Do what ever it is you want to do to edit the text
text = text.ToUpper();
//Update the other textbox with this text
txtMyText2.Text = text;
}
I think the best and most clean way is to use Rad Controls, here is an example on how to do it:
http://demos.telerik.com/aspnet-ajax/controls/examples/integration/gridandcombo/defaultcs.aspx?product=grid
The event TextChanged only fires when you send a request to server. If you wanna launch an event or make a function when the text inside textbox changes, use an OnKeyDown event (right with Schiavini).
You can use PicNet to do this in the Client instead of the Server for a better User experience. You can find it here http://www.picnet.com.au/resources/tablefilter/demo.htm Remember that the Gridview is rendered as a HTML table, therefore you can freely use this jQuery plugin.
Good luck!

ASP.NET button click fire jQuery

I have a jQuery Pager control, for each page there is a set of textboxes and a submit button. When this submit button is clicked I want it to fire off some jQuery to update the controls on the front end (i.e. current page, set visibility of controls etc). The button is an asp.net web forms postback button.
Does anyone know any way of doing this?
Merry Christmas!
Inject js from code-behind after your postback success like:
string script = "$(function(){setPage(\"" + yourpagenumber+ "\");});";
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "Pager", script, true);
And in the js you will have a setPage function that does the job of setting page with your jquery pager plugin.
function setPage(pagenumber){
alert(pagenumber);
//do your page setting here
}
Note: you can use Page.ClientScript.RegisterStartupScript if that fits your needs but the idea remains same.
Don't use a submit button, use an input of type button with an onclick event instead.
Use the OnClientClick attribute of the asp:button i.e.
OnClientClick="JQueryFunction();return false;"
for no postback and
OnClientClick="JQueryFunction();return true;"
for a postback. I'm assuming that that JQueryFunction() returns true.

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.

Preventing accidental double clicking on a button

I have a few controls that inherit from ASP.NET buttons and use onserverclick.
If the user clicks twice, the button fires two server side events. How can I prevent this?
I tried setting this.disabled='true' after the click (in the onclick attribute) via javascript, but that blocks the first postback as well.
See this example for disabling control on postback. It should help you do what you're trying to achieve.
http://encosia.com/2007/04/17/disable-a-button-control-during-postback/
You don't necessarily want to show the button disabled on postback. You want to make sure they don't accidentally submit twice. So disabling or hiding the button as a result of a server-side action is already too late in the game. By this point the 2nd request is already on it's way. You need to either do it with javascript or make sure your server side code won't run twice.
In case of an updatepanel and a button inside a FormView-Template I use the following approach:
// Using that prm reference, hook _initializeRequest
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(InitializeRequestBuchung);
// Abfangen von Mehrfachklicks auf Buttons für asynchrone Postbacks im Updatepanel
function InitializeRequestBuchung(sender, args) {
var arrButtonIds = ["ButtonInsert", "ButtonUpdate"];
// Get a reference to the PageRequestManager.
var prm = Sys.WebForms.PageRequestManager.getInstance();
if (prm.get_isInAsyncPostBack() & jQuery.inArray(args.get_postBackElement().id, arrButtonIds) > -1) {
args.set_cancel(true);
}
}
This cancels the following postback if an async postback is currently still active. Works perfectly.
Someone else said this somewhere on here a few days ago, and I concur - use javascript to simply hide the button instead of disabling it; you could show a "spinner" image in its place, which lets the user know what is going on.
Instead of hiding, what I have done is swapping buttons using javascript. Show another greyed out image on the click of the first button.
Set the Button property UseSubmitBehavior to false. Then create an OnClientClick function that disables the button.
It would look something like this:
<script type="text/javascript">
function disableFunctn(button){
button.disabled = true;
}
</script>
<asp:Button ID="button1" UseSubmitBehavior="false" OnClientClick="disableFunctn(this);"/>
fastest cheapest way:
<asp:Button ID="button1" UseSubmitBehavior="false" OnClientClick="this.disabled=true;"/>
You can also try for example btnSave.Enable = false; when the button is hit and before the processing for the button is done in the Click Event routine. If you need it to be reset to allow it to be enabled have a separate button that resets the button for reuse.
Another method is to set the button with verification so that the user is asked if they want to Save, it should pop up both times.
Yet another method would be to flag the first occurrence then set a popup for the second to verify a second or subsequent usage.

Resources