TextBox with EnterKeyPress Event - c#

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?

Related

Is there a way to let an asp.net page "catch up" to itself?

I'm working in C# and I need a button to become instantly disabled when a user clicks it. However, if I put as the very first line in the OnClick function
MyButton.Enabled = false;
it does nothing. The button remains enabled until it hits some sort of stop, whether it be the Catch block or the end of the function.
I was thinking that in VB/VBA you can use DoEvents() and that allows the code to catch up to itself. However, in C# there doesn't appear to be a DoEvents method.
This is a little different than the linked question, in that my OnClick code looks like:
OnClick="btnSubmit_Click"
and that user's OnClick code looks like:
onClick="this.disabled=true;
this.value='Sending…';
this.form.submit();"
When I tried to change my code to:
OnClick="this.disabled=true; btnSubmit_Click"
I got an error.
Compiler Error Message: CS1041: Identifier expected; 'this' is a
keyword
How can I do this in a C#/asp.net environment?
OnClick is a server-side event of the Button. So you cannot write:
OnClick="this.disabled=true; btnSubmit_Click"
OnClick accepts only the method-name of the server-side event handler.
If you want to handle the client-side button-click event to prevent that the user can click on it multiple times use OnCLientClick:
OnCLientClick = "this.disabled=true;"
You also have to set UseSubmitBehaviour to false.
Read: Disable a button control during postback.
You will need to utilize client-side JavaScript. One way is to utilize the OnClientClick property of your asp.net button control.
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick(v=vs.110).aspx
This is server-side and will be executed on PageLoad/Postback -
MyButton.Enabled = false;

Prevent button_click event in page refresh in 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

Disable 'submit' button without javascript

I'm trying to implement some sort of solution to the 'double submit' problem that occurs when there is a submit button that fires a code routine that takes some time to run.
Unfortunately, with a good number of users still using old internet browsers and some running no-script addons. I cannot use javascript.
Is there a way to disable a button after clicking a button in asp.net?
Since you want to disable the button after the user has clicked (and while your page is still loading) it has to be done on client side. JavaScript is the only client-side language you can use for this.
So make sure that if the user has Javascript disabled (highly unlikely these days), that your end-page can deal with "double submitted" data.
Short story, no.
In your situation, you would need to make some server side code to detect a double post, and then discard the duplicate request.
You can start timer on server after each button click and put it in session. On next click you can check timer and apply or decline next user click.
So it will be impossible to disable the button without some kinda of client side scripting. If you assume that it will be disabled, no matter what kinda of client side script it is, then that's an impossible task.
What you can do is ensure that the operation they're trying to perform won't ever be run more than once per page load.
When loading the page create a hidden field; populate that field with a GUID.
At the start of the server side click handler enter a Lock block.
Check for the existence of a session key based on that GUID. (Give it a prefix so it won't collide with any other GUID based session key.)
If the key existed, exit the lock block and do nothing, or display an error message of some sort.
If they key didn't exist, then add a new value to that session key, exit the lock block, and do your operation. Give the session key an appropriate expiration time; it shouldn't need to be more than a handful of minutes. You could even remove the relevant session key at the end of the operation, if it makes sense for it to be repeated at that point.
in button click event just write the code
{
//Some Code
button1.Enabled = false;
}

page/control lifecycle

I have usercontrol x in a page, within usercontol x is usercontrol z. I need to do something to usercontrol z after an event fires in usercontrol x. I understand this is very generic, but it just lays out a simple premise for my issue.
Is there a simple way to get a list of all events that fire in the page lifecycle whether I am subscribed to them or not to find out what events fire between Event A on the page and Event B on control z? I would think that there would be something like this when page tracing is turned on, but I don't see any events listed. I imagine that it would be something that the pipeline could generate, maybe I could use reflection to get a list?
I found this topic hard to search against in general, maybe I am using bad keywords...
I did find an old project on codeproject.com, but I was hoping that there would be something more elegant available by now.
*To clarify, I'm looking to generate a list after a postback of every potential event (subscribed or not), in firing order from init to unload, of all controls on a page.
THIS POST: Tracing all events in VB.NET
gets me a little bit closer, however these are lists on a per control basis, not in chronological order.
Enable trace with <%# Page Trace="true" %> in page attribute. that should display life cycle events in sequence.
Well if you're just looking to see what the events are in general, and in what order they fire, check out this article. As far as getting them...well, that depends on what you mean. It could be as simple as getting them through reflection:
EventInfo[] events = this.GetType().GetEvents();
The ASP.NET Page Life Cycle Overview on MSDN lists all of the events fired by a Page from PreInit to Unload. Most of these events fire on any class that inherits from System.Web.UI.Control as well.
Update
Now that I understand your intention better, one possible solution would be to loop through the events of every control and assign an event handler that logs the calling of the event. There is at least one way to retrieve all of the event handlers assigned to an event, and it shouldn't be too difficult to adapt that code to assign an event handler instead.

Raise button (or any control) click event manually. C#

Can anyone tell me how to raise click event of button control (or for that matter for any event).
Platform: .net 2.0/3.0/3.5
Language: c#
Domain: Windows Application, WinForms, etc.
You can use the Button.PerformClick method.
Maybe the solution is much more simple:
Maybe you don't really want your code "to click the button".
Do you just want to run the code which is behind the button from another place in the form?
If yes, put the code into a separate method (like "DoActionXXX") and call the method from the button and from everywhere else where you need it.
You can also look into Windows Accessibility or some UI automation framework that allows you to programmatically cause UI controls to respond to user gestures. If a control does not offer a way for its events to be programmatically triggered like PerformClick, you can derive from that control and expose a public method like PerformnXXX that when called internally invokes the event handlers subscribed to a particular event.
button click and to new my manually window
ex.. dital.cs open the may window
I dont think sending click events is the best way from a design point of you just make your event handler call another function. That way you always have access to that function.
so ..
void myEventHandler(ObjectwhoSentTheMessage O, Event e)
{
MyOtherFunction();
//consume the event after if you wish etc
}

Categories