如何从后面的代码向我的div添加click事件?

时间:2011-03-28 18:40:20

标签: c# asp.net html

如何从后面的代码中向我的div添加点击事件?

我正在点击要通过消息框欢迎的div,你想删除这个以及框内是或否?

全部来自背后的代码:

            while (reader.Read())
            {
                System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
                div.Attributes["class"] = "test";
        //div.Style["float"] = "left";

                div.ID = "test";
                Image img = new Image();
                img.ImageUrl = String.Format("{0}", reader.GetString(1));
                // this line needs to be represented in sql syntax
                //img.ImageUrl = "~/userdata/2/uploadedimage/batman-for-facebook.jpg";
                img.AlternateText = "Test image";

                div.Controls.Add(img);
                div.Controls.Add(ParseControl(String.Format("&nbsp&nbsp "+"{0}", reader.GetString(0))));
                div.Style["clear"] = "both";
                test1.Controls.Add(div);

            }

如果可以的话,你可以为它添加点击事件吗?

2 个答案:

答案 0 :(得分:8)

此链接应该总结: http://davidhayden.com/blog/dave/archive/2004/03/16/178.aspx

c#
_myButton.Attributes.Add("onclick", "return confirm_delete();");

javascript:
function confirm_delete()
{
  if (confirm("Are you sure you want to delete the custom search?")==true)
    return true;
  else
    return false;
}

答案 1 :(得分:2)

 div.Attributes.Add("onclick", DoSomething);