Button click is again fired on reload - c#

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.

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

Problem refresh in asp.net

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.

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

DataList ItemCreated event

I have a datalist in my aspx page. In the datalist, there are images displayed. When I click on an image, a bigger version of the image appears in a popup, and in this popup there is a button. On that button, I want to react to a click even without postback. What I'm doing now is not working every time. I am using go for the item_created event and __dopostback(btn.id,"onClick").
The item_created event fires when I click on the ok button on the div that displays the image.
If you mean that ItemCreated event is firing every time post back is occurring. Please do your databinding only first time when page loads. You can use IsPostBack property to check if it is a postback or fresh loading of page.
Page_Load(....){
if(!IsPostBack){
LoadData();
}
}
If you do not want to use postback on button click, please use ajax and page methods. You can get more information here: Using jQuery AJAX to directly call page methods.

Categories