BaseCompareValidator.CanConvert 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
지정된 문자열을 지정된 데이터 형식으로 변환할 수 있는지 여부를 결정합니다.
오버로드
| Name | Description |
|---|---|
| CanConvert(String, ValidationDataType) |
지정된 문자열을 지정된 데이터 형식으로 변환할 수 있는지 여부를 결정합니다. 오버로드된 메서드의 이 버전은 현재 문화권에서 사용하는 형식을 사용하여 통화, double 및 날짜 값을 테스트합니다. |
| CanConvert(String, ValidationDataType, Boolean) |
지정된 문자열을 지정된 데이터 형식으로 변환할 수 있는지 여부를 결정합니다. 오버로드된 메서드의 이 버전을 사용하면 문화권 중립 형식을 사용하여 값을 테스트할지 여부를 지정할 수 있습니다. |
CanConvert(String, ValidationDataType)
지정된 문자열을 지정된 데이터 형식으로 변환할 수 있는지 여부를 결정합니다. 오버로드된 메서드의 이 버전은 현재 문화권에서 사용하는 형식을 사용하여 통화, double 및 날짜 값을 테스트합니다.
public:
static bool CanConvert(System::String ^ text, System::Web::UI::WebControls::ValidationDataType type);
public static bool CanConvert(string text, System.Web.UI.WebControls.ValidationDataType type);
static member CanConvert : string * System.Web.UI.WebControls.ValidationDataType -> bool
Public Shared Function CanConvert (text As String, type As ValidationDataType) As Boolean
매개 변수
- text
- String
테스트할 문자열입니다.
- type
- ValidationDataType
값 중 ValidationDataType 하나입니다.
반품
true지정된 데이터 문자열을 지정된 데이터 형식으로 변환할 수 있으면 이고, 그렇지 않으면 . false
예제
다음 예제에서는 메서드를 CanConvert 사용하여 두 정수 값을 비교하고 두 번째 정수가 변환될 수 있는지 여부를 확인하는 방법을 보여 줍니다.
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>BaseCompareValidator CanConvert Example</title>
<script runat="server">
void Button_Click(Object sender, EventArgs e)
{
// Display whether the value of TextBox1 passes validation.
if (Page.IsValid)
{
lblOutput.Text = "Validation passed! ";
// An input control passes validation if the value it is being
// compared to cannot be converted to the data type specified
// by the BaseCompareValidator.Type property. Be sure to use
// validation controls on each input control independently.
// Test the values being compared for their data types.
ValidateType(Page.IsValid);
}
else
{
lblOutput.Text = "Validation failed! ";
// Test the values being compared for their data types.
ValidateType(Page.IsValid);
}
}
void ValidateType(bool Valid)
{
// Display an error message if the value of TextBox1 cannot be
// converted to the data type specified by the
// BaseCompareValidator.Type property (in this case an integer).
if (!BaseCompareValidator.CanConvert(TextBox1.Text, ValidationDataType.Integer))
{
lblOutput.Text += "The first value is not an integer. ";
}
// Display an error message if the value of TextBox2 cannot be
// converted to the data type specified by the
// BaseCompareValidator.Type property (in this case an integer).
if (!BaseCompareValidator.CanConvert(TextBox2.Text, ValidationDataType.Integer))
{
// An input control passes validation if the value it is being
// compared to cannot be converted to the data type specified
// by the BaseCompareValidator.Type property.
// Display a different message when this scenario occurs.
if(Valid)
{
lblOutput.Text += "However, the second value is not an integer. ";
}
else
{
lblOutput.Text += "The second value is not an integer. ";
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>BaseCompareValidator CanConvert Example</h3>
<p>
Enter an integer in each text box. <br />
Click "Validate" to compare the values <br />
for equality.
</p>
<table style="background-color:#eeeeee; padding:10">
<tr valign="top">
<td>
<h5>Value 1:</h5>
<asp:TextBox id="TextBox1"
runat="server"/>
</td>
<td>
<h5>Value 2:</h5>
<asp:TextBox id="TextBox2"
runat="server"/>
<p>
<asp:Button id="Button1"
Text="Validate"
OnClick="Button_Click"
runat="server"/>
</p>
</td>
</tr>
</table>
<asp:CompareValidator id="Compare1"
ControlToValidate="TextBox1"
ControlToCompare="TextBox2"
EnableClientScript="False"
Type="Integer"
runat="server"/>
<br />
<asp:Label id="lblOutput"
Font-Names="verdana"
Font-Size="10pt"
runat="server"/>
</form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>BaseCompareValidator CanConvert Example</title>
<script runat="server">
Sub Button_Click(sender As Object, e As EventArgs)
' Display whether the value of TextBox1 passes validation.
If Page.IsValid Then
lblOutput.Text = "Validation passed! "
' An input control passes validation if the value it is being
' compared to cannot be converted to the data type specified
' by the BaseCompareValidator.Type property. Be sure to use
' validation controls on each input control independently.
' Test the values being compared for their data types.
ValidateType(Page.IsValid)
Else
lblOutput.Text = "Validation failed! "
' Test the values being compared for their data types.
ValidateType(Page.IsValid)
End If
End Sub
Sub ValidateType(Valid As Boolean)
' Display an error message if the value of TextBox1 cannot be
' converted to the data type specified by the
' BaseCompareValidator.Type property (in this case an integer).
If Not BaseCompareValidator.CanConvert(TextBox1.Text, ValidationDataType.Integer) Then
lblOutput.Text &= "The first value is not an integer. "
End If
' Display an error message if the value of TextBox2 cannot be
' converted to the data type specified by the
' BaseCompareValidator.Type property (in this case an integer).
If Not BaseCompareValidator.CanConvert(TextBox2.Text, ValidationDataType.Integer) Then
' An input control passes validation if the value it is being
' compared to cannot be converted to the data type specified
' by the BaseCompareValidator.Type property.
' Display a different message when this scenario occurs.
If Valid Then
lblOutput.Text &= "However, the second value is not an integer. "
Else
lblOutput.Text &= "The second value is not an integer. "
End If
End If
End Sub
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>BaseCompareValidator CanConvert Example</h3>
<p>
Enter an integer in each text box. <br />
Click "Validate" to compare the values <br />
for equality.
</p>
<table style="background-color:#eeeeee; padding:10">
<tr valign="top">
<td>
<h5>Value 1:</h5>
<asp:TextBox id="TextBox1"
runat="server"/>
</td>
<td>
<h5>Value 2:</h5>
<asp:TextBox id="TextBox2"
runat="server"/>
<p>
<asp:Button id="Button1"
Text="Validate"
OnClick="Button_Click"
runat="server"/>
</p>
</td>
</tr>
</table>
<asp:CompareValidator id="Compare1"
ControlToValidate="TextBox1"
ControlToCompare="TextBox2"
EnableClientScript="False"
Type="Integer"
runat="server"/>
<br />
<asp:Label id="lblOutput"
Font-Names="verdana"
Font-Size="10pt"
runat="server"/>
</form>
</body>
</html>
설명
메서드를 CanConvert(String, ValidationDataType) 사용하여 지정된 문자열을 지정된 데이터 형식으로 변환할 수 있는지 여부를 확인합니다. 이 메서드는 해당 데이터 형식에 따라 달라지는 작업을 수행하기 전에 문자열을 호환되는 데이터 형식으로 변환할 수 있는지 여부를 테스트하는 데 일반적으로 사용됩니다.
이 버전의 메서드는 현재 문화권에서 사용하는 형식을 사용하여 값을 테스트합니다. 문화권 중립 형식을 사용하여 값을 테스트하려면 이 메서드의 BaseCompareValidator.CanConvert(String, ValidationDataType, Boolean) 오버로드된 버전을 사용합니다.
추가 정보
적용 대상
CanConvert(String, ValidationDataType, Boolean)
지정된 문자열을 지정된 데이터 형식으로 변환할 수 있는지 여부를 결정합니다. 오버로드된 메서드의 이 버전을 사용하면 문화권 중립 형식을 사용하여 값을 테스트할지 여부를 지정할 수 있습니다.
public:
static bool CanConvert(System::String ^ text, System::Web::UI::WebControls::ValidationDataType type, bool cultureInvariant);
public static bool CanConvert(string text, System.Web.UI.WebControls.ValidationDataType type, bool cultureInvariant);
static member CanConvert : string * System.Web.UI.WebControls.ValidationDataType * bool -> bool
Public Shared Function CanConvert (text As String, type As ValidationDataType, cultureInvariant As Boolean) As Boolean
매개 변수
- text
- String
테스트할 문자열입니다.
- type
- ValidationDataType
열거형 값 중 ValidationDataType 하나입니다.
- cultureInvariant
- Boolean
true문화권 중립 형식을 사용하여 값을 테스트하려면 그렇지 않으면 . false
반품
true지정된 데이터 문자열을 지정된 데이터 형식으로 변환할 수 있으면 이고, 그렇지 않으면 . false
설명
메서드를 CanConvert(String, ValidationDataType, Boolean) 사용하여 지정된 문자열을 지정된 데이터 형식으로 변환할 수 있는지 여부를 확인합니다. 이 메서드는 해당 데이터 형식에 따라 달라지는 작업을 수행하기 전에 문자열을 호환되는 데이터 형식으로 변환할 수 있는지 여부를 테스트하는 데 일반적으로 사용됩니다. 문화권 중립 형식을 사용하여 값을 테스트해야 함을 나타내려면 매개 변수에 true 전달하고, 그렇지 않으면 현재 문화권에서 cultureInvariant 사용하는 형식을 사용하여 값을 테스트합니다. 현재 문화권에서 사용하는 형식을 사용하여 값을 테스트할 때는 이 메서드의 BaseCompareValidator.CanConvert(String, ValidationDataType) 오버로드된 버전을 사용하는 것이 좋습니다.