WebForms TextBox Lost Focus Event - c#

I'm trying to add an event handler to a TextBox control that is called when the text box loses focus. Is this possible?
I know WinForms has a LostFocus event. I'm looking for something similar in ASP.NET WebForms.

You can Use OnBlur
<asp:TextBox id="TextBox1" runat="server" onblur="Javascript:alert('1234');" />
Or
EDIT:
Server side
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Attributes.Add("onblur", "alert('hi User')");
}
}

I think you are going to have to use javascript for this and the event you will need to search and look up would be onBlur() I believe. If you need an example, I might be able to scrounge one up for you. But here are some links..
W3Schools
Stack
Java Page
Hope these help.

This would be the blur event, and it's a client-side event on the resulting input element instead of a server-side event on the TextBox control.
For example, let's say you have a TextBox with the ID of "TextBox1", and let's assume jQuery is included in your project template (because it probably is), then in your client-side code you might have something like this:
$('#<%= TextBox1.ClientID %>').blur(function () {
// code to respond to the event
});
This would execute any code in that function whenever the input for that TextBox control loses focus. Notice the use of a small block of server-side code to output the server-side control's client-side ID.

Related

ASP.NET textbox textchange with same text input

My ASP.NET page has a text box (say TextBox1). I used TextChanged event of the TextBox1 to fill some other text boxes on the same page. Here is the problem:
When the user has entered some text for the first time and lose the focus on the TextBox1, TextChanged event get fired and everything works as it should be.
But if the user has entered the same input as the first time, TextChanged event doesn't get fired. (because no text changes between the postbacks)
I want to fire a server side event whether user input the same text or different text. So the rest of the page get updated in both cases.
Any method or workaround for this ?
Code behind language is C#.
Edited: codes added.
ASP page
<asp:TextBox ID="txtClientNumber" AutoPostBack="true"
runat="server" Width="100px" onFocus="select()"
OnTextChanged="txtClientNumber_TextChanged" ></asp:TextBox>
C#
protected void txtClientNumber_TextChanged(object sender, EventArgs e)
{
//txtName.Text = ...<get data from SQL Server >..
//other codes
}
Thanks.
The question is rather broad, so the answer is a bit generic. For this purpose, use ASP.NET TextBox1 OnTextChanged event and set the property AutoPostback="True".
Also, you can handle this event in client-side scripting by adding the attribute to TextBox1 control: TextBlock1.Attributes.Add("onblur","SomeJavascriptFunction()");
Hope this may help.

Enter key firing Gridview Row Command Event

I have a textbox and a gridview right below that on an aspx page. When ever I get to the page and press enter, it fires gridview onrowcommand event. Can anyone tell me what is going on here? I did google everyone suggested me to add a piece of javascript code to handle enter key event (13) . I want to know why would a page do that. Is it a web form thing or something else?
What you're witnessing is the default behavior much like hitting enter on an HTML input tag nested within form tag.
If you don't want the behavior, then you can handle it either in JavaScript as your research suggested it or in your C# code as follows:
protected void gvChild_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes.Add("onkeypress", "javascript:if (event.keyCode == 13) {
// do whatever you want with the event
}
}

Is it possible to write an event handler function for a basic html button element which will be run in asp.net server instead of a javascript block?

Practically I don't need it. But a little curiosity is running on my mind as my event handler function written for running in the server is not called. I have dragged a html button element and put it inside the form element with attribute runat="server"
Now if I double click on the button inside design view, it redirects me to a javascript block on which the event handler function is created. I erased it and write my own in the code behind file. Now when run the page on the server and click on the button, the event handler function is not called.
I want it to be run. Any way ?
You are confusing between two different events: the onclick provided in the editor is the client side click event, handled with javascript code.
The server side, c# based event handler can be added to such a button if you markup it with
runat="server"
,attribute and add a server side handler to it (assuming Button1 is the id of your button)
Button1.ServerClick += new EventHandler(Button1_ServerClick);
the markup for the html input doesnt expose the server side event to add a handler through the markup, but you can add the code i pasted above within your page_load/init events
What is your button like? try this:
<button runat="server" ID="myButton" onClick="YourFunction">My Button</button>
ASPX:
<button runat="server" id="htmlButton1" name="htmlButt" onserverclick="butt">
foo</button>
CS/Code-behind:
protected void butt(object sender, EventArgs e)
{
Response.Write("butt clicked!");
}
You'll have to write the handler (manually) - the IDE will not auto-magically wire it for you (you wire it yourself with onserverclick).

