Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keeping selection in dropdownlist visible 1

Status
Not open for further replies.

JoeCool32

Programmer
Sep 26, 2002
50
US
In my dropdown list for my latest project, I'm trying to put a piece of data on a label from the selection I make in the list. I have autopostback="True" so the aspx page will posted back to the server. The problem I'm having is that the value in the list keeps clearing whenever I make a new selection. [ponder] How can I keep whatever I select visible in the dropdown list?
 
Make sure EnabledViewState is enabled.

The other thing that may be happening is how you fill the drop down. If you are filling it in your page_load event you need to put this around it.
If Not Me.IsPostBack Then

End If

Otherwise your datagrid gets filled everytime the page makes a postback thus wiping out the selected index data. It also makes your page a lot more efficient since the data doesn't need to be fetched everytime but only the first time. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Thanks.

The drop down is filled based upon the selection of a radiobutton list. Depending on the selection choice, a program is run to fill the dd with the appropriate data.

Oh, I'm not using a datagrid. Our lead ASP.NET programmer doesn't like using them; therefore datagrids are verboten. [thumbsdown]

Would it be OK to post my code so you could see what I'm doing?
 
Well then I think your lead programmer is silly. I had meant drop down just misstyped.

Anywho Yes post your code I'll see if I can't spot it. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Ok, here we go.

First the .aspx pg code:

Search By:
Code:
<ASP:RADIOBUTTONLIST autopostback=&quot;True&quot; cssclass=&quot;BlackFont7&quot; id=&quot;rblAirportNameCityState&quot; runat=&quot;server&quot;>
	<ASP:LISTITEM selected=&quot;True&quot; value=&quot;CityState&quot;>City & State</ASP:LISTITEM>
        <ASP:LISTITEM value=&quot;AirportName&quot;>Airport Name</ASP:LISTITEM>
</ASP:RADIOBUTTONLIST>
					
<ASP:DROPDOWNLIST autopostback=&quot;True&quot; enableviewstate=&quot;True&quot; id=&quot;drlAirportNameCityState&quot; onselectedindexchanged=&quot;MadeSelection&quot; runat=&quot;server&quot; visible=&quot;False&quot;>						<ASP:LISTITEM value=&quot;&quot;></ASP:LISTITEM>
</ASP:DROPDOWNLIST>
						
<ASP:LABEL id=&quot;lblMessage&quot; runat=&quot;server&quot;></ASP:LABEL>

I did use 'enableviewstate' but it hasn't helped yet.

