我可以调用函数的背后,从一个div onclick事件一code? [英] Can I call a code behind function from a div onclick event?

查看:140
本文介绍了我可以调用函数的背后,从一个div onclick事件一code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆在运行时动态添加我的网页上div的。
当任何动态添加的div被点击 - 都需要背后调用同一个函数在我的code。每个div的必须通过其自己的ID的功能。
作为函数需要确定单击了哪个分区,然后显示/隐藏/填充其他控件的页面上,我不能使用网络的方法。

干杯家伙


            头控制和东西放在这里
         

 < D​​IV ID =div_Footer级=HoverEdit称号=点击编辑=服务器的onclick =EditDiv(div_Footer)>
        页脚控制和东西放在这里
    < / DIV>

,然后在后面的code:

 子EditDiv(ID_ofDiv作为字符串)    做一些东西到控件在页面上
    交换选项卡,显示/隐藏控件等。结束小组


解决方案

我不习惯写VB code,所以我的例子是在C#中,但也许它可以帮助您开始。
它可能不是实现这个最干净的方式,但我会试试看:

HTML

 < D​​IV ID =div_Footer级=HoverEdit称号=点击编辑=服务器的onclick =EditDiv(本)>
        页脚控制和东西放在这里
    < / DIV>

客户端

 <脚本类型=文/ JavaScript的>
功能EditDiv(S,E){
VAR ID = $(S).attr(ID);
__doPostBack(ID,ID);
}
< / SCRIPT>

服务器

 私人无效的Page_Load(对象发件人,EventArgs的发送)
{
   VAR ARG =的Request.Form [__ EVENTTARGET]; 这将是你的第一个页面请求空的,但如果用户点击一个div就会造成回发到服务器,因此该事件将被再次触发,将包含DIV ID。   如果(ARG!= NULL)
   {
      字符串DIVID =(字符串)ARG;
      调用带参数的方法。
   }
}

关于这方面更多的信息可以在这里找到:

http://wiki.asp.net/page.aspx/1082 / dopostback功能/

I have a bunch of divs on my page that are added at run time dynamically. When any of the dynamically added divs are clicked - All need to call the same function in my code behind. Each of the divs must pass it's own ID to the function. I can't use web methods as the function needs to identify which div was clicked and then show/hide/populate other controls on the page.

Cheers guys

Header Controls and stuff go here

    <div id="div_Footer" class="HoverEdit" title="Click To Edit" runat="server" onclick="EditDiv(div_Footer)">
        Footer Controls and stuff go here
    </div>

and then in the code behind :

Sub EditDiv(ID_ofDiv As String)

    'Do some stuff to the controls on the page
    'Swapping tabs, showing /hiding controls etc.

End Sub

解决方案

I'm not used to write VB code so my example is in C# but maybe it can help you get started. It's probably not the cleanest way to implement this, but i'll give it a try:

HTML

 <div id="div_Footer" class="HoverEdit" title="Click To Edit" runat="server" onclick="EditDiv(this)">
        Footer Controls and stuff go here
    </div>

Client

<script type="text/javascript">
function EditDiv(s,e){
var id = $(s).attr("id");
__doPostBack(id,id);
}
</script>

Server

private void Page_Load(object sender, EventArgs e)
{
   var arg = Request.Form["__EVENTTARGET"]; 'this will be empty on your first page request, but if the user click a div it will cause a postback to server, so this event will be fired again and will contain the div ID.

   if(arg != null)
   {
      string divID = (string)arg;
      'call your method with the argument.
   }
}

More information about this can be found here:

http://wiki.asp.net/page.aspx/1082/dopostback-function/

这篇关于我可以调用函数的背后,从一个div onclick事件一code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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