1. 程式人生 > >文字伺服器控制元件(標籤Label控制元件和文字框(TextBox)控制元件)

文字伺服器控制元件(標籤Label控制元件和文字框(TextBox)控制元件)

第一個例項:

總體的佈局圖:


佈局實現程式碼:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 請在文字框中輸入整數,計算其和:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <br />
    
    </div>
        <asp:TextBox ID="TextBox1" runat="server" Width="73px"></asp:TextBox>
&nbsp;&nbsp; +&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="TextBox2" runat="server" Width="114px"></asp:TextBox>
&nbsp;&nbsp; =&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Label ID="Label1" runat="server" Text="和"></asp:Label>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="加" />
    </form>


</body>
</html>

按鈕響應事件的程式碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int Answer;
        Answer = Convert.ToInt32(TextBox1.Text) + Convert.ToInt32(TextBox2.Text);
        Label1.Text = Answer.ToString();
    }


}

第二個例項:

頁面彈出警告框:

        //1、Response.Write("<script>alert('查詢語句執行出錯!');window.location.href=DisplayData.aspx</script>");
        //2、Page.RegisterStartupScript("msg", "<script>alert('查詢語句執行出錯!');window.location.href='DisplayData.aspx'</script>");
        //3、ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript> alert('');location.href='';</script>");  
        //4、ScriptManager.RegisterStartupScript(this, this.GetType(), "u1", "alert('內容!')", true);
        //5、Page.ClientScript.RegisterStartupScript(this.GetType(), "d", "alert('請先登入!');location='../login.aspx';", true);


TextBox控制元件

TextBox控制元件又稱文字框控制元件,為使用者提供輸入文字的功能。

1.屬性

TextBox控制元件的常用屬性及說明如表1所示。

表1 TextBox控制元件常用屬性及說明

屬    性

說    明

AutoPostBack

獲取或設定一個值,該值指示無論何時使用者在TextBox控制元件中按〈Enter〉鍵或〈Tab〉鍵時,是否自動回發到伺服器的操作

CausesValidation

獲取或設定一個值,該值指示當TextBox控制元件設定為在回發發生時進行驗證,是否執行驗證

ID

控制元件ID

Text

控制元件要顯示的文字

TextMode

獲取或設定TextBox控制元件的行為模式(單行、多行或密碼)

Width

控制元件的寬度

Visible

控制元件是否可見

ReadOnly

獲取或設定一個值,用於指示能否只讀TextBox控制元件的內容

CssClass

控制元件呈現的樣式

BackColor

控制元件的背景顏色

Enabled

控制元件是否可用

TextBox控制元件大部分屬性設定和Label控制元件類似,具體請參見Label控制元件屬性設定,下面主要介紹一下TextMode屬性。

TextMode屬性主要用於控制TextBox控制元件的文字顯示方式,該屬性的設定選項有以下3種。

l         單行(SingleLine):使用者只能在一行中輸入資訊,還可以選擇限制控制元件接收的字元數。

l         多行(MultiLine):文字很長時,允許使用者輸入多行文字並執行換行。

l         密碼(Password):將使用者輸入的字元用黑點(●)遮蔽,以隱藏這些資訊。

wrap輸入的文字是否自動換行



2.方法

TextBox控制元件常用方法同Label控制元件類似,表2列出了它的一些常用方法。

表2 TextBox控制元件常用方法及說明

方    法

說    明

DataBind

將資料來源繫結到被呼叫的伺服器控制元件及其所有子控制元件上

Focus

為控制元件設定輸入焦點

Dispose

使伺服器控制元件得以在從記憶體中釋放之前執行最後的清理操作

Equals

確定兩個物件例項是否相等