如何处理动态图像按钮单击事件. [英] how to handle dynamic image button click event.?

查看:106
本文介绍了如何处理动态图像按钮单击事件.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会在单击按钮时动态创建图像按钮.我已经处理了它的click事件,并在其中进行了一些编码.但是问题在于单击图像按钮时,图像按钮的click事件方法未调用.所以请告诉我如何处理.

谢谢你.

=========
从JSOP:不要在邮件中使用大写字母.

代码在这里:

I''m creating image buttons dynamically on button click. I have handled its click event and done some coding in it. But the problem is that on clicking on image button the click event method of image button is not calling. So please tell me how to handle it.

Thank u.

=========
From JSOP: Don''t use all-caps in your messages.

code is here:

        con.Open();
        da = new SqlDataAdapter(str, con);
        da.Fill(ds);
        PlaceHolder1.Controls.Clear();
        Table tbl = new Table();
        PlaceHolder1.Controls.Add(tbl);

        for ( i = 0; i < 5; i++)
        {
            TableRow tr = new TableRow();
            for (int j = 0; j < 2; j++)
            {
                TableCell tc = new TableCell();
                HyperLink h1 = new HyperLink();
                Image img = new Image();
                ImageButton imgbt = new ImageButton();
                if (j == 0)
                {
                    imgbt.ImageUrl = ds.Tables[0].Rows[i]["picture"].ToString();
                    imgbt.Height = 50;
                    imgbt.Width = 50;
                      imgbt.Click += new ImageClickEventHandler(imgbt_Click);

                   ds.Tables[0].Rows[i]["picture"].ToString();

                   tc.Controls.Add(imgbt);
                }
                else
                {
                    h1.Text = ds.Tables[0].Rows[i]["Name"].ToString();
                    // Add the control to the TableCell
                    tc.Controls.Add(h1);
                }

                h1.ForeColor = System.Drawing.Color.Black;
                h1.NavigateUrl = ds.Tables[0].Rows[i]["PageUrl"].ToString();

                // Add the TableCell to the TableRow
                tr.Cells.Add(tc);
            }

            // Add the TableRow to the Table
            tbl.Rows.Add(tr);
        }
    }
    catch { }
}

void imgbt_Click(object sender, ImageClickEventArgs e)
{
   //throw new NotImplementedException();
    Response.Write("welcome");
    string str1 = "";
    string str2 = "''" + ds.Tables[0].Rows[i]["Name"] + "''";
    string str3 = "''" + ds.Tables[0].Rows[i]["Adress"] + "''";

    str1 = "''" + ds.Tables[0].Rows[i]["picture"] + "''";
    Page.ClientScript.RegisterStartupScript(this.GetType  (), "Test", "getpoint(" + ds.Tables[0].Rows[i]["Latitude"] + "," + ds.Tables[0].Rows[i]["Longitude"] + "," + str1 + "," + str2 + "," + str3 + ");", true);
}

推荐答案

您必须在每次回发中创建动态图片按钮.然后,只有您的事件会触发
You have to create your dynamic imagebutton on every postback.then only your event will fire


JSOP所说的关于发布代码的内容,只需验证您是否正在运行时甚至在回发时创建图像按钮并分配处理程序即可!

看起来,一旦回发发生,事件处理程序就不会再附加.
Before doing what JSOP has said about posting code, just verify that you are creating the image button at runtime even on postback and assigning the handler too!

Looks like, the event handler is not attached again once postback happened.


如果您不向我们展示您的代码,我们将无法为您提供帮助.
If you don''t show us your code, we can''t help you.


这篇关于如何处理动态图像按钮单击事件.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