启用和禁用按钮单击的字段验证器

时间:2014-12-15 06:58:45

标签: c# asp.net

我有两个按钮控制。我点击btnavail_Click时要禁用文本框的字段验证程序。它的工作。但是当我单击按钮btnsubmit_Click时,字段验证器未启用。

 protected void btnsubmit_Click(object sender, EventArgs e)
        {
            RequiredFieldValidator2.Enabled = true;
            RequiredFieldValidator3.Enabled = true;
            RequiredFieldValidator4.Enabled = true;
            con.Open();
            SqlCommand cmd1 = new SqlCommand("Select * from tblstudentinfo where StudentId=@studentid", con);
            cmd1.Parameters.AddWithValue("@studentid",txtstudentid.Text.ToString());
            SqlDataReader rdr=cmd1.ExecuteReader();
            if(rdr.Read())
            {
                int i=Convert.ToInt32(rdr["Password"]);
                int j=Convert.ToInt32(txtpassword.Text);
                if(i == j)
                {
                    SqlCommand cmd2 = new SqlCommand("select * from tblbookinfo where Name=@bookname", con);
                    cmd2.Parameters.AddWithValue("@bookname", txtbookname.Text.ToString());
                    SqlDataReader rdr1 = cmd2.ExecuteReader();
                    if (rdr1.Read())
                    {
                        int s = Convert.ToInt32(rdr1["BookId"]);
                        SqlCommand cmd = new SqlCommand("inserttotbllendinginfo2", con);
                        cmd.CommandType = CommandType.StoredProcedure;

                        cmd.Parameters.Add(new SqlParameter
                        {
                            ParameterName = "@bookid",
                            SqlDbType = SqlDbType.Int,
                            Value = s
                        });
                        cmd.Parameters.Add(new SqlParameter
                        {
                            ParameterName = "@studentid",
                            SqlDbType = SqlDbType.Int,
                            Value = Convert.ToInt32(txtstudentid.Text)
                        });
                        cmd.Parameters.Add(new SqlParameter
                        {
                            ParameterName = "@noofbooks",
                            SqlDbType = SqlDbType.Int,
                            Value = Convert.ToInt32(txtnoofbook.Text)
                        });
                        cmd.Parameters.Add(new SqlParameter
                        {
                            ParameterName = "@dateoflending",
                            SqlDbType = SqlDbType.Date,
                            Value = txtdate.Text
                        });
                        cmd.ExecuteNonQuery();
                    }
                 }
                else
                {
                    lblpassword.Visible=true;
                    lblpassword.Text="Your password is incorrect. Please Check it";
                    lblpassword.ForeColor = System.Drawing.Color.Red;
                }

                con.Close();
              }
        }

        protected void btnavail_Click(object sender, EventArgs e)
        {
            RequiredFieldValidator2.Enabled = false;
            RequiredFieldValidator3.Enabled = false;
            RequiredFieldValidator4.Enabled = false;

            SqlCommand cmd = new SqlCommand("select * from tblbookinfo where Name=@bookname", con);
            cmd.Parameters.AddWithValue("@bookname", txtbookname.Text.ToString());
            con.Open();
            if (String.IsNullOrEmpty(txtbookname.Text))
            {
                lblbookavail.Visible = true;
                lblbookavail.Text = "Please enter a valid book name";
                lblbookavail.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
            SqlDataReader rdr = cmd.ExecuteReader();            
                if (rdr.Read())
                {
                    string s = rdr["Name"].ToString();
                    if (s != null)
                    {
                        int i = Convert.ToInt32(rdr["AvailableBooks"]);
                        lblbookavail.Visible = true;
                        lblbookavail.Text = (i.ToString());
                    }
            }
            }
           con.Close();
            }

我将两个按钮的原因验证属性设置为false。我点击btnsubmit_Click时需要启用验证器。请有人帮助我。

<table align="center">
            <tr><td>Book Name</td><td><asp:TextBox ID="txtbookname" runat="server"></asp:TextBox></td>
                <td><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Requires book name" ControlToValidate="txtbookname" ForeColor="Red" Text="*"></asp:RequiredFieldValidator></td>
                <td><asp:Button ID="btnavail" runat="server" Text="Book Availability"   OnClick="btnavail_Click" /></td><td><asp:Label ID="lblbookavail" runat="server" Visible="false"></asp:Label></td></tr>
            <tr><td>Student Id</td><td><asp:TextBox ID="txtstudentid" runat="server" ValidationGroup="save"></asp:TextBox></td>
                <td><asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Requires Studentid" ControlToValidate="txtstudentid" ForeColor="Red" Text="*"></asp:RequiredFieldValidator></td></tr>
            <tr><td>No of Book</td><td><asp:TextBox ID="txtnoofbook" runat="server" ></asp:TextBox></td>
                <td><asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Requires no of book" ControlToValidate="txtnoofbook" ForeColor="Red" Text="*"></asp:RequiredFieldValidator></td></tr>
            <tr><td>Date of lending</td><td><asp:TextBox ID="txtdate" runat="server" ></asp:TextBox></td></tr>
            <tr><td>Password</td><td><asp:TextBox ID="txtpassword" TextMode="Password" runat="server"  ></asp:TextBox></td>
                <td><asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Requires Studentid" ControlToValidate="txtpassword" ForeColor="Red" Text="*"></asp:RequiredFieldValidator></td>
                <td><asp:Label ID="lblpassword" runat="server" Visible="false"></asp:Label></td></tr>
            <tr><td><asp:Button ID="btnsubmit" runat="server" Text="Submit"  OnClick="btnsubmit_Click" /></td></tr>
            </table>

