我想知道是否可以在linkbutton的postbackurl中运行javascript。或者有没有办法在linkbutton中触发javascript。
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="editlink" onclick="LinkButton1_Click" PostBackUrl="javascript:document.getElementById('sched').style.display = 'block';" >Edit</asp:LinkButton>
答案 0 :(得分:1)
您可以使用OnClientClick
。例如:
<asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="MyFunction();" onclick="LinkButton1_Click">LinkButton</asp:LinkButton>
它将在OnClick事件之前触发。
您的Javascript功能:
<script type="text/javascript">
function MyFunction() {
document.getElementById('sched').style.display = 'block';
}
</script>
为AsyncPostBack添加UpdatePanel
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="LinkButton1" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="MyFunction();" onclick="LinkButton1_Click">LinkButton</asp:LinkButton>
<asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1">
<ProgressTemplate>
<div id="sched">
// Your code
</div>
</ProgressTemplate>
</asp:UpdateProgress>
</ContentTemplate>