我的页面上有一堆在运行时动态添加的 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.
}
}
有关此内容的更多信息可以在此处找到:
关于asp.net - 我可以从 div onclick 事件调用代码隐藏函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10596944/