views:

35

answers:

2

Greetings, In my current project, I have gridview, search button, text box for search, text box, and submit button.

-I should input string in the search box then click search button. -when click search button, it will retrieve all matches records then bind them to the view grid.

-then when I click a record in the gridview, it should bound a field to the second text box.

  • finally I should submit the page by clicking in submit button. where is the problem:

-the problme that I'm using jquery validation plugin that will make second text box is required. -when I click search button will not allow postback until I write some thing in second text box.

How can I make scond text box only do validation for required field only when click asp.net submit button.

here is my code:

<%@ Page Title="" Language="C#" MasterPageFile="~/Master.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="IMAM_APPLICATION.WebForm1" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

  <script src="js/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>

    <script src="js/jquery.validate.js" type="text/javascript"></script>
    <script src="js/js.js" type="text/javascript"></script>

     <script type="text/javascript">
         $(document).ready(function() {



     $("#aspnetForm").validate({


            // debug: true,


             rules: {

             "<%=txtFirstName.UniqueID %>": {

                 required: true


             }

                 },
                   errorElement: "mydiv",
             wrapper: "mydiv",  // a wrapper around the error message

             errorPlacement: function(error, element) {
                 offset = element.offset();
                 error.insertBefore(element)
                 error.addClass('message');  // add a class to the wrapper
                 error.css('position', 'absolute');
                 error.css('left', offset.left + element.outerWidth());
                 error.css('top', offset.top - (element.height() / 2));

             }



         });
     })



