开发者

UpdatePanel resetting object to inital state

开发者 https://www.devze.com 2023-02-03 21:33 出处:网络
I have an application that I am currently writing that works by iterating through nodes, and then updating the page with the information of the current node. I have an UpdatePanel in the page which co

I have an application that I am currently writing that works by iterating through nodes, and then updating the page with the information of the current node. I have an UpdatePanel in the page which contains a label, textbox, and a button. The label lists the currently available children of the current node, the user enters in which child they want to go to into the textbox, and then hits the submit button. I set the new value of the node in the submit button's event handler.

Here's my problem: Every time I enter in which node I want to navigate to, the object resets its value to the value it was initially initialized to. I have even put this same code into a Windows Form to validate that it's working correctly to iterate through my tree, and it works as it should, so I know my problem is AJAX-related.

This is the first app that I have written using AJAX, so I am still in the process of learning how it works. Any开发者_如何学运维 help would be greatly appreciated. I have Googled and searched SO through and through.

Here is the HTML:

<form id="form1" runat="server">    
    <asp:ScriptManager ID="ScriptManager" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:Label ID="question" runat="server" Text=""></asp:Label>
            <br />
            <asp:TextBox ID="answer" runat="server"></asp:TextBox>
            <br />
            <asp:Button ID="Submit" runat="server" Text="Submit" onclick="Submit_Click" />
        </ContentTemplate>
    </asp:UpdatePanel>
</form>

And the C#:

    protected void Submit_Click(object sender, EventArgs e)
    {
        int ans = int.Parse(answer.Text);

        if (!current.ChildIDs.Contains(ans))
        {
            return;
        }
        current = tree.Node(ans);
        question.Text = current.Question;
    }

current is the current node, which has a public ArrayList of all of its children's IDs. tree is the NodeTree I have; calling Node just returns the new node. Both current and Tree get initialized in the Page_Load event, and that only fires once (when the page is first loaded).

It's really pretty simply code; I'm just having difficulty understanding why the AJAX isn't working correctly.


I have even put this same code into a Windows Form to validate that it's working correctly to iterate through my tree, and it works as it should, so I know my problem is AJAX-related.

It sounds like you're expecting ASP.NET to remember what the object current is between requests, since that's how Windows forms applications work.

Web applications are stateless - after each request, ASP.NET discards all your variables. To access the variable during a subsequent request, you have to either:

1) Send enough data with the request to reconstruct the variable. You can do this using a querystring parameter or an HTML form value (the hidden fields another response mentioned).

2) Save the variables in a Session store (which can be in-memory or backed by a database).

3) Store the value in a coookie.

Of these three, it's easiest to show you how to use Session, given what you've shared in your question. However, beware: session has its risks - by default, ASP.NET session objects are stored in-memory, and it's a potential security hazard. But here's how you should be able to get your application to work.

// In your Page_Load code that initializes your 'current' variable

// When the user first requests the page, create a new Node
if (! this.IsPostBack) 
{
    Node current = new Node(); //
    Session("currentNode") = current;
} 
// When the user clicks a button on the page (posts), use the
// node in session instead
else 
{
    current = Session("currentNode");
}


When you update non-form elements in the browser (labels, literals, etc.), .NET is unable to see any of the changes you've made.

Try adding a hidden input for each label that records the new value. Then within the method you have wired up to the button's OnClick event, do something like this:

myLabel.Text = myHiddenInput.value;


I think you just need to tell the updatepanel to update itself. Try this:

protected void Submit_Click(object sender, EventArgs e)
{
    int ans = int.Parse(answer.Text);

    if (!current.ChildIDs.Contains(ans))
    {
        return;
    }
    current = tree.Node(ans);
    question.Text = current.Question;

    UpdatePanel.Update();

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号