这个控件一定要用这个方式来写,如果用CreateChildControls等方法,我要改的太多了
public sealed class t:Page
{
   protected override void Render(HtmlTextWriter htw)
  {
    htw.Write("123");
  }
}我调用这个控件时,想这样用
<cc:t id="s" runat="server"/>public void Page_Load(object Sender,EventArgs e)
{
s.Attributes.Add("onClick","return confirm('...');");
s.Click+=new EventHandler(m);
}
public void m(object Sender,EventArgs e)
{
...
}如何实现这个控件??

解决方案 »

  1.   

    http://www.cnblogs.com/chating/archive/2004/12/10/75093.html
      

  2.   

    s.Attributes.Add("onClick","return confirm('...');");
    s.Click+=new EventHandler(m);不是已經加上click的屬性了嗎
      

  3.   

    自定义控件加click事件?  那为什么不直接在里面加一个BUTTON呢???
      

  4.   

    protected override void Render(HtmlTextWriter output) 
    {
       output.Write(String.Format("<div onclick=\"javascript:{0}\" >click me</div>", Page.GetPostBackEventReference(this));   
    }
    用这个解决了Click的问题,但我不明白是什么机制?javascript:{0}是什么意思?s.Attributes.Add("onClick","return confirm('...');"); 这个应该怎么加???
      

  5.   

    你在写控件的时候就应该在控件里面添加click事件
      

  6.   

    to:aaron_lly(永远,永远,等她回来!) 
    我要做个自定义的菜单,用button太难看了,用linkbutton的话,只有点到字上才能触发click,我要点到区域内就能触发click请不要根据这个解答,我要的是我现在的情况解决我的问题,如题!!
      

  7.   

    javascript:{0}是什么意思?
    ===================
    你看啊,那是string.format方法,你还是去了解一下,格式化字符串..s.Attributes.Add("onClick","return confirm('...');"); 这个应该怎么加???
    可以添加在Render方法中
      

  8.   

    hackate(兰花开香入梦境,独思佳人亦飘然!!) 
    怎么做?
      

  9.   


    using System;
    using System.IO;
    using System.Drawing;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;namespace MyControl 
    {
    public class MyFriend : WebControl, INamingContainer
    {
    public  MyFriend()
    { }
    private TextBox txt; protected override void Render(HtmlTextWriter writer)
    {
    if(!this.Visible)
    {
    return;
    }
    base.Render (writer); } protected override void CreateChildControls()
    {
    if(!this.Visible)
    {
    return;
    } this.Controls.Clear();
    this.ClearChildViewState();

    Table myTable  = new Table();
    TableRow row  = new TableRow(); TableCell myCell = new TableCell(); 
    TableCell myCell2 = new TableCell();  myCell.HorizontalAlign = HorizontalAlign.Left ;
    myCell.VerticalAlign = VerticalAlign.Middle; myCell2.HorizontalAlign = HorizontalAlign.Left ;
    myCell2.VerticalAlign = VerticalAlign.Middle;
    txt = new TextBox();
    txt.ID = "txt";
    myCell.Controls .Add (txt);
    row.Cells.Add(myCell);  LinkButton myLinkButton = new LinkButton();
    myLinkButton.ID = "myLinkButton";
    myLinkButton.Click += new EventHandler(myLinkButton_Click);
    myLinkButton.Text = "好友";
    myCell2.Controls.Add(myLinkButton);
    row.Cells.Add(myCell2); 


    myTable.Rows.Add(row);
    Controls.Add(myTable);
    }
    private void myLinkButton_Click(object sender,System.EventArgs e)
    {
    txt.Text = ((LinkButton)sender).Text+Guid.NewGuid().ToString ();
    } }
    }