问题描述
我需要动态创建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"; }
这篇关于如何处理动态创建图像按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!