0

I have a web page that uses the ASP.net Tree View web control. As the web page is currently programmed, when the user clicks on a node on the tree view, the page posts back and a C# function executes to handle the event.

Can I make my web page run some Javascript code and not post back when a tree view node is clicked?

Update: Suppose my page looks like the one below. How can I make it so that clicking on node_A is handled by Javascript client-side instead of C# server-side?

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication3.WebForm2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TreeView runat="server" ID="myTreeView">
    <Nodes>
        <asp:TreeNode Text="node_A" Value="1" />
        <asp:TreeNode Text="node_B" Value="2" />
        <asp:TreeNode Text="node_C" Value="3" />
    </Nodes>
    </asp:TreeView>
    </div>
    </form>
</body>
</html>

2 Answers 2

2

Bind a click event on your elements, get the event object and call preventDefault().

element.onclick = function(e) {
    var evt = window.event || e;
    evt.preventDefault();

    /* Your code */
}

jQuery shortcut

$("element").click(function(e){ e.preventDefault(); });
Sign up to request clarification or add additional context in comments.

Comments

0

Did you try:
EnableViewState="False"

or
can you remove the runat server ?

1 Comment

No, you cannot remove the runat="server"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.