0

i have a vb.net form button that go through a jquery validation before excude a function in the code behind , actually in either ways if the validation is true or false the function stopped and never call the vb.net function how can i make the it proceed to the code behind ?

vb.net form (textbox and button )

      <asp:TextBox ID="ToCc" runat="server"></asp:TextBox>
    <asp:Button ID="emailSend" runat="server" Text="send" />

jquery :

   $("#<%=emailSend.ClientID %>").click(function(){
          if($("#<%=ToCc.ClientID %>").val() == ""){
            alert("this fienld can't be empty") ; 
            return false ; 
          } else{
    return true ; 
}

         });

the email click vb function is

 Protected Sub emailSend_Click(ByVal sender As Object, ByVal e As EventArgs) Handles 

    emailSend.Click

   MsgBox("do any thing ") 

   End Sub
1
  • you can declare the method shared and decorate it with [WebMethod] attribute and then call that method using jquery ajax to send email asynchronously if you want to use jquery Commented May 25, 2012 at 17:15

3 Answers 3

1

You haven't assigned your emailSend_Click method as the event handler for the button. Secondly, you can use the RequiredFieldValidator to accomplish your validation goals.

Try <asp:Button ID="emailSend" runat="server" Text="send" onclick="emailSend_Click" />

Additionally, remove the statement emailSend.Click from your code behind. You are already in the click event handler.

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

1 Comment

@MinaGabriel Why the insistence on using jquery? If you use a RequiredFieldValidator then it will automatically do the check server-side too, which you must do anyway - never trust the data sent back from the client.
0

this is jquery version

$('input[id$=emailSend]').click(function(){
if($('input[id$=ToCc]').val() == ''){
            alert("this fienld can't be empty") ; 
            return false ; 
          } 
});

Comments

0

More concise and a little more "jQuery"'d:

$("#<%=emailSend.ClientID %>").click(function(event) { 
      if($("#<%=ToCc.ClientID %>").val() == "") { 
        alert("this field can't be empty") ;  
        event.preventDefault(); 
      }  
}); 

By default it'll return true if everything is OK.

4 Comments

$("#<%=emailSend.ClientID %>") will look for an element with <%=emailSend.ClientID %> as ID look at SLAKS comment
Erm, no it'll not, the <%= emailSend.ClientID %> will write the full client ID to the script when the page loads as it's ASP.NET - I've done this many times without any problems. Using a attribute search just puts more load on the browser to search the DOM.
You can't use it in a separate js file anyway
That is true, and a good point to bear in mind should you site be laid out this way.

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.