Problem refresh in asp.net - c#

I create Button. Add event Click. in event function AddToDataBase.
I press Button, event work, run function - data good add to database.
more I press F5 event wirk and function AddToDataBase start working.
It is not correct. how to fix it?

Try doing a redirect after you've done the "data good add to database", otherwise F5 will submit your event again.

By default, when a page is refreshed, the PostBack event is going to be registered again, which will fire off your button click event again.
A simple solution to this would be to add the following command after your AddToDatabase function completes:
Response.Redirect(Request.Url.PathAndQuery)
This will cause the page to redirect to itself, so that if a refresh occurs, the PostBack event will not register the button click.
It is not the most elegant, but it will get the job done. If you have more complex things going on with the page, you may need to look into other solutions, such as wrapping the AddToDatabase function through AJAX or something else.

F5 queries the server with same GET and POST parameters that were used last time to display the page. So if your button doesn't redirect you to another page, doing F5 will send a request to the server as if you've clicked that button again.

Related

C# ASP.net How to add a reload of the page/panel at the end of a button event?

I have a page that consists of an updatepanel with a DropDownList, a textbox and a button in it. The updatepanel has a trigger for the button. This works fine for all my "error" cases, such as "No input!", when I pop an alert with
ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(),"noName", "window.onload = function(){alert('Please enter a username!');window.location ='Accounts.aspx';}", true); and the like.
But when I don't hit the alerts and the button event runs all it should I seem to land in some dead zone. My alerts stop displaying even though the code runs (checked by debugging), the button event does still preform all tasks as it should. However, if I refresh the page I get the classic:
To display webpage again. After I click retry my "latest" message pops up and then it works just like on first load.
So, I'm guessing that the PostBackTrigger on my button triggers a PostBack before or during the event execution since when the event is done nothing more happens until I reload. I'm thinking I need to add a reload at the end of my button event. Therefore my question is: How do I add a reload of the page/panel at the end of a button event?
Edit:
Forgot to mention that when the button event runs all it should it edits the web.config file, if this somehow could cause my issue.
I found a solution to my problem, by removing my triggers and instead using:
ScriptManager1.RegisterPostBackControl(Button);
Then I could also put an:
UpdatePanel1.Update();
At the end of my button event.

Calling postback in asp.net programmatically

I am using spgridview with spgridviewpager in update panel. I need to postback using c# in aspx page. I used button field in the spgridview. When i clicked on the button field, i need to raise the postback event using c# or javascript.
Can any have an idea how to do it programmatically?
Since there are some quite good articles on the web explaining this, let me answer your question with a link:
How to call Postback from Javascript
I think you can use triggers after the content template of update panel.
One more simple suggestion:
If you just want to cause a postback and to do nothing after postback, simply double click the button in the design view, you will get an onbuttonclick event generated in the code behind page. Don't write anything inside that button click event block. The page will get posted onclicking the button. If you want to implement something, write the code inside the button click event.
go to the button property u will find postback url ..
if not then use rowCommand to get the button event rise

ASP.NET - How to track event from previous postback?

I have a requirement to check for a certain condition on postback before redirecting (Response.Redirect) to another page.
Note... I cannot use JavaScript to detect whether or not to confirm (this is also a requirement) :s
Pseudo:
protected void lbtnRedirect_OnClick(object sender, EventArgs e)
{
if (showConfirm)
{
// Set flag for client side
this.ShowConfirm = true;
// Track this event for next postback.
}
else
{
Response.Redirect("somepage.aspx");
}
}
If the showConfrim flag == true, then the client will be show a modal dialog box asking them if they are sure they want to redirect. If the user clicks on "Yes", then the page posts back and the desired effect is that the lbtnRedirect_OnClick event is fired. How would I about tracking the lbtnRedirect event?
Edit:
I have no problem tracking the flag to show the modal (yes JS must be used to show the modal... somethings you just cannot get rid of :)). I should have been more clear.
It is when the user clicks "Yes" to continue the redirect. The page will postback again but needs to know which event to go through.
i.e. Suppose there are 3 onclick events, 1) lbtnRedirect1_Onclick 2) lbtnRedirect2_OnClick 3) lbtnRedirect3_OnClick... each of which does the confirm check.
Each onclick event does the check. So when the user clicks on "Yes" on the modal, how does the page know which event to drop back into?
You can use ViewState if you're in WebForms.
Implement a ShowConfirm property encapsulating ViewState["ShowConfirm"].
In the first postback you'll set ShowConfirm 'true', and this will activate that modal during the render (if ShowConfirm is true, that's setting as visible 'true' some control).
In the next postback, you'll set ShowConfirm 'false' because is 'true', and finally you'll do the whole redirect!
You can use an ajax call from javascript to set the required values.
Since the postback will happen before even the execution reaches to your button click event we need a workaround here, And if you don't need JS as your requirement, so take a look at
Implementing Client Callbacks Programmatically without Postbacks in ASP.NET
This is much like a wrapper for XMLHttp Ajax call IMHO.
You cannot easily create a model form, without javascipt.
One suggestion I would make is to have panels in your page.
Panel one is visible.
On submit one; panel one hides and panel two is visible asking for a confirmation.
On panel two is a confirm button, clicking this button your redirection is performed.

Moving button stops click event happening

I have a button, contained in a panel, with a click event, that works fine. However when a users presses another button, I need to move this button into another panel (this is actually a panel with a modalpopupextender), so I this code to do so:
newPanel.Controls.Add(buttonPanel)
It all get's moved and looks fine. However now when the button is clicked it doesn't fire the associated event. I have tried re-adding the event in the page_init, with this code
((Button)this.FindControl("serverModalSave")).Command += new CommandEventHandler(modalSave_Click);
But with no luck. How can I get this button to fire it's click event when moved, and why does it stop working when it's moved?
EDIT:
This Button needs to be added to a panel specified by the user at run time, so there is not a way to determine where the button will go in advance.
I could instead of moving this button, create a new one, but because this button is not created in the page_init I am having issues getting that to fire an event either.
Instead of moving the button, have another button on the other panel set to hidden.
Hide the button you wanted to move and show the hidden one when needed.
Moving the control changes the naming hierarchy and now the button can't be found and the click event can't fire.
This is due to how the page life cycle works. Here is a good (if somewhat dated) article about how view state works - if you understand this, you will understand what went wrong.
If you are creating the button in the new panel, when this button is then clicked do you re-create it in the postback ?
You must re-create all controls on each postback see here

Button click is again fired on reload

When page is reloaded it again goes to last event(e.g. Button click).Is there any way to prevent this?
If you are reloading the page by 'refreshing' (F5), and clicking to accept the resubmission of values, the button click event firing again is by design.
If it's something else, we need much more information to help.
Try to add a Response.Redirect to the current page in the end of your event handler.

Categories