单击div后执行代码 [英] Executing code behind on clicking div

查看:85
本文介绍了单击div后执行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张相册(不是图库),用它预览照片。根据与用户关联的专辑数量动态生成专辑。我想在其上添加点击事件,以便当一个人点击一个专辑时,它会将他带到该专辑中的照片。我是初学者所以我不知道如何做到这一点。但我认为添加命令参数应该可以解决问题。



ASPX代码:



I am have a photo album(not gallery) which previews the photos withing it. The albums are dynamically generated depending on number of albums associated with the user. I want to add on click event to it so that when a person clicks an album it takes him to the photos within that album. I am beginner so I do not exactly know how it can be done. But I think adding command arguments should do the trick.

ASPX CODE:

<%
for (int i = 0; i < dt5.Rows.Count; i++)
{
    string q = "select  imageurl from photos where albumid='" 
                                        + dt5.Rows[i]["albumid"].ToString() + "'";
    dt6 = dbo.Getdt(q);
    a = dt6.Rows[0]["imageurl"].ToString();
    b = dt6.Rows[1]["imageurl"].ToString();
    c = dt6.Rows[2]["imageurl"].ToString();
    a= a.Substring(1, a.Length - 1);
    b = b.Substring(1, b.Length - 1);
    c = c.Substring(1, c.Length - 1);
  %> 
   <div   class="image_stack" style="margin-removed300px ; removed 418px;"   

         runat="server">
       <img   class="stackphotos photo1" src="<%: a %>"   />
       <img   class="stackphotos photo2" src="<%: b %>"  />
       <img "   class="stackphotos photo3" src="<%: c %>" />
   </div>
     <br /><br /><br /><br />
<% } %>

推荐答案





尝试如下。



添加div标签的点击事件。

Hi,

try like below.

Add on click event for div tag.
<div class="image_stack" id="divAlbum" style="margin-removed300px ; removed 418px;">
onclick="return div_Click(this)" style="cursor:pointer;" runat="server">
    <img class="stackphotos photo1" src="<%: a %>" />
    <img class="stackphotos photo2" src="<%: b %>" />
    <img class="stackphotos photo3" src="<%: c %>" />
</img></div>





从代码后面添加属性。即将相册ID作为属性从代码添加如下。



Add attributes from code behind. i.e add album ID as attribute from code as below.

divAlbum.Attributes.Add("ID", 10); // here 10 is just as example, you specify your ID



上面的代码行必须写在你将图像绑定到div标签的地方。



写下javascript函数如下。


The above line of code, must be written where your binding the images to the div tag.

write javascript function like below.

function div_Click(sender)
{
    __doPostBack(sender.attributes["ID"].value, "div"); // target = album ID, argument = "div" - you can give any value to identify the event
    return true;
}





捕获页面加载事件中的参数,如下所示。





capture the argument in page load event as below.

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.Params.Get("__EVENTARGUMENT").ToString() == "div")
    {
        // user clicked on div. 
        if (Request.Params.Get("__EVENTTARGET").ToString() != "")
        {
            // user clicked on div. do your coding here
            int AlbumID = Convert.ToInt32(Request.Params.Get("__EVENTTARGET").ToString());  //album ID
        }
    }
}





当用户点击div区域内的任何地方时,这将捕获事件



hope它有帮助



this will capture the event when user clicks anywhere within the div area

hope it helps


这篇关于单击div后执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