Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

c#

asp.net

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?

like image 896
G Gr Avatar asked Mar 28 '11 18:03

G Gr


People also ask

Can you add a click event to a div?

To add a click event listener on div tag using JavaScript, we can call the addEventListener method on the selected div. to add a div with a class. Then we write: const div = document.

How do you call a button click event from code behind?

Button event is always trigger when you click the button but if you want to raise your button click event manually from code then you can use Button. PerformClick() from your code. Suppose on click of your Button1 you want to trigger Button2 click event then Please refer this[^] MSDN example.

How do I display a div onclick event?

To display or hide a <div> by a <button> click, you can add the onclick event listener to the <button> element. The onclick listener for the button will have a function that will change the display attribute of the <div> from the default value (which is block ) to none .

How do you add a click event to a button with plain JavaScript?

To add the click event in React using plain JavaScript, you need to use addEventListener() to assign the click event to an element. Create one <button> element as ref props so that it can be accessed to trigger the click event.


2 Answers

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;
}
like image 93
CrazyDart Avatar answered Sep 25 '22 10:09

CrazyDart


 div.Attributes.Add("onclick", DoSomething);
like image 44
Ta01 Avatar answered Sep 23 '22 10:09

Ta01