Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 20th, 2006, 10:15 AM
Registered User
 
Join Date: Jul 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to jlechem Send a message via MSN to jlechem
Default Problem with listbox postback

Hello,

I have a listbox control on a 1.1 asp.net page that is set to autopost back. When it posts back I need to gather certain info from a Dataset and do some things on the page. However no matter what item they select in the list box none of the properties in the code behind have any valid values. i.e. SelectedValue, SelectedItem, SelectedIndex. I need to know what item they have selected in the list box. I am databinding to the control and using column names to assign the DataTextField and DataValueField to the list box. Any help would be appreciated.

Justin

 
Old July 20th, 2006, 12:38 PM
Registered User
 
Join Date: Jul 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to jlechem Send a message via MSN to jlechem
Default

problem solved, the databinding was fubaring the listbox items. I added a postback check and made sure databind only happened in non post backs and the problem was solved.

 
Old July 26th, 2006, 11:45 AM
Authorized User
 
Join Date: Jul 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello,

          I am using ASP.NET 1.1 with VB.NET. My WebForm is having 3 Tabs viz., Tab1,Tab2 and Tab3. All the 3 Tabs are hyperlinked to different WebForms. I am having a ListBox1 in Tab1 with few items in it. What i need is that when i select an item say item1 from the ListBox1 and then go to Tab2 page, when i again come back to Tab1, then item1 should remain selected.

I have tried to do it with EnableViewState="True" and AutoPostback="True" on Page_Load but it didnt worked.

Any idea/suggestion is most welcome

Regards
Monica

 
Old July 26th, 2006, 12:48 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Ok, since .NET pages can only have one FORM control per page, I assume you mean that the clicking on a Tab links to another page. Here is the problem. The reason Viewstate isn't working for you is because viewstate isn't designed to work like that.

Viewstate will maintain your form variables through multiple post's to the same page, navigating away, well viewstate is lost for that page (which is what you are experiencing).

Solution?

I had to write an app similiar to this that used a Tab style menu system, the trick for me was the "tabs" didnt actually link to another page. What I did was create 6 panels on the page, panel 1 was set to be visible all other panels were set to be invisible. Clicking the tabs just changed which panel was visible which was not, but, you could click on a previous tab and all of your form values would still be there because, as i said, everything was contained on one page and thus was saved in Viewstate.

Hth.



"The one language all programmers understand is profanity."
 
Old July 26th, 2006, 01:07 PM
Authorized User
 
Join Date: Jul 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your reply.
Yeah....The trick you described should work with some extra work.
I feel there is no other way out to do that.
Thanks again!

Regards
Monica

 
Old July 26th, 2006, 01:10 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

No. There are multiple ways to do what you describe. You could, for example, store the values in session variables and re-populate the fields based on the contents of these variables. (This isn't exactly effecient since you would have to define a session variable for each form field. To counter this, you could create an array and store the form values in a session array which is only slightly more manageable.)

But thats the beauty of programming isn't it? There are many different ways to achieve the same goal =]

"The one language all programmers understand is profanity."
 
Old July 26th, 2006, 01:35 PM
Authorized User
 
Join Date: Jul 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yesssssss...earlier i also thought of using a session variable. But being not very experienced to coding, i was looking for viewstate way.

If i use session variable for the ListBox, shud i use it as on Page_load() ?

Session("ListBoxTest") = listbox1.SelectedItem.Value
where ListBoxTest is a session var in Global.asax

I am a bit astrayed about how to implement it using session var.
Is it the rite way to do?

Regards
Monica

 
Old July 26th, 2006, 02:34 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Well to get the values of a list box into a session variable, you will have to raise some sort of post back so that the values can get assigned else, if a user selects a bunch of stuff in a listbox and then navigates away with out posting the form, that information would be lost.

What you could do is something liket this:

Sub Page_Load
 Dim strValues() as string

 if not Session("values") is nothing then
   strValues = Session("Values")
   Dim item as listitem
   Dim i as integer

   for each item in listbox1.items
     if strValues(i) = [some value here] then item.selected

     i += 1
   Next
 end if
End Sub

Sub ListBox_PostBack
  Dim item as new listitem
  Dim strValues() as string
  Dim i as integer

  for each item in ListBox1.items
    If item.selected = true then
          strValues(i) = "true"
    else
          strValues(i) = "false"
    End if

    i += 1
  next

  Session("values") = strValues
End Sub

This is NOT very effiecent code, but it gives you an idea of how you would accomplish this using session variables. In as far as the array goes, if you know how many dimensions it is going to have, simply declare it with that many dimensions from the outset, else you will have to use ReDim Preserve (which can bog applications down if you are working with an extremely large array)

As far as the "right" way to do something, it comes down to preformance and personal preferance. I personally try to use very few Session variables as I don't like alot of things flying around during a session (less to manage and worry about). Implement it with what you feel comfortable with.

hth

"The one language all programmers understand is profanity."
 
Old July 26th, 2006, 02:39 PM
Authorized User
 
Join Date: Jul 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for all the help. Its time to act now. I will get back to you with results.

Regards
Monica

 
Old July 26th, 2006, 02:42 PM
Authorized User
 
Join Date: Jul 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Another thing, my listbox is single-value select only.
Where to use/call the ListBox_PostBack routine?

Regards
Monica





Similar Threads
Thread Thread Starter Forum Replies Last Post
Postback problem dbrook007 ASP.NET 2.0 Professional 3 May 8th, 2008 01:02 AM
Postback Problem dbrook007 ASP.NET 2.0 Professional 1 April 1st, 2008 06:16 AM
Listbox,Postback and Datasource nichola_x_rose ASP.NET 1.0 and 1.1 Basics 5 March 22nd, 2007 04:13 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.