Avien
Avien

Reputation: 1260

Force a usercontrol on web page to reinitialize iteself

There are 2 user controls, one is inside the other.

These controls are located on an .aspx.

One control is a modal popup the other is a custom search control (the search control is inside the modal popup).

After a user has completed a search and closes the form, the next time the popup opens (for the same user) the old values are still present.

How can I clear the values out each time the form loads?

Edit: Is it possible to capture the popup closing event?

Upvotes: 3

Views: 1168

Answers (4)

Avien
Avien

Reputation: 1260

As it turns out there is no method on the control to clear out the values on the control.

So what I have to do is create the method on the search control to clear out the control and create a method on the popup control to capture the popup open or close event to trigger the clear method.

At first I wasn't going to be able to change the control..since explaining that this was the only way to solve the issue the control can be changed..thank god! And since the method is not going to be a default option it won't affect anywhere else the controls are used.

Upvotes: 2

Gage
Gage

Reputation: 7513

For one of the applications I currently maintain we use a recursive function to reset all the fields within the control. I have added it below but remember it is used in a C# windows application not a web application. Hope it helps.

public static void ResetFields(Control.ControlCollection pageControls)
    {
        foreach (Control contl in pageControls) 
        {
            var strCntName = (contl.GetType()).Name;
            switch (strCntName)
            {
                case "Button":
                    contl.Enabled = true;
                    break;
                case "TextBox":
                    var txtSource = (TextBox)contl;
                    txtSource.Text = "";
                    break;
                case "ListBox":
                    var lstSource = (ListBox)contl;
                    lstSource.SelectedIndex = -1;
                    lstSource.Enabled = true;
                    break;
                case "ComboBox":
                    var cmbSource = (ComboBox)contl;
                    cmbSource.SelectedIndex = -1;
                    cmbSource.Enabled = true;
                    break;
                case "DataGridView":
                    var dgvSource = (DataGridView)contl;    
                    dgvSource.Rows.Clear();
                    break;
                case "CheckBox":
                    var chkSource = (CheckBox)contl;
                    chkSource.Checked = false;
                    chkSource.Enabled = true;
                    break;
            }
            ResetFields(contl.Controls);
        }
    }

Upvotes: 3

Wallace Breza
Wallace Breza

Reputation: 4948

Assuming this user control has some client side JS code associated with it, I would modify the control to clear out its search values and results upon closing the modal popup.

This will ensure when the modal popup is reopened its in the correct state for new searches.

Sys.Application.add_init wouldn't work in this case since that will only run once when the page is loaded.

You need to write some new JS when the model popup is cancelled or closed.

Modifying the control will be your best option here so all other pages that use this control can take advantage of the new reset functionality.

It's difficult to give you any more specific information at this time without knowing the structure of your page, usercontrol and any existing javascript.

Upvotes: 1

automagic
automagic

Reputation: 1077

You could clear all the values using javascript- here is an example using jquery:

http://beckelman.net/post/2008/09/24/Clear-Input-Fields-in-an-AjaxControlToolkit-ModalPopup-When-Cancel-is-Clicked.aspx

Upvotes: 3

Related Questions