0

问:当我将 btnSubmitCheckup_Click 属性设为空时,所有 RequiredFieldValidators 都在工作。但是如果我在里面放一个简单的代码,验证器就不再工作了,它会执行我放在按钮的 Click 属性中的简单代码。

如果必填字段为空白,如何阻止程序继续执行?我已经设置了 ControlToValidate,当 btnSubmitCheckup_Click 中没有代码时它可以完美地工作,但是如果我尝试在其中放入代码,它将继续前进并忽略 RequiredFieldValidator。

protected void btnSubmitCheckup_Click(object sender, EventArgs e)
        {
            Response.Write("asdasda"); ////will work even all other textboxes are empty but validators won't appear
            //_con.Open();
            //SqlCommand NewCheckup = new SqlCommand("dbo.NewCheckup", _con);
            //NewCheckup.CommandType = CommandType.StoredProcedure;
            //NewCheckup.Parameters.Add("@PatientID", SqlDbType.Int).Value = RadioButtonList_Patient.SelectedValue;
            //NewCheckup.Parameters.Add("@DeptCode", SqlDbType.VarChar).Value = RadioButtonList_Department.SelectedValue;
            //NewCheckup.Parameters.Add("@EmpID", SqlDbType.Int).Value = RadioButtonList_Employee.SelectedValue;
            //NewCheckup.Parameters.Add("@CheckupDate", SqlDbType.Date).Value = BasicDatePicker1.SelectedDate;
            //NewCheckup.Parameters.Add("@Diagnosis", SqlDbType.VarChar).Value = txtDiagnosis.Text;
            //NewCheckup.ExecuteNonQuery();
            //_con.Close();
            //Response.Write("<script>Alert('Checkup Successfully recorded!')</script>");
        }

这是我的来源

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="NewCheckup.aspx.cs" Inherits="Clinic_WebDev.NewCheckup" %>

<%@ Register assembly="BasicFrame.WebControls.BasicDatePicker" namespace="BasicFrame.WebControls" tagprefix="BDP" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Bargamed Clinic - New Checkup</title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 136px;
        }
        .style3
        {
            width: 250px;
        }
        #TextArea1
        {
            height: 104px;
            width: 272px;
        }
    </style>