</script>

     <div id="mydiv">
    <asp:GridView ID="GridView1" runat="server" 

                        style="position:absolute; top: 280px; left: 30px; height: 240px; width: 915px;" 
                        PageSize="5" 
                    onselectedindexchanged="GridView1_SelectedIndexChanged" 
                        AutoGenerateColumns="False" DataKeyNames="idcontact_info">
                    <Columns>
                        <asp:CommandField ShowSelectButton="True" InsertVisible="False" 
                            ShowCancelButton="False" />
                        <asp:BoundField DataField="First_Name" HeaderText="First Name" />
                        <asp:BoundField AccessibleHeaderText="Midle Name" DataField="Midle_Name" />
                        <asp:BoundField DataField="Last_Name" HeaderText="Last Name" />
                        <asp:BoundField DataField="Phone_home" HeaderText="Phone Home" />
                        <asp:BoundField DataField="cell_home" HeaderText="Mobile Home" />
                        <asp:BoundField DataField="phone_work" HeaderText="Phone Work" />
                        <asp:BoundField DataField="cell_Work" HeaderText="Mobile Work" />
                        <asp:BoundField DataField="Email_Home" HeaderText="Personal Home" />
                        <asp:BoundField DataField="Email_work" HeaderText="Work Email" />
                    </Columns>
                </asp:GridView>
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
        DeleteMethod="Delete" InsertMethod="Insert" 
        OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" 
        TypeName="IMAM_APPLICATION.DSContactTableAdapters.contact_infoTableAdapter" 
        UpdateMethod="Update">
        <DeleteParameters>
            <asp:Parameter Name="Original_idcontact_info" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="Title" Type="String" />
            <asp:Parameter Name="First_Name" Type="String" />
            <asp:Parameter Name="Midle_Name" Type="String" />
            <asp:Parameter Name="Last_Name" Type="String" />
            <asp:Parameter Name="Address1_Home" Type="String" />
            <asp:Parameter Name="Address2_Home" Type="String" />
            <asp:Parameter Name="City_Home" Type="String" />
            <asp:Parameter Name="State_Home" Type="String" />
            <asp:Parameter Name="Prov_Home" Type="String" />
            <asp:Parameter Name="ZipCode_Home" Type="String" />
            <asp:Parameter Name="Country_Home" Type="String" />
            <asp:Parameter Name="Phone_home" Type="String" />
            <asp:Parameter Name="Phone_Home_Ext" Type="String" />
            <asp:Parameter Name="Cell_home" Type="String" />
            <asp:Parameter Name="Fax_home" Type="String" />
            <asp:Parameter Name="Email_Home" Type="String" />
            <asp:Parameter Name="material_status" Type="String" />
            <asp:Parameter Name="DateOfBrith" Type="String" />
            <asp:Parameter Name="company" Type="String" />
            <asp:Parameter Name="Work_Field" Type="String" />
            <asp:Parameter Name="Occupation" Type="String" />
            <asp:Parameter Name="sub_Occupation" Type="String" />
            <asp:Parameter Name="Other" Type="String" />
            <asp:Parameter Name="Address1_work" Type="String" />
            <asp:Parameter Name="Address2_work" Type="String" />
            <asp:Parameter Name="City_Work" Type="String" />
            <asp:Parameter Name="State_Work" Type="String" />
            <asp:Parameter Name="Prov_Work" Type="String" />
            <asp:Parameter Name="ZipCode_Work" Type="String" />
            <asp:Parameter Name="Country_Work" Type="String" />
            <asp:Parameter Name="Phone_Work" Type="String" />
            <asp:Parameter Name="Phone_Work_Ext" Type="String" />
            <asp:Parameter Name="Cell_Work" Type="String" />
            <asp:Parameter Name="Fax_Work" Type="String" />
            <asp:Parameter Name="Email_work" Type="String" />
            <asp:Parameter Name="WebSite" Type="String" />
            <asp:Parameter Name="Note" Type="String" />
            <asp:Parameter Name="Groups" Type="String" />
            <asp:Parameter Name="InterPhoneHome" Type="Int32" />
            <asp:Parameter Name="InterMobileHome" Type="Int32" />
            <asp:Parameter Name="InterFaxHome" Type="Int32" />
            <asp:Parameter Name="InterPhoneWork" Type="Int32" />
            <asp:Parameter Name="InterMobileWork" Type="Int32" />
            <asp:Parameter Name="InterFaxWork" Type="Int32" />
            <asp:Parameter Name="rdoPhoneHome" Type="Int32" />
            <asp:Parameter Name="rdoMobileHome" Type="Int32" />
            <asp:Parameter Name="rdoEmailHome" Type="Int32" />
            <asp:Parameter Name="rdoPhoneWork" Type="Int32" />
            <asp:Parameter Name="rdoMobileWork" Type="Int32" />
            <asp:Parameter Name="rdoEmailWork" Type="Int32" />
            <asp:Parameter Name="locationHome" Type="Int32" />
            <asp:Parameter Name="locationWork" Type="Int32" />
            <asp:Parameter Name="Original_idcontact_info" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="Title" Type="String" />
            <asp:Parameter Name="First_Name" Type="String" />
            <asp:Parameter Name="Midle_Name" Type="String" />
            <asp:Parameter Name="Last_Name" Type="String" />
            <asp:Parameter Name="Address1_Home" Type="String" />
            <asp:Parameter Name="Address2_Home" Type="String" />
            <asp:Parameter Name="City_Home" Type="String" />
            <asp:Parameter Name="State_Home" Type="String" />
            <asp:Parameter Name="Prov_Home" Type="String" />
            <asp:Parameter Name="ZipCode_Home" Type="String" />
            <asp:Parameter Name="Country_Home" Type="String" />
            <asp:Parameter Name="Phone_home" Type="String" />
            <asp:Parameter Name="Phone_Home_Ext" Type="String" />
            <asp:Parameter Name="Cell_home" Type="String" />
            <asp:Parameter Name="Fax_home" Type="String" />
            <asp:Parameter Name="Email_Home" Type="String" />
            <asp:Parameter Name="material_status" Type="String" />
            <asp:Parameter Name="DateOfBrith" Type="String" />
            <asp:Parameter Name="company" Type="String" />
            <asp:Parameter Name="Work_Field" Type="String" />
            <asp:Parameter Name="Occupation" Type="String" />
            <asp:Parameter Name="sub_Occupation" Type="String" />
            <asp:Parameter Name="Other" Type="String" />
            <asp:Parameter Name="Address1_work" Type="String" />
            <asp:Parameter Name="Address2_work" Type="String" />
            <asp:Parameter Name="City_Work" Type="String" />
            <asp:Parameter Name="State_Work" Type="String" />
            <asp:Parameter Name="Prov_Work" Type="String" />
            <asp:Parameter Name="ZipCode_Work" Type="String" />
            <asp:Parameter Name="Country_Work" Type="String" />
            <asp:Parameter Name="Phone_Work" Type="String" />
            <asp:Parameter Name="Phone_Work_Ext" Type="String" />
            <asp:Parameter Name="Cell_Work" Type="String" />
            <asp:Parameter Name="Fax_Work" Type="String" />
            <asp:Parameter Name="Email_work" Type="String" />
            <asp:Parameter Name="WebSite" Type="String" />
            <asp:Parameter Name="Note" Type="String" />
            <asp:Parameter Name="Groups" Type="String" />
            <asp:Parameter Name="InterPhoneHome" Type="Int32" />
            <asp:Parameter Name="InterMobileHome" Type="Int32" />
            <asp:Parameter Name="InterFaxHome" Type="Int32" />
            <asp:Parameter Name="InterPhoneWork" Type="Int32" />
            <asp:Parameter Name="InterMobileWork" Type="Int32" />
            <asp:Parameter Name="InterFaxWork" Type="Int32" />
            <asp:Parameter Name="rdoPhoneHome" Type="Int32" />
            <asp:Parameter Name="rdoMobileHome" Type="Int32" />
            <asp:Parameter Name="rdoEmailHome" Type="Int32" />
            <asp:Parameter Name="rdoPhoneWork" Type="Int32" />
            <asp:Parameter Name="rdoMobileWork" Type="Int32" />
            <asp:Parameter Name="rdoEmailWork" Type="Int32" />
            <asp:Parameter Name="locationHome" Type="Int32" />
            <asp:Parameter Name="locationWork" Type="Int32" />
        </InsertParameters>
    </asp:ObjectDataSource>

                    <asp:TextBox ID="txtSearch" runat="server" 
                    style="position:absolute; top: 560px; left: 170px;" ></asp:TextBox>

                    <asp:Button ID="btnSearch" runat="server" Text="Search" 
                    style="position:absolute; top: 555px; left: 375px;" 
                    CausesValidation="False" onclick="btnSearch_Click"/>

                    <asp:Label ID="Label7" runat="server" Style="position: absolute; top: 630px; left: 85px;"
                    Text="First Name"></asp:Label>

                 <asp:TextBox ID="txtFirstName" runat="server" Style="top: 630px; left: 185px; position: absolute;
                    height: 22px; width: 128px"></asp:TextBox>   
 <asp:Button ID="submit" runat="server" Text="submit" />



