Modify the builtin onclick event of button?

Discussion in 'ASP .Net' started by Caspy, Oct 13, 2005.

  1. Caspy

    Caspy Guest

    When add a button to a web form, if the button can CauseValidation, the
    server emits client side javascript as:
    <input type="submit" name="Button1" value="Button"
    onclick="if (typeof(Page_ClientValidate) == 'function')
    Page_ClientValidate(); "
    language="javascript" id="Button1" />

    What I want to let the emitted javascript like :
    <input type="submit" name="Button1" value="Button"
    onclick="if (typeof(Page_ClientValidate) == 'function')
    Page_ClientValidate();
    if (Page_IsValid !=null && Page_IsValid == true)
    DoSomethingIWant();"
    language="javascript" id="Button1" />

    If I override the Render() method, the buildin jscript always emitted. How
    can I get the exactly I want above?

    Thanks in advance.
     
    Caspy, Oct 13, 2005
    #1
    1. Advertisements

  2. Caspy

    Raul Macias Guest

    You can add an onclick event handler to the button like this:

    Button1.Attributes.Add("onclick", "if (Page_IsValid !=null && Page_IsValid
    == true) DoSomethingIWant();");
     
    Raul Macias, Oct 13, 2005
    #2
    1. Advertisements

  3. Caspy

    Caspy Guest

    You approach generates something like:
    <input type="submit" name="Button1" value="Button" onclick="if(Page_IsValid
    == fasle) alert('Invalid!');if (typeof(Page_ClientValidate) == 'function')
    Page_ClientValidate(); " language="javascript" id="Button1" />

    The key point here is that (Page_IsValid == fasle) is checked before the
    page is validated, which the Page_IsValid always true. This is not waht I
    want.
     
    Caspy, Oct 14, 2005
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.