【问题标题】:Can I call a code behind function from a div onclick event?我可以从 div onclick 事件中调用函数背后的代码吗?
【发布时间】:2012-05-22 17:47:02
【问题描述】:

我的页面上有一堆在运行时动态添加的 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

【问题讨论】:

  • 对不起,我已经编辑了我的帖子,指出这些需要调用该函数的 div 是用户在运行时动态创建的。

标签: asp.net vb.net


【解决方案1】:

我不习惯编写 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/

【讨论】:

  • Cheers Rob,我的一个问题是用户可以动态添加这些 div,所以我无法预测页面加载时是否会有 1 个控件或 100 个控件!
  • 我再次更新了我的答案并根据您的需要进行了调整。
  • 感谢您提供此解决方案。我能够做类似的事情来获取 ListView 对象内行的选定索引。你摇滚!
  • 总是乐于帮助别人!
【解决方案2】:

在@Tim Schmelter 发布的link 的帮助下,我尝试关注。这太棒了:

标记:

<div id="div1" runat="server" style="height:100px;width:100px;">click me</div>

代码:

public class MyPage  
      Inherits System.Web.UI.Page
      Implements IPostBackEventHandler

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    div1.Attributes("onclick") = 
         Me.Page.ClientScript.GetPostBackEventReference(Me, "div1_clicki")
End Sub

Protected Sub Div1_Click()
    'do your stuff
    div1.InnerHtml="div1 clicked"
End Sub


'Private Members As IPostBackEventHandler
Public Sub RaisePostBackEvent1(ByVal eventArgument As String) Implements 
                    IPostBackEventHandler.RaisePostBackEvent
    If eventArgument = "div1_click" Then
            div1_click()
        End If
    End If
End Sub

【讨论】:

  • 当用户单击 div 内的子元素时似乎存在一个不幸的错误,导致事件失控 - 即使我使用的是 UpdatePanel 并且该事件从不触发,页面也会刷新.
【解决方案3】:

您可以从 javascript 创建回发。一种方法是制作一个按钮或链接按钮并向其添加点击事件。添加 Style="Display:none;" 并强制 Div 在按钮点击时回发。

<div id="div_Footer" class="HoverEdit" title="Click To Edit" runat="server" onclick="EditDiv(this)">
    Footer Controls and stuff go here
</div>
//Javascript
function EditDiv()
{
   // do your code
   __doPostBack('ButtonAID','')
}

可以在下面的文章中找到一个很好的解释。 ASP.NET postback with JavaScript

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-27
    • 2020-06-09
    • 2010-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 2011-03-21
    相关资源
    最近更新 更多