Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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>

share|improve this question
So, what is the question? Yes, I think you really need AutoEventWireup. – ErickPetru May 7 '11 at 11:44
Can you provide a sample of the actual HTML elements and associated events that aren't firing? – Sir Crispalot May 7 '11 at 11:58
You certainly don't need to change the directive. – Jakub May 7 '11 at 12:15
i updated my question – Dmitry Makovetskiyd May 7 '11 at 12:29
"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 May 7 '11 at 13:00

3 Answers

up vote 0 down vote accepted

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.

share|improve this answer
i updated my answer – Dmitry Makovetskiyd May 7 '11 at 13:35
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 May 7 '11 at 14:29
i gave up trying that effect. i will keep things simpler..thanks for the help – Dmitry Makovetskiyd May 7 '11 at 16:35

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>
share|improve this answer

Check if your browser don`t block javascript code.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.