asp.net - 我可以从 div onclick 事件调用代码隐藏函数吗?

标签 asp.net vb.net

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

关于asp.net - 我可以从 div onclick 事件调用代码隐藏函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10596944/

相关文章:

vb.net - 你如何确定一个字符是否是来自 A-Z 的字母?

c# - 我如何知道我的电子邮件是否已被接收并使用 Asp.net 阅读

c# - 如果 XValueType ="Time"和系列包含跨越多天的日期时间,则 ASP 图表格式不正确

c# - 在 ASP.NET 中的几个页面之间共享通用功能的方法

asp.net - 如果提交不应推送到存储库的文件夹,.hgignore 文件是否有效?

c# - 没有参数的 Lambda 在 VB 中有效,但在 C# 中无效

asp.net - 在 Asp.Net 中,如何确定哪个验证器控件已将 Page.IsValid 设置为 false?

asp.net - CheckBoxList ListItem 正在保存错误的值

vb.net - 生成带有字母表前缀的数字序列

vb.net - Visual Basic 中的数组