单击重置按钮

时间:2015-09-30 05:47:53

标签: c# asp.net validation

单击取消(重置)按钮后单击提交按钮时,验证未触发(未显示错误消息)。

当我在取消按钮之前首先单击提交按钮时,验证工作正常。我已经为两个字段包含了一些javascript验证,并为清除错误消息进行了一次验证。多数民众赞成

(两个文本框的JAVASCRIPT验证在上述条件下都有效)

ASPX代码

  <body>
        <h3>PROJECT DETAILS</h3>
        <form id="form1" runat="server">
            <table align="center" cellpadding="30" >
                <!----- PROJECT NAME ---------------------------------------------------------->
                <tr>
                    <td>PROJECT NAME</td>
                    <td>
                        <p>::</p>
                    </td>
                    <td class="pi">
                        <p>
                            <asp:TextBox ID="txtProjectName" runat="server" ></asp:TextBox>
                            <asp:RequiredFieldValidator ID="rfvProjectName" runat="server" ControlToValidate="txtProjectName" ErrorMessage="Mandatory field" SetFocusOnError="True"></asp:RequiredFieldValidator>
                        </p>
                    </td>
                </tr>
                <!----- DESCRIPTION ---------------------------------------------------------->
                <tr>
                    <td>DESCRIPTION</td>
                    <td>
                        <p>::</p>
                    </td>
                    <td class="pi">
                        <p>
                            <asp:TextBox ID="txtDescription" runat="server" xmlns:asp="#unknown"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="rfvDescription" runat="server" ControlToValidate="txtDescription" ErrorMessage="Mandatory Field"></asp:RequiredFieldValidator>
                        </p>
                    </td>
                </tr>
                <!----- COMPANY NAME ---------------------------------------------------------->
                <tr>
                    <td>COMPANY NAME</td>
                    <td>
                        <p>::</p>
                    </td>
                    <td class="pi">

                    <asp:DropDownList ID="ddlCompanyName" AutoPostBack="True" OnSelectedIndexChanged="ddlCompanyName_SelectedIndexChanged" runat="server" ></asp:DropDownList>

                    <asp:CustomValidator runat="server" ID="CustomValidator1" ClientValidationFunction="JSFunctionValidate" ErrorMessage="Please Select Atleast one Module"></asp:CustomValidator>

                </td>

                <!----- PROJECT STATUS ---------------------------------------------------------->
                <tr>
                    <td>PROJECT STATUS</td>
                    <td>
                        <p>::</p>
                    </td>
                    <td class="pi">
                        <asp:RadioButtonList ID="rblProjectStatus" runat="server" Style="display: inline" RepeatDirection="Horizontal">
                            <asp:ListItem Text="SUCCESS" Value="rbSuccess"></asp:ListItem>
                            <asp:ListItem Text="FAILURE" Value="rbFailure"></asp:ListItem>
                        </asp:RadioButtonList>
                        <asp:RequiredFieldValidator ID="rfvProjectStatus" runat="server" ControlToValidate="rblProjectStatus" ErrorMessage="Mandatory Field"></asp:RequiredFieldValidator>
                </tr>
                </td>
            </tr>
            <!----- WEBSITE URL ---------------------------------------------------------->
                <td>WEBSITE URL</td>
                        <asp:TextBox ID="txtWebsiteUrl" runat="server"></asp:TextBox>
                        <asp:RegularExpressionValidator ID="revWebsiteURL" runat="server" ControlToValidate="txtWebsiteUrl" ErrorMessage="ENTER VALID URL" ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)?"></asp:RegularExpressionValidator>
                        <asp:RequiredFieldValidator ID="rfvWebsiteUrl" runat="server" ControlToValidate="txtWebsiteUrl" Display="Static" ErrorMessage="It is Also Mandatory"></asp:RequiredFieldValidator>
                </td>


            </tr>
            <!----- Project Completed in ---------------------------------------------------------->

                <td>Project Completed in</td>

                        <asp:CheckBoxList ID="chklProjectCompleted" runat="server" Style="display: inline" RepeatDirection="Horizontal">
                            <asp:ListItem ID="chkProject1" runat="server" Text="<1 Month"></asp:ListItem>
                            <asp:ListItem ID="chkProject2" runat="server" Text="1-2 Month"></asp:ListItem>
                        </asp:CheckBoxList>
                        <asp:CustomValidator runat="server" ID="cvmodulelist" ClientValidationFunction="ValidateModuleList" ErrorMessage="Please Select Atleast one Module"></asp:CustomValidator>
                    </p>
                </td>
            </tr>
            <!----- Project Started at---------------------------------------------------------->
      <td>Project Started at</td>
            <asp:TextBox ID="txtProjectDate" ReadOnly="true" runat="server" OnTextChanged="txtProjectDate_TextChanged1"></asp:TextBox>
                    <asp:ImageButton ID="btnShowCalendar" runat="server" OnClick="btnShowCalendar_Click" ImageUrl="D:\calendar4.png" CausesValidation="False" Width="16px" />
                    <asp:Calendar ID="calProjectDate" runat="server" OnSelectionChanged="calProjectDate_SelectionChanged" Visible="False"></asp:Calendar>
                    <asp:RequiredFieldValidator ID="rfvProjectDate" runat="server" ControlToValidate="txtProjectDate" ErrorMessage="MANDATORY"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <!----- SAVE ---------------------------------------------------------->

                        <asp:Button ID="btnSave" runat="server" Text="SAVE" OnClick="btnSave_Click" />

                    <asp:Button ID="btnClear" runat="server" Text="CLEAR" OnClick="btnClear_Click" OnClientClick="return aa()" /></td>

            </tr>

        </table>
        <div>
            <asp:GridView ID="gdDisplay" runat="server" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" Width="1027px">
                <AlternatingRowStyle BackColor="#DCDCDC" />
                <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
                <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
                <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#F1F1F1" />
                <SortedAscendingHeaderStyle BackColor="#0000A9" />
                <SortedDescendingCellStyle BackColor="#CAC9C9" />
                <SortedDescendingHeaderStyle BackColor="#000065" />
            </asp:GridView>
        </div>

    </form>
