自己做控件小例子

自己做控件,也很容易。贴出来,给初学的人提供经验

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.ComponentModel;

namespace ISDReport.control
{    
    
public class DemoLinkButton:System.Web.UI.WebControls.WebControl,IPostBackEventHandler
    
{    
        
public DemoLinkButton()
        
{
        }

    
        [Bindable(
false),
        Category(
"Appearance"), 
        DefaultValue(
"")] 
        
        
public string Text 
        
{
            
get
            
{
                
string _text = (string)ViewState["Text"];
                
return _text==null?"":_text;
            }

            
set
            
{
                ViewState[
"Text"= value;
            }

        }


        
protected override void OnPreRender(System.EventArgs e)
        
{
             
string scriptClick="<script language='javascript'>function DoOnClick(){"
                 
+((HyperLinkClicked==null)?"":(Page.GetPostBackClientEvent(this,"Clicked"))
                 
+"; return;}</script>");
            Page.RegisterClientScriptBlock(
"OnClicked",scriptClick);
        }


        
protected override void Render(HtmlTextWriter writer)
        
{
            
if(this.Text=="")
            
{
                writer.Write(
"<a href=\"javascript:DoOnClick();\" id=\""+this.ClientID+"\">DemoLinkButton</a>");
            }

            
else{
                writer.Write(
"<a href=\"javascript:DoOnClick();\" id=\""+this.ClientID+"\">"+this.Text+"</a>");
            }

        }


        
public void RaisePostBackEvent(string eventArgument)
        
{
            
if(eventArgument=="Clicked")  HyperLinkClicked(this,System.EventArgs.Empty);
        }
            


        
public delegate void HyperLinkClickedHandle(object sender,System.EventArgs e);
        
public event HyperLinkClickedHandle HyperLinkClicked;

    }

}

posted @ 2005-08-02 11:00  让心灵去旅行  阅读(897)  评论(4编辑  收藏  举报