请问如何为动态生成的ImageButton添加Click事件? 10
我的代码是这样的:protectedvoidPage_Load(objectsender,EventArgse){if(!IsPostBack){image();}}pu...
我的代码是这样的:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
image();
}
}
public void image()
{
Class c = new Class();
string sql = "select * from Image";
DataSet ds = c.search(sql);
//将文件保存到文件夹,然后在读取
int i;
for (i = 0; i < ds.Tables[0].Rows.Count; i++)
{
ImageButton imgbtn= new ImageButton();
imgbtn.ID = i.ToString();
imgbtn.Width = Unit .Parse ("185px");
imgbtn.Height = Unit.Parse("100px");
imgbtn.ImageUrl = ds.Tables[0].Rows[i]["PathName"].ToString();
imgbtn.Click += new ImageClickEventHandler(imgbtn_Click);
this.div.Controls.Add(imgbtn);
}
while (i <5)
{
ImageButton imgbtn = new ImageButton();
imgbtn.ID = i.ToString();
imgbtn.Width = Unit.Parse("185px");
imgbtn.Height = Unit.Parse("100px");
imgbtn.ImageUrl = "Images/招租.gif";
this.div.Controls.Add(imgbtn);
i++;
}
}
protected void imgbtn_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
ImageButton ib = (ImageButton)sender;
Class c = new Class();
string sql = "select * from Image where PathName='" + ib.ImageUrl.ToString () + "'";
DataSet ds = c.search(sql);
Response.Redirect(ds.Tables[0].Rows[0]["Name"].ToString());
} 展开
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
image();
}
}
public void image()
{
Class c = new Class();
string sql = "select * from Image";
DataSet ds = c.search(sql);
//将文件保存到文件夹,然后在读取
int i;
for (i = 0; i < ds.Tables[0].Rows.Count; i++)
{
ImageButton imgbtn= new ImageButton();
imgbtn.ID = i.ToString();
imgbtn.Width = Unit .Parse ("185px");
imgbtn.Height = Unit.Parse("100px");
imgbtn.ImageUrl = ds.Tables[0].Rows[i]["PathName"].ToString();
imgbtn.Click += new ImageClickEventHandler(imgbtn_Click);
this.div.Controls.Add(imgbtn);
}
while (i <5)
{
ImageButton imgbtn = new ImageButton();
imgbtn.ID = i.ToString();
imgbtn.Width = Unit.Parse("185px");
imgbtn.Height = Unit.Parse("100px");
imgbtn.ImageUrl = "Images/招租.gif";
this.div.Controls.Add(imgbtn);
i++;
}
}
protected void imgbtn_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
ImageButton ib = (ImageButton)sender;
Class c = new Class();
string sql = "select * from Image where PathName='" + ib.ImageUrl.ToString () + "'";
DataSet ds = c.search(sql);
Response.Redirect(ds.Tables[0].Rows[0]["Name"].ToString());
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询