views:

171

answers:

3

I'm creating user controls that i will put into an update panel and make them visible only when required using triggers. Once visible it will float in a dialog box so it has a close button which will just hide the control on client side.

The controls have multiple post back states, like a wizard, i'm using a multi view control to accomplish that. My problem is that once the user is at step number two in the control, if the user closes the dialog, than opens the dialog again, (note that the control is made visible on the server and reloaded by updating the updatepanel) the second step will still be displayed. The reason is . because whenever there is a postback the control will load its state using the viewstate, even if EnableViewState is false it will still load it using the LoadControlState method. so my quesion is how can i force a user control to load fresh with its default values without any postback data.

A: 

We had a similar circumstance and the only way we found to reset our control was to do that in the code behind of the page. Not in an ajax call but on submit of the page because then we could set the properties and have them go into viewstate.

i'm pretty sure that that will break your design though.

Have you considered writing the page as RESTful? At least then you can ditch viewstate and read and write to a meaningful data store.

ViewState is probably the number one reason I went over to MVC and now avoid WebForms like the plague.

griegs
I was thinking of moving to MVC, but i wasn't sure that this should be the reason to make the move, I thought maybe there "is" a way to reset a form/control on the server without having to rest every property manually. Can you point me to a good article how to make a WebForms application RESTfull with out making to much changes?
Shrage Smilowitz
A: 

If its a wizard that uses a dialog and each step is required, dont have a close button. If the user closes it you could refresh the whole page so the user has to start again?

I had so many issue like this with WebForms, where I was only using the UpdatePanel for ajax as it "looks" like an easy option. MVC sounds like a bit of a learning curve and it is, however you can acheive things by building pages with MVC and jQuery without MS ajax and the hassle of all the events in a page that constantly fighting with each other. Its difficult to make this step without knowing MVC and geetting your hand dirty, but its worth it.

Mark Redman
I'm fully with you, but being that by nature i'm jumping all over the place, and very hardly get things finished, i'm trying to contain my moves ;-) suddenly talking my application and converting it to MVC whould be exactly the reason my application will hanker down and stop moving forward. So i wanna move to mvc from within webfroms, by reorganizing the webforms application as much possible to behave like an MVC architecture, so that the move to MVC would not be such a drastic jump. any pointers?
Shrage Smilowitz
An idea: Think of MVC a bit like ASP, where you can call public methods do loops with asp tage eg <% foreach () %> this way your html can be very basic and not use asp controls and in future easily support MVC [others might not think this is a good idea]
Mark Redman
A: 

Its is possible.

I found a secret method in the control class, called ClearChildState(), this is a protected method and will clear the viewstate and the controlstate for all childcontrols.

So in my example, i created a class that inherits from panel

namespace MyControls
{
  public class Panel:System.Web.UI.WebControls.Panel
    {
        public void Reset()
        {
            ClearChildState();
        }
    }
}

In my page initialize event i check for request[flag] to reset the control

public partial class Test : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        if (!string.IsNullOrEmpty(Request["Reset"]) && Request["Reset"] == "1")
        {
            pnlCreateAccountDialog.Reset();               
        }
    }
}

OnClient side i have a Reset() function that i can call whenever i want the next postback to load a clean control

  <script type="text/javascript">
        function AddReset() {
            $('#Reset').val('1');
        }
        function RemoveReset() {
            $('#Reset').val('');
        }
    </script>
Shrage Smilowitz