这是我的HTML代码。我想在btnavail_click上为studentid禁用Field验证器,禁用书籍和密码。我需要再次为btnsubmit_click

启用它们

2 个答案:

答案 0 :(得分:1)

为什么不将ValidationGroup添加到您的文本框和按钮(btnsubmit)这样的内容

<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="save"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
            ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1" 
            ValidationGroup="save"></asp:RequiredFieldValidator>
        <asp:TextBox ID="TextBox2" runat="server" ValidationGroup="save"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
            ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox2" 
            ValidationGroup="save"></asp:RequiredFieldValidator>
        <asp:Button ID="btnAvail" runat="server" Text="Button" ValidationGroup="save" 
            onclick="btnAvail_Click" />
        <asp:Button ID="btnsubmit" runat="server" Text="Button" 
            onclick="btnsubmit_Click" />

在这里,我给文本框提供了一个通用的验证组(我想在其上激活必要的字段验证)并提交按钮。因此,我的所有验证都只会在提交按钮上激活。

使用这种方法的好处是,您的所有工作都将在客户端完成,无需担心启用和禁用验证。

<强>更新 只需按原样使用此代码即可。

<table align="center">
    <tr>
        <td>
            Book Name
        </td>
        <td>
            <asp:TextBox ID="txtbookname" runat="server"></asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Requires book name"
                ControlToValidate="txtbookname" ForeColor="Red" Text="*"  ValidationGroup="avail"></asp:RequiredFieldValidator>
        </td>
        <td>
            <asp:Button ID="Button1" runat="server" Text="Book Availability" OnClick="btnavail_Click"  ValidationGroup="avail"/>
        </td>
        <td>
            <asp:Label ID="lblbookavail" runat="server" Visible="false"></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            Student Id
        </td>
        <td>
            <asp:TextBox ID="txtstudentid" runat="server" ValidationGroup="save"></asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Requires Studentid"
                ControlToValidate="txtstudentid" ForeColor="Red" Text="*"  ValidationGroup="save"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td>
            No of Book
        </td>
        <td>
            <asp:TextBox ID="txtnoofbook" runat="server"></asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="Requires no of book"
                ControlToValidate="txtnoofbook" ForeColor="Red" Text="*"  ValidationGroup="save"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td>
            Date of lending
        </td>
        <td>
            <asp:TextBox ID="txtdate" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            Password
        </td>
        <td>
            <asp:TextBox ID="txtpassword" TextMode="Password" runat="server"></asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ErrorMessage="Requires Studentid"
                ControlToValidate="txtpassword" ForeColor="Red" Text="*"  ValidationGroup="save"></asp:RequiredFieldValidator>
        </td>
        <td>
            <asp:Label ID="lblpassword" runat="server" Visible="false"></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Button ID="Button2" runat="server" Text="Submit" 
                OnClick="btnsubmit_Click"  ValidationGroup="save" style="height: 26px"/>
        </td>
    </tr>
</table>

答案 1 :(得分:0)

这是因为,您在服务器端代码中启用了RequiredFieldValidator,即按钮点击事件处理程序和RequiredFieldValidator等验证控件在客户端工作。这意味着当你点击Submit按钮时,由于你的验证器被禁用,它没有工作,表格被发布到服务器,现在即使你现在启用验证器,它也没用,因为表格已经发布。

而不是启用&amp;要禁用验证程序控件,您应将每个按钮控件的CausesValidation属性设置为truefalse,如下所示: -

<asp:Button ID="SubmitButton" runat="server" Text="Submit" 
            CausesValidation="true" OnClick="SubmitButton_Click" />

<强>更新

由于您不希望在btnavail按钮上进行验证,请为此按钮设置CausesValidation="false",您的加价应如下所示: -

<asp:Button ID="btnavail" runat="server" Text="Book Availability" 
            CausesValidation="false" OnClick="btnavail_Click" />