views:

992

answers:

3

I posted several times but no one would give me an answer that I can understand in plain English. I am new to JSON/JQuery/Ajax/all other cool libs or tools you may advise me on, so keep that in mind.

I am using c# with asp.net web forms (i also have ajax toolkit, but haven't used it yet).

Here is my scenario: I need to implement a functionality that is going to use TreeView control. A parent page will display a TreeView and a user will be able to click on the node, which will trigger a pop up, where a user will enter some info. The pop up will save the info to the database, return a result value to the parent window and the parent window should be updated to reflect the changes. So pretty common scenario from what I understand.

Now, my question is what is the simplest way to achieve this while keeping in mind that I have to minimize the number of trips between the client and a web server?

EDITED: I posted this several times and no one seems to have an answer to my question. This is fairly urgent, so please - if anyone has had any real dev experience with this scenario, share you ideas!

+1  A: 

I would recommend using jQuery with the jqModal plugin instead of using a ‘popup’.

Regardless this should be what you are looking for in JavaScript,

opener.document.[parent_form_ID].[parent_input_ID].value = [value to be passed to the parent];

You can add the above code to the save event of the child window to pass back the user entered data to the parent window.

Shaun Humphries
I think you misunderstood me. I seem to have a difficulty getting my point across. The pop-up window will save the data to the DATABASE - meaning it will use a SERVER aspx file to DO THAT. The stored procedure will return an ID that was generated while saving the record.
gnomixa
Keep in mind that this is a SERVER script. Now, I need to pass this id to the TREEVIEW on the parent page. In your example, value_to_be_passed needs to come from the SERVER aspx page. Hope this was clear and thanks for your help.
gnomixa
You could have the child return the ID client side and then pass it to the parent or have the child store the ID in session to be utilized by the parent.
Shaun Humphries
the session solution sounds a better one here.
gnomixa
is it really a good practice though?
gnomixa
I try to stay away from it when I can. I would clear the session variable once the ID is retrieved. I use jQuery and Ajax which eliminates the need to use session here.
Shaun Humphries
I am not opposed to ajax. In fact i will have to use it anyways, as form A contains a treeView which I am going to update with AjAX to avoid loading the whole tree. So knowing that, how would you approach this scenario?
gnomixa
so basically to give you a full picture, page A contains a TREEVIEW, when the user clicks on the node, the pop up will pop-up where the user will enter some info. Upon submitting the pop-up, it will close, and ONLY a specific node on TREEVIEW will be updated. Any tips?
gnomixa
As stated in my initial response I would use jQuery and jqModal to handle the popup. The modal is housed in the same window making it easier to update the treeview. Then save the user entered data using an Ajax callback and return the ID/update the treeview in a callback response handler.
Shaun Humphries
by a callback response handler do you mean server code inside the Page_Load() function? If(IsCallBack) {//update the tree}
gnomixa
Actually, i just googled AJAX callback and I assume that you mean ICallbackEventHandler.
gnomixa
A: 

Personally what you might want to do is simply upon the return of the value to the parent page trigger an async postback to update the tree view.

Essentially, put the tree view in a UpdatePanel, then have a hidden button to trigger, and call it from JS to force the update. It will keep the payload down, and most likely will be the minimum that you need to update.

Mitchel Sellers
A: 

I've put together an example that does what you want, I hope. It opens a client-side jQuery popup using SimpleModal (the popup itself still needs some work). When the popup opens, a textbox and a submit button are shown. On submit, an ASP.NET page method is called via Ajax. The page method receives the posted data and returns a random number (to show that it works). The random number is then shown in the tree.

First the aspx.cs file (without imports) and the class that is used as the page method return type:

public partial class _Default : Page
{
    [WebMethod]
    public static MethodResult SaveData(string nodeId, string value,
                                        string elementId)
    {
        return new MethodResult
               { ElementId = elementId, Result = new Random().Next(42) };
    }
}
public class MethodResult
{
    public string ElementId { get; set; }
    public int Result { get; set; }
}

The page method is very simple. It receives the node id so you know what to update in the database, the value from the textbox and the id of the UI element to update after returning. The method returns an instance of the MethodResult class that contains the id of the UI element to update and the actual result (a random number).

Next we have the aspx file that has some more lines of code. It has a lot of comments so I hope everything is clear.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SO_736356._Default" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Test</title>
<script type="text/javascript" src="Scripts/jquery-1.3.2.js" ></script>
<script type="text/javascript" src="Scripts/jquery.simplemodal-1.2.3.js" ></script>
<script type="text/javascript">
    $(document).ready(function() {
        // Select all a elements with an id that starts with 'treet' (all
        // nodes in the tree have an id that starts with treet: treet0,
        // treet1, ...). Add an onclick handler to all these elements.
        $("a[id^='treet']").click(function() {
            // In an asp:TreeView the href element of each node contains
            // the node value so we parse the href element to obtain this
            // value.
            var href = $(this).attr("href");
            $("#hiddenNodeId").val(href.substring(href.lastIndexOf("\\\\") + 2, href.indexOf("')")));
            // We need to remember the id of the element we clicked on,
            // otherwise we don't what element to update when the page
            // method call returns.
            $("#hiddenElementId").val($(this).attr("id"));
            // Show the popup.
            $("#popup").modal();
            // Return false to cancel the onclick handler that was already
            // on the a element (view source).
            return false;
        });

        // The spanSubmit element is our postback link so when we click it
        // we perform an AJAX call to the page method (Default.aspx/SaveData).
        $("#spanSubmit").css("cursor", "hand").click(function() {
            var nodeId = $("#hiddenNodeId").val();
            var input = $("#txtInput").val();
            var elementId = $("#hiddenElementId").val();
            $.ajax({
                type: "POST",
                url: "Default.aspx/SaveData",
                // The parameter names must match exactly.
                data: "{nodeId: \"" + nodeId + "\", value: \"" +
                        input + "\", elementId: \"" + elementId + "\"}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(result) {
                    // The property result.d contains our result. We select
                    // the right element and set its text to another value.
                    $("#" + result.d.ElementId).text(
                        "Random number " + result.d.Result);
                }
            });
        });
    });
