Prevent button_click event in page refresh in c# - c#

I want to prevent button_click event in page refresh.
I have searched but can not get any solution relevant for me.
I have found this one:
http://www.dotnetspider.com/resources/42823-How-Avoid-Re-Submitting-Data-Page-Refresh.aspx
But i do not need to go inside button_click event in page refresh.
Response.Redict("") also not relevant here as i am returning label text after succesful button_click.

What you looking for is not so simple.
The solution you can use for sure is the Post/Redirect/Get design pattern
or use some code that tries to detect the refresh of the page on:
http://www.codeproject.com/KB/aspnet/DetectingRefresh_.aspx
http://dotnetslackers.com/community/blogs/simoneb/archive/2007/01/06/Using-an-HttpModule-to-detect-page-refresh.aspx

Related

C# - are there any events fired right after loading a form?

I want to give the user the option to use a tutorial, the first time he uses the program. I tried adding it in the Form.Load event, but the forms shows up after the Messageboxes have popped up.
That's why I would like to know, are there any events fired right after loading a form?
If not, is there a way to perform actions right after loading?
You should try the shown event, which fires after the form is shown for the first time.
Load happens before the form is shown.
You could try using the Shown event but that might be a bit early too based on what you are doing but it does occur after the Load.
If you have any controls on the page you could trigger it off the controls GotFocus event. Just make sure to put in checks to only do it once if using the GotFocus method.
MSDN Form.Shown
MSDN Control.GotFocus
MSDN Reference to order of events
System.Windows.Forms.Control.HandleCreated
System.Windows.Forms.Control.BindingContextChanged
System.Windows.Forms.Form.Load
System.Windows.Forms.Control.VisibleChanged
System.Windows.Forms.Form.Activated
System.Windows.Forms.Form.Shown
The Shown event should do this for you.

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

TextBox with EnterKeyPress Event

I have a TextBox. Basically it is a search box in which user is entering keyword and then the search appears.
I have a button now for this, but I want to do on EnterPress Event. How can I achieve this?
Thanks!
Since this is ASP.NET, the textbox should fire off a postback when the Enter button is pressed automatically. That's just what happens with forms in a browser.
Edit
You don't need to do anything special to trigger a postback. The way I read your question, you want to know how to perform the search on the code-behind when the postback is triggered by an Enter keypress in the textbox. If this is incorrect, let me know and I'll delete this answer.
End Edit
The server-side code does not support a "TextboxEnterPressed" event. The closest you can get is the TextChanged event.
If you haven't already done so, move any code necessary for performing the search outside of the Button_Clicked event handler into it's own function. Then change the Button_Clicked event handler and the Text_Changed event handler to call the same function.
OR you can write something in JavaScript to handle the Enter keypress, but as I said - in a web page, pressing enter in a textbox within a form triggers a postback anyway, so you should not need to bother
Exception - if this is a TextBox with the TextMode set to "Multiline", which actually produces Textarea in the html output. If that's the case, you will need a JavaScript solution.
Put both into Panel and set DefaultButton.
Try this:
$('#input_text').keyup(function(e) {
if(e.keyCode == 13) {
alert('Enter key was pressed.');// do your stuff here
}
});
If it were me, I would go for JavaScript, there might be a way to do this in .NET itself, but this will submit the whole form, in the usual .NET way.
There are two major JavaScript frameworks, my personal preference is MooTools, however it's the harder of the two to learn, but it's benefit is that it's got a nice OOP design to it, so if you know OOP and you want to learn to do a lot more with Js then this in my opinion is the better. The alternative is JQuery, a good framework, easier to learn (though could be negligible depending on your experience and knowledge).
MooTools
JQuery
Once you have the JavaScript setup you then need to decide how you want to get the data, the simplest method would be just to use a _PostBack call which would call a standard .Net postback method, I've also heard that there is a _ICallBack method, which can be used to call a .NET method without a postback, but I personally haven't used this yet.
But my preference would be to use a WebService and a javascript call which would allow you a lot more flexibility and return quick results, either on enter or even 'as you type' search, like google - straight to the page, asynchronously, without page reload.
If you give me more data on exactly what kind of feedback you want to give the user I can advise better.
Is it a simple replacement for pressing a button, or something more advanced?

Generating event on .cs page by double click on the design view of .aspx page

I am developing a web application in asp.net and c#, now in a particular aspx page whenever I doubleClick(design view) on a button or on a drop down list, instead of going to
public void btn_click event or DropDown_SelectedIndexChanged event, the cursor points to protected void Page_Load only. Strange!! any remedy?
Not a fix for your VS misbehaving, if it is doing so. But this will help you wire up your events and perhaps notice anomalous event assignments:
Select the control in question, right-click>Properties, switch to the 'events' tab (lightning bolt) and either enter the name of a method or simply double click the empty space to generate an event handler in your codebehind.
This is also where you will see if Page_Load is already, for whatever reason, assigned to the event you are having trouble with.
HTH
May be both your btn_click event and DropDown_SelectedIndexChanged event delegates have Page_Load as the method. Check your events tab for button and dropdown.
this happens to me when I have my project running in debug mode and forget to stop it before editing my code. Long shot, but sometimes the obvious things are the things we overlook :)

Async. Postbacks cause Page_Init? (C#)

I'm experiencing a very strange problem...
I have a regular ASP.Net webpage with a page_init and a page_load function. It is my understanding(from everywhere I look) that page_init gets called on the first page load(as in, not ever called in a postback) and page_load is called anytime something happens with the page.(It is very hard finding any info about this except for dead links and stuff about the page life cycle)
Well, I have an update panel containing other update panels and other assorted controls. Anytime I edit one of these controls, an async postback happens but instead of only page_load being called, page_init is also called which isn't suppose to happen(and didn't happen before a big codebase change)
So I would like to know anything that might cause this behavior or just if my idea of how page events are called is wrong.
I think you have the wrong idea of the page load life cycle. The OnInit event is called on EVERY request. Having the Page_Init method in your code behind is a shorthand way of wiring up the pages OnInit event.
Now I believe that you are confusing this with the "IsPostBack" property which will be set to true if a page posts back to itself i.e. when you click a Button etc. My guess is what you need to do is add an if statement in your Page_Init method i.e.
if(!IsPostBack){
//Do something to to update the UI
}
Page_Init is definitely called on every page hit, postback or not, exactly the same as Page_Load.
The misconception that Page_Init doesn't get called on every request seems to be a common one.
Are you certain that this wasn't happening before your "big codebase change"?
My suggestion is that you create a simple example of what you're trying to do on a brand new scratch page based on these rules..
http://msdn.microsoft.com/en-us/library/ms178472.aspx
Keep adding complexity as you have it now until it breaks..
It would be difficult to diagnose your issue without the code..
Your Page_Init as well as Page_Load both methods should be called each time.

Categories