【问题标题】:How to use a C# bool method return value(serverside) in a jquery function(clientside)如何在 jquery 函数(客户端)中使用 C# bool 方法返回值(服务器端)
【发布时间】:2016-07-07 00:31:39
【问题描述】:

这是我的问题,我想从我后面代码中的 bool 验证方法获取返回值,并在 jquery if 语句中使用它......类似这样 - 通过将 bool 方法“validation”插入jquery if 语句

  < script >
    $(function() {
      $('form').bind('submit', function() {

        if (Page_IsValid && ***validation()==true***) {
          $('#tableWrapper').fadeTo(3000, 0.0).queue(function(next) {
            $("#tableWrapper").hide();
            next();
          });

        }


      });
    });

  < /script>

这是我的服务器端 bool 方法 -

 protected bool validate()
{
    if (calExpdate.SelectedDate < DateTime.Now)
    {
        lblExpDateError.Visible = true;
        lblExpDateError2.Visible = true;
        lblExpDateError.ForeColor = System.Drawing.Color.Red;
        lblExpDateError2.ForeColor = System.Drawing.Color.Red;

        lblExpDateError.Text = "\u2022 Expiry Date must be greater then todays date";
        lblExpDateError2.Text = "*";
        return false;
    }

    if (ddlCType.SelectedIndex == 0)

        if (txtCNumber.Text.Length < 15 || txtCNumber.Text.Length > 15)
        {

            lblCardNumError2.Visible = true;
            lblCardNumError.Visible = true;
            lblCardNumError2.ForeColor = System.Drawing.Color.Red;
            lblCardNumError.ForeColor = System.Drawing.Color.Red;
            lblCardNumError2.Text = "*";
            lblCardNumError.Text = "\u2022 Card Number: Please enter 15 digits";
            return false;
        }

    if (ddlCType.SelectedIndex == 1 || ddlCType.SelectedIndex == 2)
    {
        if (txtCNumber.Text.Length < 16 || txtCNumber.Text.Length > 16)
        {

            lblCardNumError.Visible = true;
            lblCardNumError2.Visible = true;
            lblCardNumError2.ForeColor = System.Drawing.Color.Red;
            lblCardNumError.ForeColor = System.Drawing.Color.Red;
            lblCardNumError2.Text = "*";
            lblCardNumError.Text = "\u2022 Card Number: Please enter 16 digits";
            return false;
        }

    }
    return true;
}

这是我的 aspx -

