1

I am facing very strange problem with below javascript. Compiler throw the error on line 39 of textbox code but it is very correct don’t know why this happen?.

 <script type="text/javascript">
    function WaterMark(txtName, event) {
        var defaultText = "Enter Username Here";
        // Condition to check textbox length and event type
        if (txtName.value.length == 0 & event.type == "Load") {
        //if condition true then setting text color and default text in textbox
        txtName.style.color = "Gray";
        txtName.value = defaultText;
    }
        // Condition to check textbox value and event type
        if (txtName.value == defaultText & event.type == "TextChanged") {
        txtName.style.color = "black";
        txtName.value = "";
    }
    }

  <table id="Search"><tr><td>
  <cc1:ToolkitScriptManager ID="TKit" runat="server"></cc1:ToolkitScriptManager>
  <asp:TextBox ID="Tbox" runat="server" Width="300"  OnLoad="WaterMark(this,event);" OnTextChanged ="WaterMark(this,event);" ></asp:TextBox>
            <cc1:AutoCompleteExtender 
                ID="Atx"  
                TargetControlID="Tbox" 
                runat="server" 
                UseContextKey="True"
                MinimumPrefixLength="1"
                EnableCaching="true" 
                CompletionSetCount="1"
                CompletionInterval="1000"
                ServiceMethod="location"
                CompletionListCssClass ="MM">
             </cc1:AutoCompleteExtender>
        </td></tr><tr><td>

How to overcome what is the solution for this?.!

Error Display

user2741987
  • 99
  • 11
  • i think you confuse textbox server side event [OnLoad](http://msdn.microsoft.com/en-us/library/system.web.ui.control.load(v=vs.110).aspx) and js client event onload – Grundy Jul 09 '14 at 12:49
  • @Grundy, I think you have to go through the Error Display It shows that I have mistake on textbox method call on OnLoad Event And Textchange Event where also suggest me to applye } on method call is it correct?. – user2741987 Jul 09 '14 at 12:54
  • i see it and i repeat: you confuse textbox server side event with client side event. – Grundy Jul 09 '14 at 13:01
  • @Grundy, I didn't get you could you explain in details please. If I correct from "Load" to "load" in js than is it work?. and TextChange event in js is it correct. – user2741987 Jul 09 '14 at 13:17

1 Answers1

0

asp:TextBox is a server side control. It have a few server side events.
When in aspx markup attribute name match with name this events then asp try find function for this event.
In your case, i think you want add client side event handler, but asp think that you mean server side.

first: client side event on text change for input - is onchange

second: as for load event - you can't add this for input with type=text, so you can use window.onload in which check needed textbox, or see this link

So in finish your markup will be like

<asp:TextBox ID="Tbox" runat="server" Width="300"  onchange ="WaterMark(this,event);" ></asp:TextBox>

and script,for example, like

<script type="text/javascript">
    function WaterMark(txtName, event) {
        var defaultText = "Enter Username Here";
        // Condition to check textbox length and event type
        if (txtName.value.length == 0 & event.type == "Load") {
            //if condition true then setting text color and default text in textbox
            txtName.style.color = "Gray";
            txtName.value = defaultText;
        }
        // Condition to check textbox value and event type
        if (txtName.value == defaultText & event.type == "change") {
            txtName.style.color = "black";
            txtName.value = "";
        }
    }

    window.onload = function () {
        var defaultText = "Enter Username Here";
        var txtName = document.getElementById('<%: Tbox.ClientID %>');
        // Condition to check textbox length and event type
        if (txtName.value.length == 0) {
            //if condition true then setting text color and default text in textbox
            txtName.style.color = "Gray";
            txtName.value = defaultText;
        }
    }
</script>

UPDATE I think, you need use onclick event instead of onchange for your function, i.e.

<asp:TextBox ID="Tbox" runat="server" Width="300"  onclick ="WaterMark(this,event);" ></asp:TextBox>

<script type="text/javascript">
    function WaterMark(txtName, event) {
        var defaultText = "Enter Username Here";
        // Condition to check textbox value and event type
        if (txtName.value == defaultText & event.type == "click") {
            txtName.style.color = "black";
            txtName.value = "";
        }
    }

    window.onload = function () {
        var defaultText = "Enter Username Here";
        var txtName = document.getElementById('<%: Tbox.ClientID %>');
        // Condition to check textbox length and event type
        if (txtName.value.length == 0) {
            //if condition true then setting text color and default text in textbox
            txtName.style.color = "Gray";
            txtName.value = defaultText;
        }
    }
</script>
Community
  • 1
  • 1
Grundy
  • 13,356
  • 3
  • 35
  • 55