3

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

3 回答 3

3

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

于 2012-05-15T08:56:05.860 回答
0

您可以从 javascript 创建回发。一种方法是制作一个按钮或链接按钮并向其添加点击事件。添加样式=“显示:无;” 并强制 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','')
}

可以在下面的文章中找到一个很好的解释。 使用 JavaScript 的 ASP.NET 回发

于 2012-05-15T09:20:54.463 回答
0

在@Tim Schmelter 发布的链接的帮助下,我尝试了以下操作。这太棒了:

标记:

<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
于 2012-05-15T09:03:44.260 回答