我有一种方法可以创建一些图像按钮。我在下面添加了一行: btnCity.Click += new ImageClickEventHandler(btnCity_Click); 到对点击事件进行编码的方法。
在@Jacek 的请求中添加添加按钮的代码:
private void LocateCities()
{
IDBManager dbManager = new DBManager(DataProvider.SqlServer);
dbManager.ConnectionString = @"Data Source=server; Initial Catalog=db; Integrated Security = SSPI;";
try
{
dbManager.Open();
dbManager.CreateParameters(2);
dbManager.AddParameters(0, "@Function", "All");
dbManager.AddParameters(1, "@Team", "All");
DataSet ds = new DataSet("Stuff");
ds = dbManager.ExecuteDataSet(CommandType.StoredProcedure, "sp_select_staff_and_cities");
foreach (DataRow dr in ds.Tables[0].Rows)
{
int xaxis = Convert.ToInt32(dr["xaxis"]) ;
int yaxis = Convert.ToInt32(dr["yaxis"]) ;
int textxaxis = xaxis + 30;
int textyaxis = yaxis - 10;
ImageButton btnCity = new ImageButton();
btnCity.ImageUrl = "~/Images/cyanball1.gif";
btnCity.Height = 10;
btnCity.Attributes.Add("style", "Z-INDEX:100; POSITION:relative; left:" + xaxis + "px; TOP:" + yaxis + "px; Left:10px;Right:10px");
Label lblCity = new Label();
lblCity.Text = dr["city"].ToString();
lblCity.Attributes.Add("style", "Z-INDEX: 100;POSITION:relative; left:" + textxaxis + "px; TOP:" + textyaxis + "px");
PanelMap.Controls.Add(lblCity);
PanelMap.Controls.Add(btnCity);
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
dbManager.Dispose();
}
}
我还创建了以下方法来处理点击事件:
void btnCity_Click(object sender, ImageClickEventArgs e)
{
ImageButton btnCity = (ImageButton)sender;
Response.Write("works");
}
但它不起作用。有什么建议吗?