<%@ Page Title="Purchase Form" Language="C#" MasterPageFile="~/Master Pages/FrontEnd.master" AutoEventWireup="true" CodeFile="Purchase.aspx.cs" Inherits="_Purchase" %>

  <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
  </asp:Content>
  <asp:Content ID="Content2" ContentPlaceHolderID="mainCont" runat="Server">
    <div id="fadediv">
      <asp:UpdatePanel runat="server">
        <ContentTemplate>

          <h1>Purchase Information</h1>
          <br />
          <div id="tableWrapper">
            <div runat="server" id="tableWrapper1">
              <table class="auto-style3">
                <tr>
                  <td colspan="3">
                    <h2>Personal and Shipping Information
                                    <br />
                                        Please fill in the form below</h2>
                  </td>
                </tr>
                <tr>
                  <td class="auto-style7">Name</td>
                  <td class="auto-style6">
                    <asp:TextBox ID="txtFName" runat="server" MaxLength="50"></asp:TextBox>
                  </td>
                  <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Text="*" ErrorMessage="Please Enter a First Name" CssClass="ErrorMessage" ControlToValidate="txtFName"></asp:RequiredFieldValidator>
                  </td>
                </tr>


                <tr>
                  <td class="auto-style7">Phone</td>
                  <td class="auto-style6">
                    <asp:TextBox ID="txtPhone" runat="server" TextMode="Number" MaxLength="10"></asp:TextBox>
                  </td>
                  <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Please Enter a Phone Number" CssClass="ErrorMessage" ControlToValidate="txtPhone">*</asp:RequiredFieldValidator>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ErrorMessage="Please enter a phone number between 8 and 10 digits. Area code required" ValidationExpression="^\(?(?:\+?61|0)(?:(?:2\)?[ -]?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])|3\)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])|7\)?[ -]?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)|8\)?[ -]?(?:5[ -]?[1-4]|6[ -]?[0-8]|[7-9][ -]?[0-9]))(?:[ -]?[0-9]){6}|4\)?[ -]?(?:(?:[01][ -]?[0-9]|2[ -]?[0-57-9]|3[ -]?[1-9]|4[ -]?[7-9]|5[ -]?[018])[ -]?[0-9]|3[ -]?0[ -]?[0-5])(?:[ -]?[0-9]){5})$"
                    ControlToValidate="txtPhone" CssClass="ErrorMessage">*</asp:RegularExpressionValidator>
                  </td>
                </tr>



                <tr>
                  <td class="auto-style7">Card Type</td>
                  <td class="auto-style6">
                    <asp:DropDownList ID="ddlCType" runat="server" CssClass="ddl">
                      <asp:ListItem Selected="True">AMEX</asp:ListItem>
                      <asp:ListItem>VISA</asp:ListItem>
                      <asp:ListItem>Master Card</asp:ListItem>
                    </asp:DropDownList>
                  </td>
                  <td>&nbsp;</td>
                </tr>
                <tr>
                  <td class="auto-style7">Card Number</td>
                  <td class="auto-style6">
                    <asp:TextBox ID="txtCNumber" runat="server" TextMode="Number"></asp:TextBox>
                  </td>
                  <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ErrorMessage="Please Enter a Card Number" ControlToValidate="txtCNumber" CssClass="ErrorMessage">*</asp:RequiredFieldValidator>
                    <asp:Label ID="lblCardNumError2" runat="server" CssClass="labelcssRed" Visible="False">*</asp:Label>
                  </td>
                </tr>
                <tr>
                  <td class="auto-style7">Expiry Date</td>
                  <td class="auto-style6">
                    <asp:Calendar ID="calExpdate" runat="server" SelectedDate="02/01/2016 22:15:30" VisibleDate="2016-02-01"></asp:Calendar>
                  </td>
                  <td>
                    <asp:Label ID="lblExpDateError2" runat="server" CssClass="labelcssRed" Visible="False">*</asp:Label>
                  </td>
                </tr>
                <tr>
                  <td class="auto-style7">Email</td>
                  <td class="auto-style6">
                    <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
                  </td>
                  <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ErrorMessage="Please Enter an Email" ControlToValidate="txtEmail" CssClass="ErrorMessage">*</asp:RequiredFieldValidator>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txtEmail" CssClass="ErrorMessage" ErrorMessage="Please enter a valid email address" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
                  </td>
                </tr>
                <tr>
                  <td class="auto-style7">&nbsp;</td>
                  <td class="auto-style6">
                    <asp:ValidationSummary ID="ValidationSummary1" runat="server" CssClass="ErrorMessage" />
                  </td>
                  <td>&nbsp;</td>
                </tr>
                <tr>
                  <td class="auto-style7">&nbsp;</td>
                  <td class="auto-style6">

                    <asp:Label ID="lblCardNumError" runat="server" Visible="False"></asp:Label>
                    <br />

                    <br />
                    <asp:Label ID="lblExpDateError" runat="server" Visible="False"></asp:Label>

                  </td>
                  <td>&nbsp;</td>
                </tr>
                <tr>
                  <td class="auto-style7">&nbsp;</td>
                  <td class="auto-style6">


                    <asp:Button ID="btnSubmit" runat="server" OnClick="Button1_Click" Text="Submit" />&nbsp;
                    <asp:Button ID="btnCancel" runat="server" OnClick="btnCancel_Click" Text="Cancel" CausesValidation="False" />


                  </td>
                  <td>&nbsp;</td>
                </tr>
              </table>
            </div>
          </div>
          <script>
            $(function() {
              $('form').bind('submit', function() {

                if (Page_IsValid) {
                  $('#tableWrapper').fadeTo(3000, 0.0).queue(function(next) {
                    $("#tableWrapper").hide();
                    next();
                  });

                }


              });
            });
          </script>
        </ContentTemplate>
      </asp:UpdatePanel>
    </div>

    <asp:Label ID="message" runat="server" Text="" Style="position: absolute; left: 600px"></asp:Label>
  </asp:Content>

【问题讨论】:

    标签: c# jquery methods boolean


    【解决方案1】:

    enter code here找到了答案....在使用 jquery 之前尽可能多地进行客户端验证,最困难的部分是验证日历控件,但最后我只是在日历控件上设置了开始日期这是今天的日期,并将日历中的值插入文本框,然后验证文本框。

    【讨论】:

      猜你喜欢
      • 2015-11-22
      • 2011-08-30
      • 1970-01-01
      • 1970-01-01
      • 2016-05-16
      • 2015-12-07
      • 2012-06-22
      • 2020-08-27
      • 1970-01-01
      相关资源
      最近更新 更多