2016-11-06 98 views
0

我想設置RangeValidator控件的最大值和最小值如下編程:嘗試設置RangeValidator控件的最大值和最小值編程在ASP.NET

<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="RangeValidator" 
MaximumValue="<%# Max %>" MinimumValue="<%# Min %>" Type="Integer" > 
Value should be in <%# Min %> and <%# Max %> </asp:RangeValidator> 

這裏,最小和最大的整數後面的代碼。但是出現錯誤: 「RangeValidator1」的MaximumValue屬性的值''無法轉換爲'Integer'類型。「 我會很感激這方面的幫助。由於

+0

嘗試顯式地在您的RangeValidator綁定中將Max和Min轉換爲Integer。 – Zeeshan

回答

0
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server" > 
<asp:RangeValidator ID="RangeValidator1" runat="server"ErrorMessage="RangeValidator"></asp:RangeValidator> 
    </form> 
</body> 
</html> 

代碼對於代碼隱藏

protected void Page_Load(object sender, EventArgs e) 
     { 
      RangeValidator1.MinimumValue = 1.ToString(); 
      RangeValidator1.MaximumValue = 5.ToString(); 
     } 

上面的代碼可以幫助你做它的工作。

0

以下是RangeValidator的完整語法。

<asp:RangeValidator 
     id="ProgrammaticID" 
     ControlToValidate="ProgrammaticID of control to validate" 
     MinimumValue="value" 
     MaximumValue="value" 
     Type="DataType" 
     ErrorMessage="Message to display in ValidationSummary control" 
     Text="Message to display in control" 
     ForeColor="value" 
     BackColor="value" 
     runat="server" > 
    </asp:RangeValidator> 

控件的作用拋出異常如果由MaximumValue或MinimumValue屬性指定的值不能被轉換到由Type屬性指定的數據類型。 MSDN Source

所以嘗試設置類型屬性正確或改變最大值適當類型。在你的情況下,嘗試轉換最小最大到Integer。

相關問題