</div>    
</asp:Content>
+1  A: 

The way I did this was to have the 2 buttons not as submit buttons, but regular buttons. When the one that you want to perform validation is clicked, call:

$("#aspnetForm").submit();

This causes validation then (if passed) submit.

For the one you don't want to validate, you can call:

$("#aspnetForm").get(0).submit();

This gets the underlying DOM element and calls its submit - this won't trigger the 'onsubmit' event created by .validate

Luke Schafer
+2  A: 

You can disable the submit blocking by default by passing the onsubmit option, like this:

 $("#aspnetForm").validate({
    onsubmit: false,
    rules: {
         "<%=txtFirstName.UniqueID %>": {
             required: true
         }

    },
    errorElement: "mydiv",
    wrapper: "mydiv",
    errorPlacement: function(error, element) {
       offset = element.offset();
       error.insertBefore(element)
       error.addClass('message');  // add a class to the wrapper
       error.css('position', 'absolute');
       error.css('left', offset.left + element.outerWidth());
       error.css('top', offset.top - (element.height() / 2));

    }
 });

Then call the .valid() method on only that button, like this:

$("#<%=submit.ClientID%>").click(function() {
  return $("#aspnetForm").valid();
});

When you click the submit button specifically, you're invoking validation, normally the validation plugin rigs up to the <form>'s submit event, but setting onsubmit:false stops it from doing this. That leaves you to invoke validation whenever you want manually...which is what that .valid() call is doing, only on the button you want it on.

Nick Craver