自定义控件学习笔记(六)

自定义控件学习笔记(六)--生成回发

1。要点

1)生成回发的含义--不用用户点提交按钮,就自动提交表单
2)使用Page.ClientScript.GetPostBackEventReference(post)达到回发的目的

2。控件

 

using  System;
using  System.Web.UI;
using  System.Collections.Specialized;


namespace  TestCustomControl
{
    
public class GenerateAutoPostBack : Control, IPostBackDataHandler
    
{
        
bool autoPostBack = false;

        
public string Text
        
{
            
get
            
{
                
if (ViewState["myText"!= null)
                
{
                    
string text = (string)ViewState["myText"];
                    
return text;
                }

                
else
                
{
                    
return "";
                }


            }

            
set { ViewState["myText"= value; }
        }

        
        
public bool AutoPostBack
        
{
            
get{return autoPostBack;}
            
set{autoPostBack=value;}
        }

        
        
public event EventHandler TextChanged;

        
protected override void Render(HtmlTextWriter writer)
        
{
            writer.WriteBeginTag(
"input");
            writer.WriteAttribute(
"name", UniqueID);

            
if (ID != null)
            
{
                writer.WriteAttribute(
"id", ClientID);
            }


            
if (Text.Length > 0)
            
{
                writer.WriteAttribute(
"value", Text);


            }

            
            
if(autoPostBack)
            
{
                
//writer.WriteAttribute("onchange","javascript:"+Page.GetPostBackEventReference(this));
                PostBackOptions post = new PostBackOptions(this);
                writer.WriteAttribute(
"onchange""javascript:" + Page.ClientScript.GetPostBackEventReference(post));
            }

            
            writer.Write(HtmlTextWriter.TagRightChar);

            writer.WriteEndTag(
"input");


        }


        
public bool LoadPostData(string postDataKey, NameValueCollection postCollection)
        
{

            
//先用户修改文字并回传后,text被修改(Render)之前引发此事件。
            
//流程:render-》显示-》用户修改-》LoadPostData-》(RaisePostDataChangedEvent)-》Render(第二次)
            string temp = Text;
            Text 
= postCollection[postDataKey];

            
if (temp != Text)
                
return true;//引发RaisePostDataChangedEvent
            else
                
return false;
        }


        
public void RaisePostDataChangedEvent()
        
{
            
if (TextChanged != null)//指客户端使用控件时候,写入了 (OnTextChanged="MethodName"),否则则认为用户没有处理此事件
            {
                TextChanged(
thisnew EventArgs());
            }

        }

    }

}

 

3。用法

 

<% @ Page Language="C#" AutoEventWireup="true" CodeFile="GeneratePostBack.aspx.cs" Inherits="TestCustomControl_First_GeneratePostBack"  %>
<% @ Register Assembly="GenerateAutoPostBack" Namespace="TestCustomControl" TagPrefix="Surance" %>
<! 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 > 无标题页 </ title >
</ head >
< body >
    
< form  id ="form1"  runat ="server" >
    
< div >
    
< Surance:GenerateAutoPostBack  ID ="G1"  Runat ="server"  AutoPostBack ="true"  OnTextChanged ="G1_OnTextChanged"   />
    
</ div >
    
</ form >
</ body >
</ html >

 

后台

 

  protected   void  G1_OnTextChanged( object  sender, EventArgs e)
    
{
        Response.Write(
"Generate");
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值