do not doDropDownList1.SelectedIndexChanged += new System.EventHandler(DropDownList1_SelectedIndexChanged);
 
in Render(), do it in Init or Load event handler

解决方案 »

  1.   

    SpringWang(白水书生),您的这句我加进去以后,页面是有刷新了,但事件还是没有触发。
      

  2.   

    protected override void void OnInit(EventArgs e)
    { DropDownList1=new DropDownList();
    for (int i=0;i<=9;i++)
    {
    DropDownList1.Items.Add(i.ToString());
    }

    DropDownList1.SelectedIndexChanged += new System.EventHandler(DropDownList1_SelectedIndexChanged);
                               DropDownList1.AutoPostBack=true; Controls.Add(DropDownList1);
    Label1=new Label();
    Label1.Text =DropDownList1.SelectedItem.Text;
    Controls.Add(Label1);}protected override void Render(HtmlTextWriter output)
    {
                              DropDownList1.RenderControl(output);
    Label1.RenderControl(output);
    }
      

  3.   

    综合思归和白水书生的留言,我改进了代码,好象可以了,请过目。
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;namespace WebControlLibrary1
    {
    [DefaultProperty("Text"), 
    ToolboxData("<{0}:DataControl2 runat=server></{0}:DataControl2>")]
    public class DataControl2 : System.Web.UI.WebControls.WebControl,INamingContainer
    {
    DropDownList DropDownList1;
    Label Label1;

    private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    Label1.Text =DropDownList1.SelectedItem.Text;

    }

    protected override void CreateChildControls() 
    {
    DropDownList1=new DropDownList();
    for (int i=0;i<=9;i++)
    {
    DropDownList1.Items.Add(i.ToString());
    }

    DropDownList1.SelectedIndexChanged += new System.EventHandler(DropDownList1_SelectedIndexChanged);

    Controls.Add(DropDownList1);
    Label1=new Label();
    Label1.Text =DropDownList1.SelectedItem.Text;
    Controls.Add(Label1);

    DropDownList1.AutoPostBack=true;

    }

    protected override void Render(HtmlTextWriter output)
    {


    DropDownList1.RenderControl(output);
    Label1.RenderControl(output);
    }
    }
    }
      

  4.   

    sorry,protected override void void OnInit(EventArgs e)==>
    protected override void OnInit(EventArgs e)
      

  5.   

    哈,思归大侠的方法和我写的这个很类似,都实现了,还想请问一下,重写CreateChildControls() 方法和重写Render有什么区别?