Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 动态图像按钮单击事件未被激发_C#_Asp.net_Events_Dynamic - Fatal编程技术网

C# 动态图像按钮单击事件未被激发

C# 动态图像按钮单击事件未被激发,c#,asp.net,events,dynamic,C#,Asp.net,Events,Dynamic,dynamic imagebutton单击事件未被解雇请提供帮助 我已经在asp.net oninit方法中创建了动态按钮 protected override void OnInit(EventArgs e) { base.OnInit(e); ImageButton img = new ImageButton(); img.ID = "first_button"; img.Click += new ImageClickEventHan

dynamic imagebutton单击事件未被解雇请提供帮助 我已经在asp.net oninit方法中创建了动态按钮

protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
 ImageButton img = new ImageButton();
        img.ID = "first_button";
        img.Click += new ImageClickEventHandler(first_Click);
        img.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "ClassLibrary1.image.first.gif");
        img.Attributes.Add("onmouseover", "onmousehand(this,'over')");
        img.Attributes.Add("onmouseout", "onmousehand(this,'out')");
        p1.Controls.Add(img);
        img.Dispose();
        img = new ImageButton();
        img.ID = "previous_button";
        img.Click += new ImageClickEventHandler(previous_Click);
        img.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "ClassLibrary1.image.previous.gif");
        img.Attributes.Add("onmouseover", "onmousehand(this,'over')");
        img.Attributes.Add("onmouseout", "onmousehand(this,'out')");
        p1.Controls.Add(img);
        img.Dispose();

        t1.Attributes.Add("style", "color:#666666;");
        // t1.Text = "Page " + current_page + " of " + total_pages;
        t1.ID = "text_box1";
        t1.Attributes.Add("onclick", "textbox_enable('" + t1.ClientID + "')");
        p1.Controls.Add(t1);
        img = new ImageButton();
        img.ID = "go_button";
        img.Click += new ImageClickEventHandler(go_Click);
        img.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "ClassLibrary1.image.go.GIF");
        img.Attributes.Add("onmouseover", "onmousehand(this,'over')");
        img.Attributes.Add("onmouseout", "onmousehand(this,'out')");
        p1.Controls.Add(img);
        img.Dispose();
        ImageButton img1 = new ImageButton();
        img1.ID = "next_button";
        img1.CommandName = "next_button";
        img1.CommandArgument = "next1";

        img1.Click += new ImageClickEventHandler(next_Click);
        img1.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "ClassLibrary1.image.next.gif");
        img1.Attributes.Add("onmouseover", "onmousehand(this,'over')");
        img1.Attributes.Add("onmouseout", "onmousehand(this,'out')");
        p1.Controls.Add(img1);
        //img.Dispose();
        img = new ImageButton();
        img.ID = "last_button";
        img.Click += new ImageClickEventHandler(last_Click);
        img.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "ClassLibrary1.image.last.gif");
        img.Attributes.Add("onmouseover", "onmousehand(this,'over')");
        img.Attributes.Add("onmouseout", "onmousehand(this,'out')");
        p1.Controls.Add(img);
        img.Dispose();    
   }
   private   void next_Click(object sender, ImageClickEventArgs e)
    {

        ImageButton next = (ImageButton)sender;
        string value = next.CommandArgument;
        current_page++;
        t1.Text = "Page " + current_page + "of" + total_pages;
    }
  protected override void Render(HtmlTextWriter writer)
    {

        t1.Text = "Page " + current_page + " of " + total_pages; 
        p1.RenderControl(writer);
        base.Render(writer);
    }
试试这个

protected void Page_Load(object sender, EventArgs e)
{
    ImageButton img = new ImageButton();
    img.ID = "SampleImage";
    img.Click += new ImageClickEventHandler(img_Click);
    this.form1.Controls.Add(img);
}

void img_Click(object sender, ImageClickEventArgs e)
{
    Response.Write("Hello World");
}

您可以在页面_Init中定义动态按钮功能,而不是Oninit

    protected void Page_Init(object sender, EventArgs e)
{
    ImageButton imagebutton = new ImageButton();
    imagebutton.ID = "myID";
    imagebutton.Attributes.Add("runat", "server");
    imagebutton.Click += new ImageClickEventHandler(image_Click);
    this.form1.Controls.Add(imagebutton);
}

void image_Click(object sender, ImageClickEventArgs e)
{
    //Your code here
}

它对我有效:)而且应该有效。您处理ImageButton的目的是什么?但是它没有转到名为why is it的click方法。那么代码中是否存在一些问题,我应该在默认设置中提供页面号。aspx page抱歉,我不明白您的意思:)