How to control page post from javascript? - asp.net

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.

Related

Activate-Deactivate submit button in a form

I made a registration form using backbone.js, CoffeeScript and jquery.
I am trying to disable the submit button after 1 click (so that it doesn't fire events again and again) , i also want that button get active again when i fill fields of my form.. Thanks for your time.
In your code that runs the event (hopefully in the events delegate routine of your view) all you have to do is tell JQuery to disable the button
$("#btnSubmit").attr("disabled", true);
Then when you need to re-enable it
$("#btnSubmit").removeAttr("disabled");
Just call the appropriate calls when required.
seems there is no enable/disable property. maybe you could use the “silent” option to keep the “change” event from triggering?
or just create a validation routine and call it from the event i.e.
(pseudo code)
BUTTON EVENT FIRED
IF IsValid() then
'do something
else
'do something else
end if
func IsValid() as boolean
' check to see if the form is ready to submit i.e. required fields are valid
Thanks alot #Bryan and #max ..
I have done it as :
enable_button: ->
$("#my_button").attr("disabled", false).removeClass('disabled')
I am also changing the color of button on enable/disable.

Please help me understand AutoPostBack property of an ASP.NET control

I'm learning about ASP.NET, mainly by following through a book, but also making an effort to actually do things along the way. But, I came across an explanation about list controls which I don't understand. This is what it says:
"[in the context of the Smart Tasks panel]...the last option sets the AutoPostBack property of the control. With this option checked, the control will submit the page it's contained in back to the server as soon as the user chooses a new item from the list"
Can you explain this statement for me? Thanks in advance for your help.
For normal client controls (such as a list control with AutoPostBack set to false), when a user chooses an item in the list, the browser does not communicate with the server. There's no network traffic and no delay for your user before they see the results of the choice, but there's also no opportunity to do anything in your server code, like calculate dependent values. If you want to do anything to the screen in response to the choice, you have to use a client-side script.
When AutoPostBack is set to true, selecting an item in the list sends a message to the server (via an HTTP POST). ASP.NET then executes whatever code you have attached to the list's changed event, rebuilds the page, and sends the revised page to the client.
If you set AutoPostBack="true" on a control, when it's value changes, it will automatically postback to the server.
For example if you wanted a dropdown that when changed displayed different data in a table below or something, you might want to postback get the new value so your page could refresh the data.
This is opposed to style of the dropdown and a button beside it you click to postback, so instead of change value, click the button, you can just change the value with AutoPostBack="true".
A ListBox has a SelectedIndexChanged event that you can handle to detect when the selected item in a ListBox has changed. You'd configure it like this:
<asp:ListBox ID="ListBox1" runat="server" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"/>
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// Do something
}
With AutoPostBack="false" (the default), that event handler doesn't actually happen on the server in "real time". The user has to perform an unrelated action to submit (POST) the form, such as clicking a button, to make that event fire on the server.
If you want to take "real time" action on that event, you set AutoPostBack="true" which makes the form automatically submit every time the selected item is changed.
The benefit - you get "real time" notification of events. The drawback - the page talks a lot more to the server, so each click costs bandwidth and causes client "lag".
Further reading: http://www.dotnetspider.com/resources/189-AutoPostBack-What-How-works.aspx
When a user selects a ListItem (or whatever the collection item is), the page should automatically submit the web form to the server with a POST event.
here's the wikipedia page on HTTP POST events
http://en.wikipedia.org/wiki/HTTP_POST
The responsibility of an asp.net control contained in a Page is to render a part of the html that the user will end up seeing in his browser. Some controls support the AutoPostBack property. What it does is that it makes the control emit some extra javascript that will submit the form whenever the value of that control is changed, so that you can react to this on the server side.
Basically AutoPostBack is used so that whenever there is some change in the controls text or anyother change in the controls property, the page is submitted to the server.
Posting the page means, the page is submitted to the server. Suppose i use a textbox and i make its AutoPostBack = "true", now i write some text into it and click outside the textbox, then the page will refresh.
This refresh indicates that your value which you entered into the textbox has been submitted to the server.
The postback is handled by ASP.NET server. AutoPostBack will automatically post back your page to the server.
Add an event Handler. This will give you a better picture.
In your case of DropDownList: Add an eventhandler: double click the DropDownList, it will route you to an eventhandler:
Write something in that event handler let us say : Response.Write("message");
The page will refresh and you will see your message, this means the page was posted to the server and the server has executed your event handler and displayed you the message.
I hope this was usefull

Confirm message in 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?') "

ASP.Net Cross Page Posting

Currently I have two pages:
The first page contains an input form, and the 2nd page generates an excel document. The input form's button posts to this 2nd page.
What I'd like to do is add a second button which also posts to the 2nd page; however, I'll need requests created from this new button to act differently, which brings me to my question:
Is there a way I can tell, from the 2nd page, which button was pressed to submit the request?
The main reason I'm asking is I'd like to re-use the 2nd page's logic in parsing the information from the first page if possible; I'd rather not have to copy it to a new page and have the new button post to that.
Thanks!
You could have a hidden text box on the form from the first page that each button sets a value in before posting to the second page. The second page could then evaluate the value of that hidden text box.
Edit: After re-reading your post, I think I misunderstood what you were attempting to accomplish. If you're simply trying to determine which button on the sending page was clicked, it could be done with the querystring:
Page1.aspx:
<asp:Button ID="Button1" Text="Button 1" PostBackUrl="~/Page2.aspx?button=1" runat="server" />
<asp:Button ID="Button2" Text="Button 2" PostBackUrl="~/Page2.aspx?button=2" runat="server" />
Page2.aspx.cs:
string sButton = "0";
if (Request.QueryString["button"] != null)
sButton = Request.QueryString["button"];
if (sButton == "1")
// Do button 1 stuff
else if (sButton == "2")
// Do button 2 stuff
If this is just pure HTML and your form action attribute points to the 2nd page...
You simply need to have two different submit buttons with the same name attrib and two different value attribs. When either button is clicked, it'll submit the form fields to the second page. Checking the value of the posted element with the submit button's name will tell you which one was clicked.
If we're talking ASP.NET (as your title and tag suggests) you can handle the server-side click event of each button in a postback to determine which one was clicked. But submitting an ASP.NET form to a second page isn't necessarily simple. See the Scott Guthrie post on URL Rewriting, specifically the section near the end about overriding the form tag's action attrib.
After doing some debugging, it looks like my best bet is to determine whether or not the button is included in the form values.
I.e.
if(Request.Form["Button1ClientID"] != null)
//button 1 stuff
else if(Request.Form["Button2ClientID"] != null)
//button 2 stuff
If anyone knows of a better way to do this, please let me know
Edit: UniqueID, not ClientID
Actually, checking for the button in Request.Form only works if you are using Button controls. I needed this in a project where I am using LinkButtons, which do not place anything into Request.Form. However, I was able to determine which button was pressed with the following code:
if (Request.Form["__EVENTTARGET"] == button1.UniqueID) {
// do button 1 stuff
} else if (Request.Form["__EVENTTARGET"] == button2.UniqueID) {
// do button 2 stuff
}
Anyways, I find these forums so helpful, I thought I should post an answer for once.

Asp.Net AlertBox code behind page?

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);

Resources