Link to home
Start Free TrialLog in
Avatar of -Dman100-
-Dman100-Flag for United States of America

asked on

why is my dropdown list getting reset after the page refreshes?

I have a created a form using a user control.  One of the dropdownlists in the form is populated after the user makes a specific selection from another dropdownlist...i.e. a dependent dropdown list.  This is triggered using the SelectedIndexChanged event handler on the controllling dropdownlist.

The dependent dropdownlist gets populated correctly, but when I choose one of the options and go to submit the form, the dependent dropdown list is reset and doesn't maintain the selection that was made.

The controlling dropdown list ID is "level".  The dependent dropdown list ID is "EnrollmentSize".

I've attached a text file copying the user control code and code-behind used in the form.

I thought it had something to do with EnableViewState, but that is set to true and has no bearing.  Everytime I try to submit the form, the "EnrollmentSize" DDL is reset and doesn't maintain the option that is selected.

Thanks for any help.
code.txt
Avatar of plusone3055
plusone3055
Flag of United States of America image

you need to implement a javascript that refreshes the query and that varaible needs to be saved so that when you refresh the page the selection from the drop down menu stays

and example would be...

function RefreshQuery(){
document.myform.Variable1.value=document.myform.varaible.value
document.myform.submit()
}

Happy Coding
if you populating the dropdown on PostBack, then put the following condition


if (!Page.IsPostBack) {
....
}

Open in new window

may be something like this
protected void level_SelectedIndexChanged(object sender, EventArgs e)
{
	if (!Page.IsPostBack) 
	{
		string sv = level.SelectedValue;

		switch (sv)
		{
			case "District":
				EnrollmentSize.Items.Insert(0, new ListItem("Please Select...", ""));
				EnrollmentSize.Items.Insert(1, new ListItem(">24000 Schools", ">24,000 Schools"));
				EnrollmentSize.Items.Insert(2, new ListItem("15001 - 24000 Schools", "15,001 - 24,000 Schools"));
				EnrollmentSize.Items.Insert(3, new ListItem("2001 - 15000 Schools", "2,001 - 15,000 Schools"));
				EnrollmentSize.Items.Insert(4, new ListItem("1 - 2000 Schools", "1 - 2,000 Schools"));
				break;
			case "School":
				EnrollmentSize.Items.Insert(0, new ListItem("Please Select...", ""));
				EnrollmentSize.Items.Insert(1, new ListItem(">2000 Students", ">2,000 Students"));
				EnrollmentSize.Items.Insert(2, new ListItem("501 - 1999 Students", "501 - 1,999 Students"));
				EnrollmentSize.Items.Insert(3, new ListItem("151 - 500 Students", "151 - 500 Students"));
				EnrollmentSize.Items.Insert(4, new ListItem("<150 Students", "<150 Students"));
				break;
			default:
				break;
		}
	}
}

Open in new window

Avatar of -Dman100-

ASKER

roshmon,

I tried adding the !Page.IsPostBack, but as I expected, the DDL doesn't get populated at all.  I reversed it and tried Page.IsPostBac, but that didn't work either.  The selection is still reset.

plusone3055,

Thanks for the suggestion, but I'm not clear on why a javascript solution would need to be implemented here.  Can you clarify?  Why isn't the asp.net ddl control not maintaining the dropdown list item value on the submit button event handler?

Thanks.
try to do it in Page_Load
try this

protected void Page_Load(object sender, EventArgs e)
{
	if (!Page.IsPostBack)
	{
		StringBuilder sb = new StringBuilder();
		sb.Append("<script type=\"text/javascript\" language=\"Javascript\">\n");
		sb.Append("var jlcProt = ((location.protocol==\"https:\") ? \"https:\" : \"http:\");\n");
		sb.Append("document.write('<script type=\"text/javascript\" language=\"Javascript\" src=\"' + jlcProt + ");
		sb.Append("'//sftrack.searchforce.net/SFConversionTracking/CTCommon.js\"><\\/script>');");
		sb.Append("</script>\n");
		sb.Append("<script type=\"text/javascript\" language=\"Javascript\">\n");
		sb.Append("var jValue = \"\";\n");
		sb.Append("var jOrderID = \"\";\n");
		sb.Append("var jconversion_type = \"lead\";\n");
		sb.Append("var jvar1=\"\";\n");
		sb.Append("var jvar2=\"\";\n");
		sb.Append("var jvar3=\"\";\n");
		sb.Append("var jprotocol = \"http\"; if(location.protocol == \"https:\"){jprotocol = \"https\";} ImageJSConversionProcess();\n");
		sb.Append("</script>\n");

		if (IsProductRequest)
		{
			ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "conversioncode", sb.ToString(), false);
		}
	}
}