</head>
<body>
    <table class="auto-style1">
            <tr>
                <td>
                    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/InsertProcedure.aspx">Insert Procedure</asp:HyperLink>
                </td>
                <td>
                    <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/InsertPatient.aspx">Insert Patient</asp:HyperLink>
                </td>
                <td>
                    <asp:HyperLink ID="HyperLink3" runat="server" NavigateUrl="~/InsertEmployee.aspx">Insert Employee</asp:HyperLink>
                </td>
                <td>
                    <asp:HyperLink ID="HyperLink4" runat="server" NavigateUrl="~/InsertDepartment.aspx">Insert Department</asp:HyperLink>
                </td>
                <td>
                    <asp:HyperLink ID="HyperLink5" runat="server" NavigateUrl="~/NewCheckup.aspx">New Checkup</asp:HyperLink>
                </td>
            </tr>
    </table>
    <form id="form1" runat="server">
    <table class="style1">
        <tr>
            <td align="right" bgcolor="#66FF66" class="style2">
                Name of Patient</td>
            <td bgcolor="#CCFFCC" class="style3">
                <asp:TextBox ID="txtPatientName" runat="server" AutoPostBack="True" 
                    TextMode="Search" Width="170px"></asp:TextBox>
            </td>
            <td bgcolor="#66FF66" style="margin-left: 80px">
                Press Enter to search a patient
                <br />
                <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" 
                    ControlToValidate="txtPatientName" ErrorMessage="Please search for a patient" 
                    Font-Italic="True" Font-Names="Segoe UI" ForeColor="#666633" 
                    EnableClientScript="False"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td align="right" bgcolor="#66FF66" class="style2">
                Patient</td>
            <td bgcolor="#CCFFCC" class="style3">
                <asp:RadioButtonList ID="RadioButtonList_Patient" runat="server" 
                    AutoPostBack="True" DataSourceID="SqlDataSource3" DataTextField="PatientName" 
                    DataValueField="PatientID">
                </asp:RadioButtonList>
                <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:CLINICConnectionString %>" 
                    SelectCommand="SELECT PatientID, FirstName + ' ' + MiddleName + ' ' + LastName AS PatientName FROM PATIENT WHERE (FirstName + ' ' + MiddleName + ' ' + LastName LIKE '%' + @PatientName + '%')">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="txtPatientName" Name="PatientName" 
                            PropertyName="Text" />
                    </SelectParameters>
                </asp:SqlDataSource>
            </td>
            <td bgcolor="#66FF66">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" 
                    ControlToValidate="RadioButtonList_Patient" 
                    ErrorMessage="Please select the patient" Font-Italic="True" Font-Names="Segoe UI" 
                    ForeColor="#666633"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td align="right" bgcolor="#66FF66" class="style2">
                Department
            </td>
            <td bgcolor="#CCFFCC" class="style3">
                <asp:RadioButtonList ID="RadioButtonList_Department" runat="server" 
                    AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="DeptName" 
                    DataValueField="DeptCode" 
                    onselectedindexchanged="RadioButtonList_Department_SelectedIndexChanged">
                </asp:RadioButtonList>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:CLINICConnectionString %>" 
                    SelectCommand="SELECT DeptCode, DeptName, Status FROM DEPARTMENT">
                </asp:SqlDataSource>
            </td>
            <td bgcolor="#66FF66">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                    ControlToValidate="RadioButtonList_Department" 
                    ErrorMessage="Please select the department" Font-Italic="True" 
                    Font-Names="Segoe UI" ForeColor="#666633"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td align="right" bgcolor="#66FF66" class="style2">
                Employee</td>
            <td bgcolor="#CCFFCC" class="style3">
                <asp:RadioButtonList ID="RadioButtonList_Employee" runat="server" 
                    DataSourceID="SqlDataSource2" DataTextField="EmpName" DataValueField="EmpID">
                </asp:RadioButtonList>
                <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:CLINICConnectionString %>" 
                    SelectCommand="SELECT EmpID, EmpName, EmpStatus, Contact FROM EMPLOYEE WHERE (EmpID IN (SELECT EmpID FROM EMPLOYEE_SPECIALIZATION WHERE (DeptCode LIKE @DeptCode)))">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="RadioButtonList_Department" 
                            DefaultValue="None" Name="DeptCode" PropertyName="SelectedValue" />
                    </SelectParameters>
                </asp:SqlDataSource>
            </td>
            <td bgcolor="#66FF66">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                    ControlToValidate="RadioButtonList_Employee" 
                    ErrorMessage="Please select the employee" Font-Italic="True" 
                    Font-Names="Segoe UI" ForeColor="#666633"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td align="right" bgcolor="#66FF66" class="style2">
                <asp:Label ID="Label1" runat="server" Text="Date of Checkup"></asp:Label>
            </td>
            <td bgcolor="#CCFFCC" class="style3">
                <BDP:BasicDatePicker ID="BasicDatePicker1" runat="server" />
            </td>
            <td bgcolor="#66FF66">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
                    ControlToValidate="BasicDatePicker1" 
                    ErrorMessage="Please select the date of checkup" Font-Italic="True" 
                    Font-Names="Segoe UI" ForeColor="#666633"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td align="right" bgcolor="#66FF66" class="style2">
                <asp:Label ID="Label2" runat="server" Text="Diagnosis"></asp:Label>
            </td>
            <td bgcolor="#CCFFCC" class="style3">
                <asp:TextBox ID="txtDiagnosis" runat="server" Height="67px" 
                    TextMode="MultiLine" Width="247px"></asp:TextBox>
            </td>
            <td bgcolor="#66FF66">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
                    ControlToValidate="txtDiagnosis" ErrorMessage="Please put the diagnosis" 
                    Font-Italic="True" Font-Names="Segoe UI" ForeColor="#666633" 
                    ValidationGroup="ValidationGroupDaw"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td bgcolor="#66FF66" class="style2">
                &nbsp;</td>
            <td align="center" bgcolor="#CCFFCC" class="style3">
                <asp:Button ID="btnSubmitCheckup" runat="server" 
                    onclick="btnSubmitCheckup_Click" PostBackUrl="~/NewCheckup.aspx" 
                    Text="Submit Checkup" CausesValidation="False" 
                    ValidationGroup="ValidationGroupDaw" />
            </td>
            <td bgcolor="#66FF66">
                &nbsp;</td>
        </tr>
        <tr>
            <td bgcolor="#66FF66" class="style2">
                &nbsp;</td>
            <td bgcolor="#CCFFCC" class="style3">
                &nbsp;</td>
            <td bgcolor="#66FF66">
                &nbsp;</td>
        </tr>
    </table>
    </form>
</body>
</html>
4

2 回答 2

2

您是在说您的按钮无法进行验证吗?尝试将您的 CausesValidation 更改为 true,加油 :)

  <td align="center" bgcolor="#CCFFCC" class="style3">
            <asp:Button ID="btnSubmitCheckup" runat="server" 
                onclick="btnSubmitCheckup_Click" PostBackUrl="~/NewCheckup.aspx" 
                Text="Submit Checkup" CausesValidation="true" 
                ValidationGroup="ValidationGroupDaw" />
        </td>
于 2013-10-04T09:09:00.590 回答
1

首先,您的按钮包含CausesValidation="False"将使其不调用客户端验证。

确实发生的错误是它确实验证了您是否不填写任何代码,但这可能是因为您无法判断按钮事件是否被调用。

至于确保调用验证,您应该在按钮单击事件上添加验证,如下所示:

protected void btnSubmitCheckup_Click(object sender, EventArgs e)
    {
        validate();
        if(page.IsValid)
        {
          //your code here
        } 
       else
       {
         //if you want to do something special if validation fails
       }
    }
于 2013-10-04T09:22:02.640 回答