我已经在stackoverflow和google上到处查看但是没有找到任何我可以使用的成功答案,所以我决定在这里发布我的问题。
我有一个asp标签,显示用户可以分配的优惠券总数和一个asp文本框,要求用户输入他们想要分配的优惠券数量。
我需要找到一种方法来检查文本框中输入的值是否小于或等于标签中的数字。
我在我的ascx页面上使用了比较验证器,但这似乎并没有起作用。
这是我目前的代码
这是我的前端代码: ASCX
<h4>
<b>
<asp:Label runat="server" ID="lblAvailableVoucherCount" Visible="false" ></asp:Label>
<asp:Label runat="server" ID="lblCount" Visible="false"></asp:Label>
</b>
</h4>
<table>
<tr>
<td>
<b>
<asp:Label runat="server" ID="lblGrp" Text="CHOOSE A GROUP" Visible="false"></asp:Label>
</b>
<asp:DropDownList runat="server" ID="GroupsDropDownList" Width="160px"
AutoPostBack="false" Visible ="false"></asp:DropDownList>
</td>
</tr>
<tr>
<td>
<b>
<asp:Label runat="server" ID="number"
Text="ENTER THE NUMBER OF VOUCHERS YOU WANT TO ASSIGN TO THE GROUP"
Visible="false" ></asp:Label>
</b>
<asp:TextBox ID="noOfVouchers" runat="server" Visible = "false"/>
</td>
<td>
<asp:CompareValidator runat="server"
ErrorMessage="You DO NOT have enough vouchers available to be assigned to the group!"
ControlToValidate="noOfVouchers" ValueToCompare="lblCount"
Operator="LessThanEqual" Type="String" ForeColor="#FF0000"
Font-Bold="true"></asp:CompareValidator>
</td>
<td>
<asp:RequiredFieldValidator runat="server"
ID="RequiredFieldValidatorNumVouchers"
ControlToValidate="noOfVouchers" Text="Required"
ErrorMessage="You MUST enter the number of vouchers you wish to assign the the group!"
></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label runat="server" ID="lbltest2" ForeColor="#FF0000"
Font-Bold="true" Visible="true"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Button runat="server" ID="assign" Text="Assign"
OnClick="assign_voucher_click" Visible="false" Width="150"
Height="35px"></asp:Button>
</td>
</tr>
</table>
这是我的后端代码: ascx.cs
//Assigning Vouchers to Groups
protected void assign_voucher_click(object sender, System.EventArgs e)
{
group_name = GroupsDropDownList.SelectedValue;
string InstId = "";
using (OdbcConnection dbConnection = new OdbcConnection(ConnStr))
{
dbConnection.Open();
{
OdbcCommand dbCommand = new OdbcCommand();
dbCommand.Connection = dbConnection;
dbCommand.CommandText = @"SELECT voucher_no_pk FROM vouchers WHERE admin_admin_email_id = ? AND voucher_status = 'NEW' LIMIT " + Convert.ToInt32(noOfVouchers.Text) + ";";
dbCommand.Parameters.AddWithValue("@admin_admin_email_id", username);
dbCommand.Parameters.AddWithValue("LIMIT", noOfVouchers.Text.ToString());
dbCommand.ExecuteNonQuery();
OdbcDataReader dataReader = dbCommand.ExecuteReader();
while (dataReader.Read())
{
Voucher_list.Add(dataReader["voucher_no_pk"]);
}
if (Voucher_list.Count == 0)
{
lblCount.Text = "Sorry your DO NOT have enough vouchers available";
}
dataReader.Close();
OdbcCommand dbCommand1 = new OdbcCommand();
dbCommand1.Connection = dbConnection;
//fetching the group id for the selected group name
dbCommand1.CommandText = @"SELECT group_id_pk FROM vouchergroup WHERE group_name = ? AND admin_admin_email_id = ?;";
dbCommand1.Parameters.AddWithValue("@group_name", group_name);
dbCommand1.Parameters.AddWithValue("@admin_admin_email_id", username);
dbCommand1.ExecuteNonQuery();
OdbcDataReader dataReader1 = dbCommand1.ExecuteReader();
while (dataReader1.Read())
{
group_id = dataReader1["group_id_pk"].ToString();
}
dataReader1.Close();
lbltest2.Text = "group id selected for group:'" + group_name + "'";
OdbcCommand dbCommand4 = new OdbcCommand();
dbCommand4.Connection = dbConnection;
dbCommand4.CommandText = @"SELECT instructor_inst_email FROM vouchergroup WHERE group_id_pk = " + group_id + ";";
dbCommand4.ExecuteNonQuery();
OdbcDataReader dataReader4 = dbCommand4.ExecuteReader();
while (dataReader4.Read())
{
InstId = dataReader4["instructor_inst_email"].ToString();
}
dataReader4.Close();
OdbcCommand dbCommand2 = new OdbcCommand();
dbCommand2.Connection = dbConnection;
//updating the vouchers table with the group id, voucher status and instructor email id
for (int i = 0; i < (Voucher_list.Count); i++)
{
dbCommand2.CommandText = @"UPDATE vouchers SET voucher_group_voucher_group_id =" + group_id + ", voucher_status = 'Available to Assign', instructor_instructor_email = ? WHERE voucher_no_pk = " + Convert.ToInt32(Voucher_list[i]) + ";";
dbCommand2.Parameters.AddWithValue("@instructor_instructor_email", InstId);
dbCommand2.ExecuteNonQuery();
}
createInstructorEmail(InstId, group_name, Voucher_list.Count);
if (Voucher_list.Count != 0)
{
lbltest2.Text = "Congratulations! Your vouchers have been assigned to group:<i>" + group_name + "</i>";
}
OdbcCommand dbCommand3 = new OdbcCommand();
dbCommand3.Connection = dbConnection;
dbCommand3.CommandText = @"SELECT vg.group_name AS 'GROUP NAME', vg.instructor_inst_email AS 'GROUP OWNER EMAIL', count(vo.voucher_no_pk) AS `count` FROM vouchergroup vg LEFT JOIN vouchers vo ON vg.group_id_pk = vo.voucher_group_voucher_group_id AND vo.admin_admin_email_id = ? GROUP BY vg.group_id_pk ;";
dbCommand3.Parameters.AddWithValue("@admin_admin_email_id", username);
dbCommand3.ExecuteNonQuery();
OdbcDataReader dataReader2 = dbCommand3.ExecuteReader();
if (dataReader2.HasRows)
{
groupList.DataSource = dataReader2;
groupList.DataBind();
dataReader2.Close();
}
else
{
assign.Visible = false;
lblGrp.Visible = false;
GroupsDropDownList.Visible = false;
number.Visible = false;
noOfVouchers.Visible = false;
lblgroupcreated.Text = "You DO NOT have any Groups yet. Create a New Group and then assign vouchers to them!";
}
}
dbConnection.Close();
}
noOfVouchers.Text = "";
assignMore.Visible = true;
assign.Visible = false;
}
我一直在尝试我能想到的一切,所以任何帮助都会受到高度赞赏 我有一种感觉,我在ascx.cs代码中的某个地方错过了if else语句,这可能是原因,但我对c#和asp.net来说相当新,我&#39 ;我不确定。
答案 0 :(得分:0)
好吧,如果你想要比较两个控制,那就是:
controltocompare而不是ValueToCompare
controltovalidate =“noOfVouchers”
controltocompare = “lblCount”强>
示例:
<asp:TextBox runat="server" id="txtSmallNumber" />
<asp:TextBox runat="server" id="txtBigNumber" />
<asp:CompareValidator runat="server" id="cmpNumbers" controltovalidate="txtSmallNumber" controltocompare="txtBigNumber" operator="LessThan" type="Integer" errormessage="The first number should be smaller than the second number!" /><br />