Adding an onmouseover attribute to an <asp:Panel> in a <asp:Repeater>

I have a repeater containing several several panels.
I have noticed that there is no onmouseover attribute of the <asp:Panel>.
I have read that I can add the attribute to the panel by calling in the page_load:
PanelID.Attributes.Add("onmouseover", "Script to Run");
The problem is that I am in a repeater, and the PanelID is generated by asp.net like ContentPlaceHolder_ctl01_myID_0 so not only it is hard to figure out, but VS2010 does not recognise it as a proper object and throws an error on it, plus I have to attach it on every item, so I need to use a for or foreach, but I don't know what to iterate on.
Is there a way like
foreach(childcontrol in Repeater.ChildControlsISpecificallyNeedPossiblyIdentifiedBySomeID)
{
childcontrol.Attributes.Add("onmouseover", "Script to Run");
}
to do this in C# in the Page_Load eventhandler?
I want to run a client side onload, onmouseclick and onmouseout on the panels too, so I want to attach those attributes too.
Thinking outside the box, instead of specifying the onmouseover-attribute inline using ASP.NET, you can attach the javascript event listeners in javascript.
Give the panel a CSS class:
<asp:Panel CssClass="panel"></asp:Panel>
Using jQuery, the syntax to bind the event handlers would be as follows:
$(".panel").mouseover(functionToRun);
If you're not using jQuery, you'll need a bit more code, but I'm sure you get the idea.
panel is represented with div on client side, you can get the client side id by using SomePanel.ClientID however if you are referring to the triggering panel you can always use this in the JavaScript
would not put all the event handlers on each element
Add the attribute to the panel in the event of repeater onitemdatabound and find the control in that, as this event will called for each and every time the row is bound so that you dont have to call it foreach.
You may consider using ItemDataBound event.
protected void myRepeater_ItemDataBound(object source, RepeaterCommandEventArgs e) {
if(e.Item.ItemType == ListItemType.Item){
Panel pnl = (Panel)e.Item.FindControl("myPanel");
pnl.Attributes.Add"onMouseOver", "yourFnctionName()");
}
}

Determine when Postback occurs because of a textbox being changed

I've got a usercontrol that has a <asp:TextBox> with autopostback set to true. When the user types text in and moves off the textbox, asp.net fires a postback event. However, it isn't handled specifically by the textbox, but is instead simply a postback - the page_load fires and then life goes on.
What I need is a way to know that this textbox fired the event. I then want to fire my own custom event so that my main page can subscribe to it and handle the event when the textbox changes.
At first I thought I could capture the sender - I was assuming that the sender would be the textbox, but it appears to be the page itself.
Anyone have any thoughts?
I think you're missing something, so I want to explain it step by step :
your textbox should be something like that :
<asp:TextBox runat="server" ID="TextBox1" AutoPostBack="true"
OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
and in your codebehind you shhould have an event like that :
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
string str = TextBox1.Text;
}
NOTE : if you want to attach your event in code-behind you can do this by :
TextBox1.TextChanged +=new EventHandler(TextBox1_TextChanged);
but if you talking about custom controls, you should implement IPostBackDataHandler interface to raise events.
Is this i dynamically created usercontrol or textbox? If so you have to recreate that control in the page_load event and the event should be fired on the textbox as normal.
The page load sender will always be ASP.default_aspx because the page is what is calling that event.
You can hook up to the even OnTextChanged where that sender would be the textbox that has changed or caused the post back.
Keep in mind the page load will always fire before any of the events on the controls.
This is potentially something that could be overlooked, but do you actually have an event coded for the textbox's TextChanged event? If you have it set to autopostback but never actually created an event for it, you're only going to get your page load and other related events.
As alluded to in other answers you need to handle the 'OnTextChanged' event.
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
private void TextBox1_TextChanged(object sender, System.EventArgs e)
{
TextBox txt = (TextBox)sender;
string id = txt.ID;
switch(id)
{
case "TextBox1":
break;
}
}

Categories

Resources