admin管理员组

文章数量:1416651

I am facing a problem with buttons.

I want a client side validation to occur(say selecting a maximum of only 4 checkboxes in a datalist).

I used a INPUT TYPE="BUTTON" RUNAT="SERVER" style of tag. and put Onserverclick="someservermethod" which works fine.

I wrote a Validate() and added onclick="return validate();" to the above HTML markup and was thinking if it returns false then my server side code someservermethod() would not execute and it works great as expected

But when I return true I thought it would fire my someservermethod(), but it is not working. Any thoughts on why it is not working?

HTML MARKUP BELOW FOR THE BUTTON

button onclick="return ValidateSelection();" type="button" id="btnSubmit"
                runat="server" onserverclick="btnSubmit_Click"

JAVASCRIPT CODE

function ValidateSelection()
{
  if(good)
  {
    // I expect that my server side code btn_Submit would have got called. But
    // it doesn't get called.
    return true;
  }
  else
  {
    //This works great. Not calling the server side method for the button
    alert('something happened');
    return false;
  }  
}

I think I am messing up with the formatting. Pardon me.

I am facing a problem with buttons.

I want a client side validation to occur(say selecting a maximum of only 4 checkboxes in a datalist).

I used a INPUT TYPE="BUTTON" RUNAT="SERVER" style of tag. and put Onserverclick="someservermethod" which works fine.

I wrote a Validate() and added onclick="return validate();" to the above HTML markup and was thinking if it returns false then my server side code someservermethod() would not execute and it works great as expected

But when I return true I thought it would fire my someservermethod(), but it is not working. Any thoughts on why it is not working?

HTML MARKUP BELOW FOR THE BUTTON

button onclick="return ValidateSelection();" type="button" id="btnSubmit"
                runat="server" onserverclick="btnSubmit_Click"

JAVASCRIPT CODE

function ValidateSelection()
{
  if(good)
  {
    // I expect that my server side code btn_Submit would have got called. But
    // it doesn't get called.
    return true;
  }
  else
  {
    //This works great. Not calling the server side method for the button
    alert('something happened');
    return false;
  }  
}

I think I am messing up with the formatting. Pardon me.

Share Improve this question edited Aug 14, 2012 at 7:10 Sudhir Bastakoti 100k15 gold badges161 silver badges167 bronze badges asked Aug 14, 2012 at 6:55 SARAVANSARAVAN 15.1k16 gold badges53 silver badges70 bronze badges 6
  • Show us plete input tag along with javascript mathod for validation – Adil Commented Aug 14, 2012 at 6:57
  • So the actual problem is that your client side validate() function returns true but the server side code is not executed? If so, do you have any form submission at all? – user447356 Commented Aug 14, 2012 at 7:01
  • @ShadowWizard In my case the .aspx doesnot have any form submission tags. But earlier when I was not supposed to validate, I just had onserverclick. Now I need to do clientside validation and then after googling for sometime I added the Validate() and called it from the button's onclick. – SARAVAN Commented Aug 14, 2012 at 7:09
  • 1 Sounds like you are overwriting the click event in a wrong way. Try with such button instead: <asp:button OnClientClick="return ValidateSelection();" id="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit"></asp:Button> – user447356 Commented Aug 14, 2012 at 7:17
  • @ShadowWizard Yes. I see what u r saying. Actually this is the answer too for my questions. Thanks!!!! – SARAVAN Commented Aug 14, 2012 at 7:34
 |  Show 1 more ment

1 Answer 1

Reset to default 4

The control you are using right now it HtmlButton which is not "fit" for what you are trying to do, as your onclick overwrites the required client side code for the post back.

What you need is the Button control which does support overwriting the client side onclick and still perform the post back as usual.

Change the markup to this and it should work:

<asp:button OnClientClick="return ValidateSelection();" id="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit"></asp:Button>

本文标签: javascriptBUTTON ONCLICK ONSERVERCLICKStack Overflow