Javascript – How to enable javascript in asp.net webforms

asp.netc++javascript

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>

Best Solution

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>