TextBox的属性和AutoPostBack的使用

来源:互联网 发布:js压缩视频文件 编辑:程序博客网 时间:2024/05/20 14:41

html源码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TextBox.aspx.cs" Inherits="TextBox" %>

<!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>TextBox</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
&nbsp;
        
<asp:TextBox ID="tb1" runat="server" style="z-index: 100; left: 83px; position: absolute; top: 78px"></asp:TextBox>
        
<asp:Button ID="btn1" runat="server" Style="z-index: 101; left: 86px; position: absolute;
            top: 113px"
 Text="显示" OnClick="btn1_Click" Height="27px" Width="78px" />
        
<asp:TextBox ID="tb2" runat="server" Style="z-index: 102; left: 266px; position: absolute;
            top: 76px"
></asp:TextBox>
        这个类子的实现是单击按纽显示将左边的TEXTBOX中的数据显示到右边的TEXTBOX中来.我们使用的是Server.HtmlEncode()函数,该函数的目的是,当我们输入带有标记,也就是说输入相当于HTML语言的内容时候,我们可以避免在Web应用程序的脚本入侵.
<br />
        
<br />
        
<br />
        
<br />
        
<br />
        
&nbsp;下面要说的可能就是TextBox里面的一个经常使用的属性AutoPostBack<br />
        它包括两个属性值true/false默认的为false,当他为false的时候对应值的改变后就不会发生自动回传服务器处理.
<br />
        说得有点悬乎,来个类子吧,我们设置一个TextChanged事件,这个事件我也要稍微解释下,他的事件只有在页面回传的时候才会被激发,也就是说当页面与服务器交换数据的时候才会被激发,在没有触发一个其他事件让数据回传的时候,这个事件数据数据沉默状态.
<br />
        我们所要做的就是将AutoPostBack的属性值设置为True这样当TextBox的内容被改变的时候就直接回传.(还有一个要说的就是,我们所用的这个属性,不是每改变一个字符数据就回传一次,那样将降低我们页面使用的效率.现在的情况是当文本框获得焦点和失去焦点的时候并且文本内容改变了
        
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"
            Style
="z-index: 103; left: 80px; position: absolute; top: 361px"></asp:TextBox>
        那么就可以触发事件TextChange
        
<asp:Label ID="Label1" runat="server" Style="z-index: 105; left: 264px; position: absolute;
            top: 364px"
 Text="Label"></asp:Label>
        d
<br />
        
<br />
        
<br />
        
<br />
        
<br />
        上面是两个控件,一个TextBox,一个Label将上面所说的东西在这个里面实现
</div>
    
</form>
</body>
</html>

TextBox.aspx.cs源码

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

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

    }

    
protected void btn1_Click(object sender, EventArgs e)
    
{
        
this.tb2.Text = Server.HtmlEncode(this.tb1.Text);
    }

    
protected void TextBox1_TextChanged(object sender, EventArgs e)
    
{
        
this.Label1.Text = Server.HtmlEncode(this.TextBox1.Text);
    }

}

 

原创粉丝点击