Submitting form with multiple onclick events

Discussion in 'ASP .Net' started by Samuel Hon, Mar 4, 2004.

  1. Samuel Hon

    Samuel Hon Guest

    Hi

    I'm building a custom control which basically takes data from the user
    and then submits it. I would use the validator controls but if I
    understand correctly (I've been working with .Net for 2 weeks) you
    cant 'embed' them in the custom controls. Someone correct me if I'm
    wrong (and please point to an example :) )

    So, to get around this, I'm using Javascript. In my control, I'm doing

    Writer.WriteAttribute("OnClick", "javascript:checkData();" &
    Page.GetPostBackEventReference(Me, "Data"))

    for an Input item so I get something like

    <input type="button" value="Submit"
    OnClick="javascript:checkData();__doPostBack('MyControl','Data')" />

    The checkData() function does this:

    function checkLogin()
    {
    if (document.Data.item1.value == "" )
    {
    alert("Please enter item 1.");
    return false;
    }

    if (document.Data.item2.value == "" )
    {
    alert("Please enter item 2.");
    return false;
    }

    if (document.Data.item1.value != "" && document.Data.item2.value !=
    "" )
    {
    return true;
    }

    }

    My query is, how do I get the click event to fire correctly so that i
    get alerts when there is a problem, and it passes through and submits
    the form if there isnt a problem?

    Thanks

    Sam
     
    Samuel Hon, Mar 4, 2004
    #1
    1. Advertisements

  2. Samuel Hon

    Natty Gur Guest

    Hi,

    Your client side script should use cancelBubble and returnValue to
    disable event default process and raise alert if error found.

    function CallServer()
    {
    if validate
    {
    __doPostBack('MyControl','Data');
    }
    else
    {
    alert("Error");
    window.event.cancelBubble = true;
    window.event.returnValue = false;
    }
    }


    HTH

    Natty Gur[MVP]

    blog : http://weblogs.asp.net/ngur
    Mobile: +972-(0)58-888377
     
    Natty Gur, Mar 4, 2004
    #2
    1. Advertisements

  3. Samuel Hon

    Samuel Hon Guest

    Thanks Natty

    This means I have to create inline javascripts which I'd rather not do
    if I can help it. Just as luck would have it, after spending an 'hour
    or two' trying to find a solution before posting, 5 mins after posting
    I find a solution :( Always the way...

    Anyhow, just for reference purposes, what I've done is to validate on
    OnMouseDown and postback on OnClick. Works a treat
     
    Samuel Hon, Mar 5, 2004
    #3
  4. Samuel Hon

    Peter Blum Guest

    Hi Samuel,

    Its unfortunate that you had to rework validation because of some incorrect
    information about validators. The ASP.NET validators work fine within custom
    controls. They have one key limitation related to their "location": the
    ControlToValidate must be associated with the ID of a control in the same
    "naming container". If your custom control impliments INamingContainer,
    ControlToValidate can only point to other controls within your custom
    control. If not, it can refer to other controls in the same naming container
    that your custom control appears. Some types of naming containers: Page,
    UserControl, and rows in DataGrid and DataList.

    If you wanted to support validation without naming container limits, I wrote
    a replacement to Microsoft's validators that overcomes that limitation among
    numerous others: Professional Validation And More.
    http://www.peterblum.com/vam/home.aspx.

    --- Peter Blum
    www.PeterBlum.com
    Email:
    Creator of "Professional Validation And More" at
    http://www.peterblum.com/vam/home.aspx
     
    Peter Blum, Mar 5, 2004
    #4
  5. Samuel Hon

    Samuel Hon Guest

    <snip>

    Thanks Peter for clearing that up, I will re-investigate

    Sam
     
    Samuel Hon, Mar 6, 2004
    #5
    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.