</script>
</head>
<body>
<form id="form" runat="server">
<div>
    <%-- The actual tree, no fancy stuff here. --%>
    <asp:TreeView ID="tree" runat="server">
        <Nodes>
            <asp:TreeNode Value="0" Text="Node_0" >
                <asp:TreeNode Value="0_0" Text="Node_0_0">
                    <asp:TreeNode Value="0_0_0" Text="Node_0_0_0" />
                    <asp:TreeNode Value="0_0_1" Text="Node_0_0_1" />
                </asp:TreeNode>
                <asp:TreeNode Value="0_1" Text="Node_0_1">
                    <asp:TreeNode Value="0_1_0" Text="Node_0_1_0" />
                </asp:TreeNode>
                <asp:TreeNode Value="0_2" Text="Node_0_2">
                    <asp:TreeNode Value="0_2_0" Text="Node_0_2_0" />
                </asp:TreeNode>
            </asp:TreeNode>
            <asp:TreeNode Value="1" Text="Node_1">
                <asp:TreeNode Value="1_0" Text="Node_1_0">
                    <asp:TreeNode Value="1_0_0" Text="Node_1_0_0" />
                    <asp:TreeNode Value="1_0_1" Text="Node_1_0_1" />
                </asp:TreeNode>
            </asp:TreeNode>
        </Nodes>
    </asp:TreeView>
</div>

<%-- The popup with the hidden fields that are set when we click a node
     and the textbox and submit button. --%>
<div id="popup" style="display: none;">
    <input id="hiddenNodeId" type="hidden" />
    <input id="hiddenElementId" type="hidden" />
    <label id="lblInput" for="txtInput">Input:</label><input id="txtInput" type="text" />
    <br />
    <span id="spanSubmit">Save</span>
</div>
</form>
</body>
</html>
Ronald Wildenberg