[英]ASP.net C# enabling and disabling required field validator through javascript
[英]Enabling and disabling field validator for button click
我有兩個按鈕控制。 我想點擊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啟用它們
為什么不將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>
這是因為,您在服務器端代碼中啟用了RequiredFieldValidator
,即按鈕單擊事件處理程序和RequiredFieldValidator
等驗證控件在客戶端工作。 這意味着當您單擊Submit
buttton時,由於您的驗證器已被禁用,因此它無效並且表單已發布到服務器,現在即使您現在啟用Validator,也沒有用,因為表單已經發布。
您應該將每個按鈕控件的CausesValidation屬性設置為true
或false
,而不是啟用和禁用驗證器控件,如下所示: -
<asp:Button ID="SubmitButton" runat="server" Text="Submit"
CausesValidation="true" OnClick="SubmitButton_Click" />
更新:
由於您不希望在btnavail
按鈕上btnavail
驗證,因此btnavail
按鈕設置CausesValidation="false"
,您的標記應如下所示: -
<asp:Button ID="btnavail" runat="server" Text="Book Availability"
CausesValidation="false" OnClick="btnavail_Click" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.