views:

315

answers:

5

Which type of event would i place on a textbox to cause an action on the web form when the cursor leaves that textbox ?and how can i implement this?

I actually want to display a message on the form after details have been entered in the last textbox to notify users if they have left any field blank. I hope to apply this on the last textbox on the form.

I know an event handler shoud be able to instantiate this but am not sure which event would do this and how to implement it....

all advices are warmly welcome.. Thank you..

+2  A: 

I think you are probably going to want to look at the ASP.NET validation controls. They should be able to handle what you are wanting to do.

http://devhood.com/Tutorials/tutorial%5Fdetails.aspx?tutorial%5Fid=46

http://msdn.microsoft.com/en-us/library/aa479045.aspx

Kevin
+1 for providing links to teach ..(Give a man a fish..etc teach how to fish ..etc.. )
Madi D.
+2  A: 

You'll need to use javascript on the client to handle this. What you want to do is add a handler for the blur event. The blur event occurs when an element loses focus. Use this in conjunction with your client-side validation logic to trigger validation when the field loses focus.

I prefer adding my javascript unobtrusively. Below is an example of how you would do it using jQuery and the jQuery validation plugin. Using it with standard ASP.NET validators would work as well, just replace the call to the validation logic with that for your client-side validators, i.e., call Page_ClientValidate().

<script type="text/javscript">
    $('form').validate();  // set up validation
    $('#lastTextBoxID').blur( function() {
       $('form').valid(); // validate when the blur event happens
    });
</script>
tvanfosson
i had given u +1 for ur effort in solving the problembut where are editor when i need them ???? I don't got votes Up for my answer and this occurrs each time so i think that i will not strain myself to answer questions....
Ahmy
I don't think you understood his question. You answer seems to be about how to **change focus**. He wants to do some validation when an element **loses focus**.
tvanfosson
A: 

You can put the textbox inside a div and add ur function in the event of onmouseover() like this :

<div onmouseover="ChangeFocus()">
<asp:TextBox runat="server" ID="TxtBox1"></asp:TextBox>
</div>
<script type="text/javascript" language="javascript">
function ChangeFocus()
{
   var Details = document.getElementById('<%=TxtBox1.ClientID %').text; 
   //Display the details of the textbox in the place u need

}
</script>

And take care to adjust the width of the div and the textbox to fit it.

or u can replace asp textbox with input field of type "text" and set it's event onmouseover to the function ChangeFocus().

Hope that this will be usefull

Ahmy
Thanks Ahmy..that was very helpful
Selase
Where is +1 Vote ? D: looool
Ahmy
A: 

You could do something like this:

<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
        <asp:TextBox ID="txtName" runat="server" />
        <asp:RequiredFieldValidator 
            ID="RequiredFieldValidator1" 
            runat="server" 
            ErrorMessage="The name field is required." 
            ControlToValidate="txtName" Display="None">
         </asp:RequiredFieldValidator>
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
gwalker
A: 

Could the TextChanged event be a likely solution? and when would this event occur?

I tried that but it didnt work... It appears that the event fires after the page has been validated (on button click).

any work arounds?

Selase
No this event can't be a solution because this event occurrs if the text inside the text box changed not in case of focus changed please try my solution and it will help u and textchanged occurrs after changing the text already exist in the textbox
Ahmy