Open in new window

Hi roshmon,

I tried adding !Page.IsPostBack in the Page_Load event handler, but that still doesn't correct the issue.  The EnrollmentSize DDL is still reset when I click the submit button.

I cannot see why the the selected value is not maintained?
I can't see any reason why what you've done shouldn't work. When you say EnrollmentSize is reset do you mean it loses all the values in it or just the option that is selected?
If it's the former then I'd suspect it's probably ViewState related, is viewstate enabled on the page that contains the user control and the user control itself?

As a test, if you change the level dropdownlist and then select another value, do you get both sets of items added together? The reason I ask is that your routine to populate the dropdownlist does not clear the list items first (which it should), therefore if this appears to behave correctly then the list is getting cleared somewhere else, either through lack of viewstate persistence or being cleared out explicitly somewhere else.
Avatar of b_levitt
b_levitt

When you say reset, are the options completely cleared out of EnrollmentSize, or does it just go back to the first option, but the items are still there?

If it's the former, I'm guessing your onselectedindexchanged event handler is running more than once for some reason.  A shot in the dark is the "Selected='True'" you have on the first item of the level DDL.

Are you loading this UserControl programmatically in code behind?  If so you might be adding it too late in the event cycle or without an ID.

When I have issues like this, sticking a breakpoint in all the events helps to see what is getting hit and then you can track down why.
The EnrollmentSize only losses the option that was selected.  The DDL is sitll populated with all the correct values.  It only losses the option that was selected and goes back to the first option...i.e.  "Please Select..."

The viewstate on the page is enabled and all the other form values are maintained.  It is only happening with the EnrollmentSize ddl.  

I've set breakpoints and tried debugging and I do see that the EnrollmentSize.SelectedValue is an empty string, but I don't see why or where that is happening in the button click event.
Were you able to solve this?  If not, can you provide a stand alone version of the code (including an aspx page to host your ascx) so I can try to run it myself?
Hi b_levitt,

No, I was never able to solve this.  For the moment, I just added the values statically to the EnrollmentSize dropdown list.

I've attached a sample of the form running.  I removed a couple of the fields...like state and country since those are populated dynamically and I commented out some lines on the button click event handler that uses a postme.dll that simulates a form post action.  Also, the formatting isn't going to be pretty because I don't have the style sheet attached.  But, you should be able to run the form and debug to see what I'm encountering.

I had to change the file name and extensions to .txt in order to upload the file, so you'll have to change the extension back to aspx.cs and .ascx.cs as required for it to run.

Thanks for your help with this.  I really appreciate it.

Regards
sample.zip
ASKER CERTIFIED SOLUTION
Avatar of b_levitt
b_levitt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi b_levitt,

Sorry for the delay in getting back to you.  It appears the problem is somehow related to our company master page.  We encounter a very similar issue with the ItemCommand event handler not getting raised.

We isolated the issue to the master page, but runnign the code without the master page and it ran fine, but as soon as we attach the master page it bombs.  It is very strange because we have tons of other pages that use the master page and have event handlers in the code-behind and they run fine.  It is only new changes to existing pages or a new page altogether that is causing this to happen.

So, for the moment we seem to be stuck until we can dedicate some time to dig deeper into what is causing this to happen.  We are probably going to have to re-build the master page bit by bit and test to see what and where it breaks.

I sincerely appreciate all your help and the time and effort you put into helping me debug this issue.

Best Regards.
Problem is related to master page.  Unable to resolve the issue currently.  All the help was accurate and complete.  Thanks again!