</body>
</html>

ASPX.CS代码

我已经包含了明确的字段和许多基本功能!

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int a;

            try
            {
                con.Open();
                OracleCommand cmd = con.CreateCommand();

               string interests = string.Empty;
               foreach (ListItem item in this.chklProjectCompleted.Items)          /*Populating selected checkbox values into interests STRING */
               if (item.Selected)
               interests += item + ",";
               string ProjectCompletion= interests.TrimEnd('?', '.', ',');                        /*Method to trim trailing comma in database */

               if (rblProjectStatus.SelectedIndex>0)   /*populating radio button with 0 & 1 VALUES */
               {
                   a = 1;
               }
               else
               {
                   a = 0;
               }
    cmd.CommandText = "INSERT INTO CMPPOJECT(PROJECTNAME,DESCRIPTION,COMPANY,PROJECTSTATUS,WEBSITEURL,COMPLETEDIN,STARTEDIN)VALUES('" + txtProjectName.Text + "','" + txtDescription.Text + "','" + ddlCompanyName.SelectedIndex + "','" + a + "','" + txtWebsiteUrl.Text + "','" + ProjectCompletion + "','" + txtProjectDate.Text + "')";
               cmd.ExecuteNonQuery();
               Response.Write("<script>alert('REGISTRATION SUCCESS')</script>");
               txtDescription.Text = "";
               txtProjectDate.Text = "";
               rblProjectStatus.SelectedIndex = -1;
               txtProjectName.Text = "";
               txtWebsiteUrl.Text = "";
               calProjectDate.Visible = false;
               chkProject1.Selected = false;
               chkProject2.Selected = false;
               ddlCompanyName.SelectedIndex = 0;
     }
            catch(Exception ex)
            {
                 Response.Write("<script>alert('Sorry,some error occured! Please try again!')</script>");
        }
            finally
            {
                con.Close();
                con.Open();
                OracleCommand cmd = con.CreateCommand();
                cmd.CommandText = " Select PROJECTNAME,COMPANY,PROJECTSTATUS,STARTEDIN,COMPLETEDIN FROM CMPPOJECT";
                DataTable dt = new DataTable();
                cmd.CommandType = CommandType.Text;
                OracleDataAdapter da = new OracleDataAdapter(cmd);
                DataTable Dt = new DataTable();
                da.Fill(Dt);
                gdDisplay.DataSource = Dt;
                gdDisplay.DataBind();
               // return Dt;
                con.Close();
   }
      }


protected void btnClear_Click(object sender, EventArgs e)
            {
                txtDescription.Text = "";
                txtProjectDate.Text = "";
                txtProjectName.Text = "";
                txtWebsiteUrl.Text = "";
                calProjectDate.Visible = false;
                chkProject1.Selected = false;
                chkProject2.Selected = false;
                ddlCompanyName.SelectedIndex = 0;
                rblProjectStatus.SelectedIndex = -1;
                rfvDescription.ErrorMessage = "";
                rfvProjectDate.ErrorMessage = "";
                rfvProjectName.ErrorMessage = "";
                rfvProjectStatus.ErrorMessage ="";
                rfvWebsiteUrl.ErrorMessage = "";
                revWebsiteURL.ErrorMessage = "";               
           }

请帮我解决。

0 个答案:

没有答案