views:

34

answers:

3

The current form is here. It is not complete, and only a couple options will work.

Select "Image CD" and then any resolution and click "Add to Order." The order will be recorded on the server-side, but on the client-side I need to reset the product drop-down to "{select}" so that the user will know that they need to select another product. This is consistant with the idea that the sub-selections disappear.

I don't know whether I should be using ASP postback or standard form submittal, and most of the fields need to be reset when the user adds an item to the order.

+1  A: 

In the pageload event on the form, you need to add something simalar to this:

if (IsPostBack)
{
   //Proccess the order here

   ProductOption.SelectedIndex = 0;
}

That will allow you to process the order, but then start over the order form.

Jason Webb
Works perfectly! Thanks!
Giffyguy
+1  A: 

The simplest means would be a recursive function that forks on the type of control

private void ResetControls( Control control )
{
    if ( control == null)
        return;

    var textbox = control As TextBox;
    if ( textbox != null )
        textbox.Text = string.Empty;

    var dropdownlist = control as DropDownList;
    if ( dropdownlist != null )
        dropdownlist.SelectedIndex = 0; // or -1

    ...

    foreach( var childControl in controlControls )
        ResetControls( childControl );
}

You would this call this function in your Load event by passing this. (This is presuming you want to reset more than a single control or small list of controls).

Thomas
+2  A: 

I'd use Response.Redirect(Request.RawUrl) method to reset the form data from the server side.

A little explanation: this is PRG design pattern

Used to help avoid certain duplicate form submissions and allow user agents to behave more intuitively with bookmarks and the refresh button.

A workaround for this question: necessary data may be stored in Session for example. This means we getting the data with the first POST, putting it to the storage, performing redirect and getting it back.

Alex
This definitely makes sense. The only drawback to this solution is that you don't get to be selective about which fields are reset. I may want the "Image" field to persist its value.
Giffyguy
Maybe use Server.Transfer instead. Saves a round trip to the browser and back.
Jon P
@Jon P: nope, I'm going to edit my answer to add explanation.
Alex
Is there a way to persist previous data with this approach? I can't wait to hear it. If that was possible, this would no doubt be the more robust solution.
Giffyguy
@Jon P: Response.Redirect() does not a waste round trip. If you post to a script that clears the form by Server.Transfer() and reload you will be asked to repost by most browsers since the last action was a HTTP POST.
Alex