TextBox.Columns 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置文本框的显示宽度(以字符为单位)。
public:
virtual property int Columns { int get(); void set(int value); };
[System.ComponentModel.Bindable(true)]
public virtual int Columns { get; set; }
public virtual int Columns { get; set; }
[<System.ComponentModel.Bindable(true)>]
member this.Columns : int with get, set
member this.Columns : int with get, set
Public Overridable Property Columns As Integer
属性值
文本框的显示宽度(以字符为单位)。 默认值为 0,表示未设置该属性。
- 属性
例外
指定的宽度小于 0。
示例
下面的代码示例演示如何使用 Columns 属性为 TextBox 控件指定 2 个字符的宽度。
重要
此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述。
<%@ 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>
<title> TextBox Example </title>
<script runat="server">
protected void AddButton_Click(Object sender, EventArgs e)
{
int Answer;
Answer = Convert.ToInt32(Value1.Text) + Convert.ToInt32(Value2.Text);
AnswerMessage.Text = Answer.ToString();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3> TextBox Example </h3>
<table>
<tr>
<td colspan="5">
Enter integer values into the text boxes. <br />
Click the Add button to add the two values. <br />
Click the Reset button to reset the text boxes.
</td>
</tr>
<tr>
<td colspan="5">
</td>
</tr>
<tr align="center">
<td>
<asp:TextBox ID="Value1"
Columns="2"
MaxLength="3"
Text="1"
runat="server"/>
</td>
<td>
+
</td>
<td>
<asp:TextBox ID="Value2"
Columns="2"
MaxLength="3"
Text="1"
runat="server"/>
</td>
<td>
=
</td>
<td>
<asp:Label ID="AnswerMessage"
runat="server"/>
</td>
</tr>
<tr>
<td colspan="2">
<asp:RequiredFieldValidator
ID="Value1RequiredValidator"
ControlToValidate="Value1"
ErrorMessage="Please enter a value.<br />"
Display="Dynamic"
runat="server"/>
<asp:RangeValidator
ID="Value1RangeValidator"
ControlToValidate="Value1"
Type="Integer"
MinimumValue="1"
MaximumValue="100"
ErrorMessage="Please enter an integer <br /> between than 1 and 100.<br />"
Display="Dynamic"
runat="server"/>
</td>
<td colspan="2">
<asp:RequiredFieldValidator
ID="Value2RequiredValidator"
ControlToValidate="Value2"
ErrorMessage="Please enter a value.<br />"
Display="Dynamic"
runat="server"/>
<asp:RangeValidator
ID="Value2RangeValidator"
ControlToValidate="Value2"
Type="Integer"
MinimumValue="1"
MaximumValue="100"
ErrorMessage="Please enter an integer <br /> between than 1 and 100.<br />"
Display="Dynamic"
runat="server"/>
</td>
<td>
 
</td>
</tr>
<tr align="center">
<td colspan="4">
<asp:Button ID="AddButton"
Text="Add"
OnClick="AddButton_Click"
runat="server"/>
</td>
<td>
</td>
</tr>
</table>
</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>
<title> TextBox Example </title>
<script runat="server">
Protected Sub AddButton_Click(sender As Object, e As EventArgs)
Dim Answer As Integer
Answer = Convert.ToInt32(Value1.Text) + Convert.ToInt32(Value2.Text)
AnswerMessage.Text = Answer.ToString()
End Sub
</script>
</head>
<body>
<form id="form1" runat="server">
<h3> TextBox Example </h3>
<table>
<tr>
<td colspan="5">
Enter integer values into the text boxes. <br />
Click the Add button to add the two values. <br />
Click the Reset button to reset the text boxes.
</td>
</tr>
<tr>
<td colspan="5">
</td>
</tr>
<tr align="center">
<td>
<asp:TextBox ID="Value1"
Columns="2"
MaxLength="3"
Text="1"
runat="server"/>
</td>
<td>
+
</td>
<td>
<asp:TextBox ID="Value2"
Columns="2"
MaxLength="3"
Text="1"
runat="server"/>
</td>
<td>
=
</td>
<td>
<asp:Label ID="AnswerMessage"
runat="server"/>
</td>
</tr>
<tr>
<td colspan="2">
<asp:RequiredFieldValidator
ID="Value1RequiredValidator"
ControlToValidate="Value1"
ErrorMessage="Please enter a value.<br />"
Display="Dynamic"
runat="server"/>
<asp:RangeValidator
ID="Value1RangeValidator"
ControlToValidate="Value1"
Type="Integer"
MinimumValue="1"
MaximumValue="100"
ErrorMessage="Please enter an integer <br /> between than 1 and 100.<br />"
Display="Dynamic"
runat="server"/>
</td>
<td colspan="2">
<asp:RequiredFieldValidator
ID="Value2RequiredValidator"
ControlToValidate="Value2"
ErrorMessage="Please enter a value.<br />"
Display="Dynamic"
runat="server"/>
<asp:RangeValidator
ID="Value2RangeValidator"
ControlToValidate="Value2"
Type="Integer"
MinimumValue="1"
MaximumValue="100"
ErrorMessage="Please enter an integer <br /> between than 1 and 100.<br />"
Display="Dynamic"
runat="server"/>
</td>
<td>
 
</td>
</tr>
<tr align="center">
<td colspan="4">
<asp:Button ID="AddButton"
Text="Add"
OnClick="AddButton_Click"
runat="server"/>
</td>
<td>
</td>
</tr>
</table>
</form>
</body>
</html>