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

问题描述

我需要动态创建Image Button,并且必须生成click事件..但是当点击图像按钮时,click事件不会生成并隐藏Image按钮..我的代码中有什么问题..



protected void Button1_Click(object sender,EventArgs e)
{
DBConnections.GetCon obj = new DBConnections.GetCon();

MySqlConnection con = new MySqlConnection(obj.getConStr());
con.Open();
MySqlCommand cmd = new MySqlCommand(SELECT * FROM< tabel>,con);
MySqlDataReader dr = cmd.ExecuteReader();

while(dr.Read())
{
imgBtn = new ImageButton();
//图片imgBtn = new Image();


imgBtn.ID = dr.GetInt32(1).ToString();
if(dr.GetInt32(2)== 0)
{
imgBtn.ImageUrl =〜/ Users / SeatImg / 1.jpg;
}
其他
{
imgBtn.ImageUrl =〜/ Users / SeatImg / 3.jpg;
}



imgBtn.Click + = new ImageClickEventHandler(img_Click);
this.Panel2.Controls.Add(imgBtn);
}
con.Close();

}

protected void img_Click(object sender,ImageClickEventArgs e)
{
imgBtn.PostBackUrl =www.google.com;
}

解决方案


I need to create Image Button dynamically, and have to generate click event.. But while clicking on image button, the click event is not generating and hiding the Image button.. What is the problem in my code..

This is my code..

 ImageButton imgBtn;

protected void Button1_Click(object sender, EventArgs e)
        {
            DBConnections.GetCon  obj= new DBConnections.GetCon();

            MySqlConnection con = new MySqlConnection(obj.getConStr());
            con.Open();
            MySqlCommand cmd = new MySqlCommand("SELECT * FROM <tabel>",con);
            MySqlDataReader dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                imgBtn = new ImageButton();
                //Image imgBtn = new Image();


                imgBtn.ID = dr.GetInt32(1).ToString();
                if (dr.GetInt32(2) == 0)
                {
                    imgBtn.ImageUrl = "~/Users/SeatImg/1.jpg";
                }
                else
                {
                    imgBtn.ImageUrl = "~/Users/SeatImg/3.jpg";
                }



                imgBtn.Click += new ImageClickEventHandler(img_Click);
                this.Panel2.Controls.Add(imgBtn);
            }
            con.Close();

        }

        protected void img_Click(object sender, ImageClickEventArgs e)
        {
            imgBtn.PostBackUrl = "www.google.com";
        }
解决方案


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

07-29 16:43