Now the codebehind (it's VB):

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
drlAirportNameCityState.Visible = False 'setting default display value for dropdownlist
RemoveItemsFromDropDownList() 'removing any listitems from drop down list
'based on selected search by option calling sub that will populate drop down list w/ appropriate info
If rblAirportNameCityState.Items(0).Selected = True Then 'Search By City
SearchByCityState(sender, e)
ElseIf rblAirportNameCityState.Items(1).Selected = True Then 'Search By Airport
SearchByAirportName(sender, e)
End If
End Sub

Sub RemoveItemsFromDropDownList()
'removing all the elements from the drop down list collection
Dim theLoopCount As Int16 = 1
Do While theLoopCount < drlAirportNameCityState.Items.Count()
drlAirportNameCityState.Items.RemoveAt(theLoopCount)
Loop
End Sub

Public Sub MadeSelection(ByVal sender As Object, ByVal e As System.EventArgs)
'writing content to page
lblMessage.Text = &quot;Sorry! We don't have that info yet.&quot;
End Sub

Hope that's enough for now. B-)
 
I have written this assuming you have the basic knowledge of making event handlers and such.
Ok I am guessing on how some of your code works as it obviously isn't all here. From what I've got we need to do a couple of things.

1.
I am assuming that this is the call the the code that fills drlAirportNameCityState.

If rblAirportNameCityState.Items(0).Selected = True Then 'Search By City
SearchByCityState(sender, e)
ElseIf rblAirportNameCityState.Items(1).Selected = True Then 'Search By Airport
SearchByAirportName(sender, e)
End If


If it is then put this code into the event handler that triggers the list to be filled. That is whatever handles the radiolist postback event. If you don't have one you need to make one.

2.
Also that RemoveItemsFromDropDownList sub can be deleted. Instead put drlAirportNameCityState.Items.Clear at the top of the code that is filling drlAirportNameCityState. I am guessing that that is enscapulated in SearchByCityState and SearchByCityState.

3.
In the designer make drlAirportNameCityState visible = false so that is the default state. Then in the code-behind get rid of the drlAirportNameCityState.Visible = False line. Instead when you fill drlAirportNameCityState put drlAirportNameCityState.Visible = True

Now if I am correct you have nothing in the Page_Load any more. This is good. Your selection should also stay with you now as well. The problem was basically with the RemoveItemsFromDropDownList call. However, in order to make everything work I had to rearrange the code a little. If you have anymore questions feel free to ask and I'll see what I can do. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Thanks, Mark.

Let's hear it for the cool Canadian. [cheers][flip]
[wavey2][roll2]
 
Lol NP
How'd you know I was Candian? That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
I looked at your Member Profile.

So, how 'bout them Oilers? JJ

&quot;Ignorance and prejudice and fear walk hand in hand&quot; - Witch Hunt, by Rush
 
ah ya how 'bout them? Not a huge spectator sports fan. I'd rather be out doing something. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Well, to each their own. I don't care too much for hockey myself, but I know the Oilers had a great run w/ 'The Great One'.

One thing about your suggestions: The first list item for the radiobuttonlist (rbl) has to be selected upon the page loading per the lead programmer. So I need to keep that first If....Then statement in the Page_Load.

I did try it your way and got a compilation error as my new event handler wasn't recoginized. JJ [peace]

&quot;Ignorance and prejudice and fear walk hand in hand&quot; - Witch Hunt, by Rush
 
Yes the Oilers did have a good run but that was quite a while ago. I don't believe they are doing that great this year though.

About the if then in the page load. I understand that you need to have one selected but that if statment won't set the radio list to be selected only to check if it is.
Just put this code in the page load for a first time setup then rely on the event handler to get things done.

If Not Me.IsPostBack then
drlAirportNameCityState.Items(0).Selected = True
End If [/color green]

One thing that I did notice is that you don't have the Handles .... behind your event handler.
I was wondering why this is or if your code even works without it?

A radioList event handler should look like this
Private Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged

End Sub
That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
The way my lead set it up, event handlers aren't needed for the radio selections. The code for the ddl population subs borrows fr GlobalConstants and SQLComponent and is rather involved; I don't even understand half of it. Maybe you and he should get together and talk shop somehow.

I am something of a newbie when it comes to the .NET environment. I learned Basic in high school, but got away fr programming for a decade 'cause I thought I couldn't cut it on the more complex programs. Got back into it professionally, but in COBOL. Took a Visual Basic 5 course in 1999 as a resume enhancer but haven't put it to practical use until now. Have had to relearn it (along w/ starting ASP) in .NET on the job pretty much on my own {we use Wrox programming books, but they only help so much}. My unofficial title is Web/Intranet Trainee and my learning curve at this point is rather Malthusian.

Your suggestion about the Page_Load alternative does work very well, btw. Thanks for all your help. My lead is out sick, and my supervisor is more in tune w/ FoxPro, so your assistance has been helpful.

Anyhoo, what's there to go out and do in Edmonton, besides freeze to death in Celsius? My wife, every time out gov't does something she dislikes, wants to pack up the bags and move to Canada, so I'm somewhat curious. Would you recommend a move this time of year or any time of year? JJ [peace]

&quot;Ignorance and prejudice and fear walk hand in hand&quot; - Witch Hunt, by Rush
 
Glad I could help JJ. Sounds interesting how your lead has things setup. If you don't mind and think I am worthy could you click the &quot;Mark this post as a helpful/expert post!&quot; link?


As for a move. I love it here! Depending on where you live it might be a bit (you may have to read alot) of a climate change. So far this winter has been very mild nothing below -10 C. The norm however, is -16 and it's +10 today. I think Edmonton is a fairly nice city with quite a few parks and greenery around. Some of the suburbs and different areas have more culture to them. aka Nice architechture etc. The biggest thing is the weather. Since I don't mind the cold, (I enjoy snowboarding, sledding, playing) it doesn't bother me any.

Glad to be of service my good man That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
The post has been marked.

Enjoy the rest of your week. Here in the States we get a 4-day weekend on account of Thanksgiving, so in an hour I'll be out the door for a looooooooong while. [2thumbsup]

Thanks again. JJ [peace]

&quot;Ignorance and prejudice and fear walk hand in hand&quot; - Witch Hunt, by Rush
 
I envy you That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top