views:

1961

answers:

2

How do I access Jquery Treeview inside the .aspx file upon form submit? also, Can I add nodes to Jquery Treeview on the fly? (on the client side)

I am using asp.net web forms, c#

EDITED: someone mentioned the following in one of the questions: "on form submit, someone is going to have to write code on the client to collect that data and send it via Ajax to a server method"

how is this done?????

+1  A: 

Since jQuery Treeview is a client-side component, you can't access it from the server-side. So in order to pass any data from your tree to the server, you have to write client-side javascript code (actually jQuery code). The same thing regarding adding nodes to the treeview on the fly: only using client-side jQuery code. Remember that your C# on the server-side has no idea about your TreeView.

The integration between jQuery and ASP.NET WebForms is rather problematic and "not so natural", because ASP.NET is built on different concept... So if you are working with ASP.NET WebForms, I would suggest you to use server-side components instead (it can be Microsoft's own ASP:TreeView or other 3rd-party WebForms-aimed components). Alternatively, you can try the new ASP.NET MVC Framework - it is built on more common (for other web-development platforms) concept, and the integration between it and jQuery is straightforward (actually jQuery is even shipped with it).

Don't get me wrong... I am not saying that the integration between jQuery and ASP.NET WebForms is totally impossible. It is possible. But you'll need to do "not-so-beautiful" things and work hard for every simple operation. If you still want to use jQuery, then use it only for the client-side animations...

UPDATE: As for this quote - "on form submit, someone is going to have to write code on the client to collect that data and send it via Ajax to a server method" - well, this is exactly what I am talking about. On the client-side you call javascript method when submitting the form (for example, by setting onclick='mymethod();' on your "Submit" button). This code does what it needs to do and then... it is supposed to perform AJAX call using jQuery nice syntax. But this won't work with ASP.NET WebForms, as I've explained before. Well, you can read about Microsoft AJAX Client-side library (here: http://msdn.microsoft.com/en-us/magazine/cc163300.aspx), perhaps this will help. But I still think that this integration won't be easy and straightforward. Use jQuery for the animation and ASP.NET server-side components for all other things!

Dmitry Perets
thanks Dmitry, I am not stuck on using jQuery treeview. I just need a pure client based treeview. All I need is a treeview on the client side, then Save button will trigger a callback which will collect the treeview data and send it to the server.
gnomixa
Of course I could implement my own treeview but at this day and age I shouldn't have to.
gnomixa
From what I understand, as long as I can access the tree on the client, I should be able to use MS callback (my page will implement System.Web.UI.ICallbackEventHandler interface) to consume the tree and pass the data to the server, JSON format. I realize that using Jquery ajax wont work with .net
gnomixa
Well, if your treeview should be on client-side, then perhaps you do want to use jQuery for it, just because it is nicer and simpler to use. But you understand correctly: you won't be able to use its ajax functionality. You'll need MS AJAX Client-side library (which I am not so familiar with it =( )
Dmitry Perets
So essentially, to send the tree information to the server method, I need to use ASP.NET AJAX library?
gnomixa
I don't think that you are the only one who is not so familiar with Microsoft AJAX Library. I could only find one link (the one you published) on google. Other links all led to some book that was published in 2007. I am not even sure who uses this library.
gnomixa
Out of the links available, I really don't get enough info for my case - it is so poorly explained using the simplest examples. How do i send tree data (which could be large) over to the asp.net server method???? that's all i want to know. I have been trying to find info on this for over a week.
gnomixa
+2  A: 

Well, I think I've found what you want. Take a look here.

In brief, you must define a WebMethod on your server side, and then you can easily access it using jQuery. An exellent working example is under the link above, and here I'll modify it to show how you can pass arguments. So...

In your page code-behind *.cs:

// We'll use this class to communicate
public class Dog
{
    public string Name { get; set; }
    public string Color { get; set; }
}

// This is your page, in my case Default.aspx
public partial class _Default : System.Web.UI.Page
{
    [WebMethod]
    public static string ProcessData(Dog myDog)
    {
        return "Your " + myDog.Color + " dog's name is " + myDog.Name + "!";
    }
}

Then on your *.aspx:

<script src="jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="json2.js" type="text/javascript"></script>

<script type="text/javascript">

    $(document).ready(function() {
        $("#btnProcess").click(function() {
            // This is what we want to send to the server
            var dogItem =
            {
                 Color: $("#txtColor").val(),
                 Name: $("#txtName").val()
            };

            // And this is how it should be sent - in serialized way
            var dogItemSerialized = JSON.stringify(dogItem);

            $.ajax({
                type: "POST",
                url: "Default.aspx/ProcessData",
                data: "{'myDog':" + dogItemSerialized + "}",    
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    $("#result").text(msg.d);
                }
            });
        });
    });

</script>
Color: <input id="txtColor" type="text" /><br />
Name: <input id="txtName" type="text" /><br />
<input id="btnProcess" type="button" value="Click Me" />
<div id="result"></div>

So here you fill textboxes and then your data is sent to the server which understands it as a Dog object. Pay attention to arguments passing, because this is the most confusing part. You should pass them in JSON format, which is a little bit "too-much-stringy". So I use here json2.js script which helps to convert usual javascript object into JSON-serialized string (JSON.stringify() method). It is available here. But it is still rather ugly =) It is important that I pass argument called "myDog" which value is the serialized dogItem. Because this is exactly what the server expects to get (so, for example, I can't change the argument name, this won't work:

data: "{'someAnotherArgumentName':" + dogItemSerialized + "}"

And the last thing. Pay attention to the following line:

success: function(msg) {
            $("#result").text(msg.d);
         }

If you are working with ASP.NET prior to 3.5 (for example, ASP.NET 2.0), then you'll need to write just $("#result").text(msg) instead of msg.d. Only ASP.NET 3.5 encapsulates all the data under "d" member for some reason...

Anyway, in the above article you can find useful links (both inside the article and in comments), so you can read more about arguments, "msg.d" and so on.

I hope this helps!

Dmitry Perets