ASP.NET TreeView: PostBack after Checked Changed?

2019-06-28 05:16发布

How to automatically post back to the server when a TreeNode check box has been checked in a TreeView control ??

2条回答
我命由我不由天
2楼-- · 2019-06-28 05:54

To make things modernized a bit using jQuery:

<asp:UpdatePanel runat="server">
  <ContentTemplate>
    <asp:TreeView ID="TVP" runat="server">
		...
    </asp:TreeView>
    <asp:Button ID="BTVPNC" runat="server" style="display: none;" />
  </ContentTemplate>
</asp:UpdatePanel>

And VB.NET code behind:

Private Sub TVP_PreRender(sender As Object, e As EventArgs) Handles TVP.PreRender
    Dim js As String = "$('#" + Me.TVP.ClientID + "').find('input[type=""checkbox""]')"
    js += ".bind('change',function(){" + Me.ClientScript.GetPostBackEventReference(Me.BTVPNC, "") + ";});"
    ScriptManager.RegisterStartupScript(Me.TVP, Me.TVP.GetType(), "TVP_NodeCheckAutoPostBack", js, True)
End Sub

Private Sub TVP_TreeNodeCheckChanged(sender As Object, e As TreeNodeEventArgs) Handles TVP.TreeNodeCheckChanged
	'Handle your TreeView NodeChecked with AutoPostBack event here.
End Sub

查看更多
孤傲高冷的网名
3楼-- · 2019-06-28 06:05

sorry, there is no autopostback

you can add this behaviour with simple javascript

you should check this article http://forums.asp.net/p/1109288/1888180.aspx

<script language="javascript" type="text/javascript">

    function postBackByObject()
    {
        var o = window.event.srcElement;
        if (o.tagName == "INPUT" && o.type == "checkbox")
        {
           __doPostBack("","");
        }
    }
</script>
查看更多
登录 后发表回答