1

My problem is that i want to call a js function on a TextBox attribute "OnTextChanged", the js function should check if the text in TextBox is correct and set visibility of Button.

Here is my asp code:

...
<head runat="server">
...
<script type="text/javascript" src="<%# Page.ResolveClientUrl("Verify.js") %>"></script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:TextBox ID="FirstTextBox" ClientIDMode="Static" runat="server" ></asp:TextBox>
        <asp:Button ID="AddButton" runat="server" Text="Add person" Height="23px" OnClick="AddButton_Click"/>
...
</form>
</body>

js(in another file):

function Valid()
{
    var textBox = '<%= FirstTextBox.ClientID %>';
    var acceptButton = '<%= AddButton.ClientID %>';
    var text= document.getElementById(textBox).value;
    var accept = document.getElementById(acceptButton);
    if(data correct)
    {
        do things..
    }
    else
    {
        accept.style.display = 'none';
    }
}

and in c# code i set attribute for TextBox.

ClientScriptManager cs = Page.ClientScript;
cs.RegisterClientScriptInclude("Verify", "Verify.js");
this.FirstTextBox.Attributes.Add("OnTextChanged", "Valid()");

JS function isn't execute, it looks like the program even didn't go inside the js funcion. Anyone have an idea what i doing wrong? Thanks for help!

4
  • What do you mean "what am i doing wrong"? You haven't told us what is going wrong to begin with. Commented Apr 28, 2014 at 15:35
  • 2
    The js variables '<%= FirstTextBox.ClientID %>' and '<%= AddButton.ClientID %>' are not going to be evaluated by .net - that js file will be served literally. I would think you are getting console errors cannot read property 'value' of null? Commented Apr 28, 2014 at 15:38
  • 2
    I think you should use onChange js event instead server OnTextChanged Commented Apr 28, 2014 at 15:40
  • @DaveParsons i don't get any errors at the console. Commented Apr 28, 2014 at 15:46

2 Answers 2

3

I think there are two issues here:

  1. There isn't a javascript event OnTextChanged this should be updated to onchange or onkeypress depending on the responsiveness you are looking for.
  2. Your external javascript value contains code blocks which will not be evaluated, as powerful as .NET can be it's not going to randomly parse your resources for code blocks and attempt to make sense of them in the current context.

Given your current set up I think the easiest way is to update your code as follows:

JS:

function Valid(textboxId, btnId){
    var textBox = textboxId;
    var acceptButton = btnId;
    var text= document.getElementById(textBox).value;
    var accept = document.getElementById(acceptButton);
    if(data correct)
    {
        do things..
    }
    else
    {
        accept.style.display = 'none';
    }
}

C#:

ClientScriptManager cs = Page.ClientScript;
cs.RegisterClientScriptInclude("Verify", "Verify.js");
this.FirstTextBox.Attributes.Add("onchange", string.Format("Valid('{0}', '{1}')",FirstTextBox.ClientID, AddButton.ClientID));

Hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

1

You should use onchange instead of OnTextChanged which is Server Side event.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.