加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 我可以通过div onclick事件调用函数后面的代码吗?

发布时间:2020-12-15 22:36:15 所属栏目:asp.Net 来源:网络整理
导读:我的页面上有一堆div,它们是在运行时动态添加的. 当点击任何动态添加的div时 – 所有需要在后面的代码中调用相同的函数.每个div必须将自己的ID传递给函数. 我无法使用Web方法,因为函数需要识别单击哪个div,然后显示/隐藏/填充页面上的其他控件. 干杯伙计们
我的页面上有一堆div,它们是在运行时动态添加的.
当点击任何动态添加的div时 – 所有需要在后面的代码中调用相同的函数.每个div必须将自己的ID传递给函数.
我无法使用Web方法,因为函数需要识别单击哪个div,然后显示/隐藏/填充页面上的其他控件.

干杯伙计们

标题控件和东西都在这里

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

然后在后面的代码中:

Sub EditDiv(ID_ofDiv As String)

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

End Sub

解决方法

我不习惯编写VB代码,所以我的例子是在C#中,但也许它可以帮助你开始.
它可能不是实现这个的最干净的方法,但我会试一试:

HTML

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

客户

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

服务器

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.
   }
}

有关这方面的更多信息,请访问:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读