G Gr
G Gr

Reputation: 6090

How can I add a click event to my div from code behind?

How can I add a click event to my div from code behind?

Im looking upon clicking on the div to be greeted by a message box with would you like to delete this and a yes or no within the box?

All from the code behind:

            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);

            }

can you add a click event to it if so how?

Upvotes: 8

Views: 12501

Answers (2)

Ta01
Ta01

Reputation: 31630

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

Upvotes: 4

CrazyDart
CrazyDart

Reputation: 3801

This link should sum it up: 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;
}

Upvotes: 10

Related Questions