ive试图以库存形式获取产品的总数,并将其用作范围验证器的最大值,同时将产品添加到购物车中。我在前端使用了代码:
<td class="style2">
QUANTITY<br />
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="must fill some value"></asp:RequiredFieldValidator>
<asp:RangeValidator ID="RangeValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="must fill value lesser than the Total Quantity in stock"
MaximumValue="<%=maxValue %>" MinimumValue="1" Display="Dynamic" ></asp:RangeValidator>
<br />
后端CS代码是:
public partial class productDetail : System.Web.UI.Page
{
public int maxValue=0;
public int minValue = 1;
.....
protected void Page_Load(object sender, EventArgs e)
{
//Defined SQL Conn string...
connection1.Open();
string cartCmd = "select TotalQuantity from Product where ProductName= '"+prodName+"';";
SqlCommand cmd = new SqlCommand(cartCmd, connection1);
SqlDataReader readerTotQquantity = cmd.ExecuteReader();
if (readerTotQquantity .HasRows)
{
readerTotQquantity .Read();
TotalQuantity = readerTotQquantity ["TotalQuantity"].ToString();
}
double val = Convert.ToDouble(TotalQuantity);
maxValue = (int)val;
// also tried maxValue=Convert.ToInt32(TotalQuantity);
// tired maxValue=int.Parse(TotalQuantity); etc
connection1.Close();
}
}
我很累以在Visual Studio输出面板上打印值,并显示正确的值和类型为System.int32。但是页面上的错误显示"最大值&lt;%= maxValue%>不能小于rangeValidator1的最小值1"。
我还尝试了多次将/删除type =" integer"添加到rangeValidator,但错误仍然存在。请帮助我,因为这花费了几个小时的时间来弄清楚它出错的地方。
范围验证器是符文服务器控制器,并且此<%=
语句为其延迟运行。页面渲染时运行。
您可以使用'&lt;%#'此语句。并绑定您的范围验证器。
<%#maxValue %>
您需要调用验证者或验证者任何父母DataBind()
方法。
maxValue = Convert.ToInt32(TotalQuantity);
RangeValidator1.DataBind();
或只是设置RangeValidator1.maximumValue直接:)
RangeValidator1.MaximumValue = Convert.ToInt32(TotalQuantity);