点击事件上的div asp.net事件、div、net、asp

2023-09-03 04:27:46 作者:猪是的念着倒

我在Default.aspx的DIV与IMG内部如下

I have a div in default.aspx with img inside as follow

 <div id="sTab" class="tabs firsttab" runat="server">
     <asp:Image ID="sTabImg"  src="images/home.png" runat="server" />
 Activities
</div>

我想在ASP.NET对点击的div执行一些动作(在JavaScript)。

I want to perform some action on click of the div in ASP.NET (not in javascript).

我尝试了以下

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
    }

    sTabImg.Attributes["onclick"] = ClientScript.GetPostBackEventReference(this, "ClickDiv");
}

protected void ClickDiv()
{
    Label2.Text = "helo got it";
}

页面越来越刷新,当我调试的问题,它不是进入ClickDiv function..What错在这里。

The page is getting refreshed and when i debugged the issue, its not entering the ClickDiv function..What wrong here..

推荐答案

为什么不使用的的ImageButton ?

<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>ImageButton Sample</title>
<script language="C#" runat="server">

      void ImageButton_Click(object sender, ImageClickEventArgs e) 
      {
         Label1.Text = "You clicked the ImageButton control at the coordinates: (" + 
                       e.X.ToString() + ", " + e.Y.ToString() + ")";
      }

</script>

</head>
<body>
<form id="form1" runat="server">

  <h3>ImageButton Sample</h3>

  Click anywhere on the image.<br /><br />

  <asp:ImageButton id="imagebutton1" runat="server"
       AlternateText="ImageButton 1"
       ImageAlign="left"
       ImageUrl="images/pict.jpg"
       OnClick="ImageButton_Click"/>

      <br /><br />

      <asp:label id="Label1" runat="server"/>

   </form>

</body>
</html>

你可以看到一些例子这里。