0

How can I register AND execute that JavaScript function in ASP.NET on a Page_Load event so my textbox content gets validated and if nothing is in there I can disable a save button?

function Validate(source, arguments)
{
}
11
  • 1
    Wouldn't a required field validator do this for you? Why use 'raw' javascript? Commented Jul 19, 2011 at 11:40
  • Do you mean that you want to execute a javascript, get a result and according it disable button and do all that on a Page_Load? Commented Jul 19, 2011 at 11:41
  • Why are you trying to do it with JavaScript? You're saying you want to perform this check in the Page_Load and disable the submit button, so the value of the TextBox is set by the code behind and not the user (otherwise you would check it with a RequiredFieldValidator or a CustomValidator), so why don't you perform the check directly in the code behind? Commented Jul 19, 2011 at 11:43
  • @Five The RequiredFieldValidator does only validate when something is entered and removed again. It should trigger on the page_load event! Commented Jul 19, 2011 at 11:45
  • If so - you can reference both your text box and save button in the page load event. If the text box text = string.empty disable your button. You could use the registerstartupscript below - but you're making extra work IMO. Commented Jul 19, 2011 at 11:50

1 Answer 1

1

Use ClientScriptManager.RegisterStartupScript

See here for example

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
  public void Page_Load(Object sender, EventArgs e)
  {
    // Define the name and type of the client scripts on the page.
    String csname1 = "PopupScript";
    Type cstype = this.GetType();

    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Check to see if the startup script is already registered.
    if (!cs.IsStartupScriptRegistered(cstype, csname1))
    {
        StringBuilder cstext1 = new StringBuilder();
        cstext1.Append("<script type=text/javascript> alert('Hello World!') </");
        cstext1.Append("script>");

        cs.RegisterStartupScript(cstype, csname1, cstext1.ToString());
    }
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>RegisterStartupScript</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
</body>
</html>

Hope this helps

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

3 Comments

How is it possible to debug the javascript if its build dynamically?
You can create not dynamically function, and call that function dynamically, it's just a sample @msfanboy
its not executed at the end of the page because the html control I search for is null.

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.