1

i am trying to use javascript events in asp.net webforms. but events for input controls like textfield, such as onClick, onFocus,onBlur, dont appear. do i need to change my directive:

<%@ Page Title="" Language="C#" MasterPageFile="~/YourGuruMaster.master" AutoEventWireup="true" CodeFile="AskQuestion.aspx.cs" Inherits="AskQuestion" %> 

i want to be able to do this:

//code page protected void Page_Load(object sender, EventArgs e) { QuestionTextBox1.Attributes["onfocus"] = "ClearSearchText()"; //Markup page function ClearSearchText() { var searchUserName = document.getElementById('<%=QuestionTextBox1.ClientID%>'); if (searchUserName.value = searchUserName.defaultValue) { searchUserName.value = ""; } return false; } <p dir="rtl" style=""> <asp:TextBox ID="QuestionTextBox1" runat="server" Width="702px" Text="פרטים עד 5000 תווים"></asp:TextBox> 

4
  • So, what is the question? Yes, I think you really need AutoEventWireup.CommentedMay 7, 2011 at 11:44
  • Can you provide a sample of the actual HTML elements and associated events that aren't firing?CommentedMay 7, 2011 at 11:58
  • You certainly don't need to change the directive.CommentedMay 7, 2011 at 12:15
  • "AutoEventWireup" is for server side events, not JS. And not for onSomeEvent="method" code, but for Object_Event methods to work automatically, and they usually apply to things like page and user controls not text boxes and stuff, like Page_Load.
    – Meligy
    CommentedMay 7, 2011 at 13:00

2 Answers 2

2

Add onfocus and onblur into the markup as follows:

<asp:TextBox ID="TextBox1" runat="server" onfocus="TextBox1_focus(this, event)" onblur="TextBox1_blur(this, event)" Text="Search..."></asp:TextBox> <script type="text/javascript"> var searchText = 'Search...'; function TextBox1_focus(sender, e) { if (sender.value == searchText) sender.value = ''; } function TextBox1_blur(sender, e) { if (sender.value == '') sender.value = searchText; } </script> 
    1

    Well, not sure which ASP.NET version you use. I think last versions allow this (rendering attributes that the server controls don't understand to the browser still). Try using "onfocus" instead (lower case).

    However, if this is not working for you, then you have to do it from code behind...

    protected void Page_Load(object sender, EventArgs e) { QuestionTextBox1. Attributes["onfocus"]="someJavaScriptMethod"; } 

    Alternatively, if you have jQuery in the page you can go something like ...

    <script type="text/javascript"> $(function() { $('#<%= QuestionTextBox1.ClientID %>').focus(someJavaScriptMethod); }); </script> 

    If you do that, inside someJavaScriptMethod(), you can use the word this to point at the focused control, and you can create a jQuery object from it easily like $(this).

    .

    Please leave me a comment if none of the above solves your problem.

    2
    • In the edited question do you mean it doesn't take effect and method not called or the attribute is not rendered at all? I mean, if you check the source of the page in browser, do you see the 'onfocus' in the HTML of the text-box or not?
      – Meligy
      CommentedMay 7, 2011 at 14:29
    • i gave up trying that effect. i will keep things simpler..thanks for the helpCommentedMay 7, 2011 at 16:35

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.