您可以使用比較驗證器驗證文本框,然後如果頁面通過驗證,則使用double.Parse方法。
<asp:TextBox ID="txtDouble" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ErrorMessage="Input must contain a double." ControlToValidate="txtDouble"
Operator="DataTypeCheck" SetFocusOnError="True" Type="Double"></asp:CompareValidator>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
/*C#*/
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
double d = double.Parse(txtDouble.Text);
}
}
來源
2010-04-14 16:16:47
Jon