Change panels based on drop-down menu index change

Mike521

Freshman
Joined
May 3, 2004
Messages
27
Hey all, I've got a webform that displays additional information based on the item selected in a drop down menu. Right now it's set up so the user clicks a "continue" button after making their selection. However I'd like it to automatically extend to the additional info right after they make their choice on the drop down menu.

I tried adding " OnSelectedIndexChanged="btnNext_Click " but that didn't seem to do it.


Thanks in advnce for any suggestions :)

Here's the code for btnNext



Code:
Sub btnNext_Click( s As Object, e As EventArgs )


             pnlForm2.Visible = True
 
  	if children.selecteditem.text = "1 Child - $135.00" then
		pnlChild2.Visible = false
		pnlChild3.Visible = false
	end if
	
	if children.selecteditem.text = "2 Children - $209.00" then
		pnlChild3.Visible = false
	end if
	
	if school.SelectedItem.Text = "Park View Elementary" then
		chkChild1Session2.visible = false
		chkChild2Session2.visible = false
		chkChild3Session2.visible = false
	end if
  

	
End Sub
 
Robby said:
Set AutoPostback to True in the dropDown control then use its SelectedIndexChanged event to execute your code.


thanks! that did the trick! Let me ask you though -- when I get to the point of sending the whole form, will this "autopostback" thing try to send the form? Not sure what it does besides this.

Another problem that I'm hitting now.. if the selected item = "Select One" it's supposed to hide part 2 of the form (meaning they haven't selected anything yet).

Technically it's not necessary because that part of the form will never be visible until they do select something. But nevertheless, for the sake of being complete, I added the line of code to do it, however it's not working. I'll put the new code below. Note the if block, it's supposed to evaluate to true, however it either doesn't eval to true, or it just ignores it somehow... Any ideas?


Code:
Private Sub Children_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

	txtFamilyName.text = children.selecteditem.text
	pnlPayment.visible = true

 
   	if (School.selecteditem.text = "Select One") or (children.selecteditem.text = "Select One") then
		''if this is true, user has not chosen school or children. do not show them anything yet
		txtFamilyName.text = children.selecteditem.text
		pnlChild1.Visible = false
		pnlChild2.Visible = false
		pnlChild3.Visible = false
		pnlPayment.visible = false
	end if
 
	if school.SelectedItem.Text = "Park View Elementary" then
		chkChild1Session2.visible = false
		chkChild2Session2.visible = false
		chkChild3Session2.visible = false
	else
		chkChild1Session2.visible = true
		chkChild2Session2.visible = true
		chkChild3Session2.visible = true		
	end if
 
  	if children.selecteditem.text = "1 Child - $135.00" then
		pnlChild1.Visible = true
		pnlChild2.Visible = false
		pnlChild3.Visible = false
	end if
	
	if children.selecteditem.text = "2 Children - $209.00" then
		pnlChild1.Visible = true
		pnlChild2.Visible = true
		pnlChild3.Visible = false
	end if
	
	if children.selecteditem.text = "3 Children - $275.00" then
		pnlChild1.Visible = true
		pnlChild2.Visible = true
		pnlChild3.Visible = true	
	end if

End Sub
 
AutoPostBack and PostBack means that the page is sent back to the server for processing.

At this point you need to seperate your Page_Load event into three parts;
(All these are optional of course)
Code:
sub Page_Load(....)
 
	 If Not IsPostBack then
			1) Code that runs only the very first time your page is loaded
	 elseif IsPostBack 
			2) code that runs each subsequent time
	 end if
 
	 3) Code that will run every single time the page is loaded
 
end sub
 
Robby said:
AutoPostBack and PostBack means that the page is sent back to the server for processing.

At this point you need to seperate your Page_Load event into three parts;
(All these are optional of course)
Code:
sub Page_Load(....)
 
	 If Not IsPostBack then
			1) Code that runs only the very first time your page is loaded
	 elseif IsPostBack 
			2) code that runs each subsequent time
	 end if
 
	 3) Code that will run every single time the page is loaded
 
end sub


OK I think I get your idea here, so I should lose the SelectedIndexChanged procedure, and put that stuff at the else IsPostBack section, right? Then when the postback comes, it does all those things and continues.. I'll give that a shot :) thanks for the help!
 
ok I did that and it works perfectly. seems to me this autopostback combined with an "if ispostback" statement in the page load area is a more accurate method, right? as opposed to doing "if selectedindexchanged"

Any time you can think of that it's better to use selectedindexchanged?
 
You would never use "if selectedindexchanged"...

You will/could use the Selectedindexchanged event for many situations such as;
Say you have a list of cities in a dropDownList and upon the selection of a city you want to fetch (from a datatable) all residents of that city; you would place the Fetch logic in the SelectedIndexChange event, WHILE still keeping the logic that loaded the DropDowwnList( for City) in the Page Load event in a "If Not IsPostBack" condition.
 
Back
Top