【发布时间】:2015-05-29 03:40:18
【问题描述】:
我有一个包含三个列或字段的数据列表:
Column1:一些数据
Column2:链接按钮更新
Column3: div "divCheck",默认隐藏。
<asp:DataList ID="MyDataList" runat="server" EnableViewState="True">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<table width="100%">
<tr id="ItemRow" runat="server">
<td >
<%# DataBinder.Eval(Container.DataItem, "some_data")%>
</td>
<td >
<asp:LinkButton Text="Update" class="lnk_showCheck" CommandName="update" Runat="server" ID="update" />
</td>
<td >
<div id="check" style="display: none">Check</div>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
当链接按钮被点击时,它会调用一个jquery函数来显示一个div。
<head>
<script type="text/javascript">
jQuery(function ($) {
$('a.lnk_showCheck').click(function (e) {
e.preventDefault();
$('div',$(this).closest("td").next()).show();
});
});
</script>
</head>
但是,jquery调用后,并没有触发这个服务端函数:
Protected Sub MyDataList_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles MyDataList.ItemCommand
If e.CommandName = "update" Then
' Put some code here
' It doesn't trigger after LinkButton calls jquery function.
End If
End Sub
为什么?我应该如何调用这个服务器端函数?
编辑:
我正在尝试从服务器端调用 jquery 函数 ShowCheck,但我不知道如何使它像 ('a.lnk_showCheck').click(function
<script type="text/javascript">
jQuery(function ($) {
$('a.lnk_showCheck').click(function (e) {
//e.preventDefault();
$('div', $(this).closest("td").next()).show();
});
});
</script>
<script type="text/javascript">
function ShowCheck() {
$('div', $(this).closest("td").next()).show();
};
</script>
服务器端函数调用jquery函数:
Protected Sub MyDataList_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles MyDataList.ItemCommand
If e.CommandName = "update" Then
Page.ClientScript.RegisterStartupScript(Me.[GetType](), "show", " ShowCheck();", True)
End If
End Sub
调用 ShowCheck jquery 函数时出错。
错误:无法获取属性“toLowerCase”,因为它未定义。
【问题讨论】:
-
e.preventDefault();将停止默认操作的发生......在这种情况下,回发以运行您的事件处理程序。 -
是的,但是如果我不输入
codee.preventDefault();code,那么它会回发并且不会显示div。 -
为了到达服务器,你必须回发。它是事件处理程序运行的唯一方式。可能您的答案是您需要测试回发并处理您的 div 服务器端。
-
我正在尝试其他角度:在服务器端函数完成后调用 jquery 函数。但是,我不知道如何正确构造jquery函数。
-
此外,您可以加载要调用 in 事件处理程序的 javascript,并在页面回发后运行它。使用类似RegisterStartupScript