473,406 Members | 2,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

listbox question

I have the following code in my Page_Load event to populate a listbox
from a database :

if (!(Page.IsPostBack))
{
SqlConnection objConnection = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectTransitTest"]
);
string strTelcoNumber = "SELECT DISTINCT OSValue FROM CallTypeSwitch
ORDER BY OSValue";

//You must open the connection before populating the DataReader
objConnection.Open();

SqlCommand objCmd = new SqlCommand(strTelcoNumber, objConnection);

//Create/Populate the DataReader
SqlDataReader objDataReader = null;
objDataReader = objCmd.ExecuteReader();

//Databind the DataReader to the listbox Web control
lstRoutes.DataSource = objDataReader;
lstRoutes.DataBind();
}

What I want to do is to get rid of the IsPostback so that if the
database is updated the listbox will also be updated. But in order to
do this I need to store the value chosen by the user and set the listbox
to that value on my Submit button click event. I think ViewState might
be the right way to go about this but I have no real experience of using
this in C#. Does any body know how to do this?
Any advice would be much appreciated.

Cheers,

Mike


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 17 '05 #1
2 1627
Mike, you'll be happt to hear that it's pretty darn easy.

From the code behind page ViewState functions very similarly to a Session or
Application variable (as far as setting and retrieving it).

Now, I program mostly in VB.Net, but the syntax shouldn't be too different.
Just look at how you set a Session or Application level variable in c# and
ViewState will be about the same.

In VB you set it like this:

ViewState("[Your Key Name Here]") = [Your Object Here]

Or

ViewState.Add("[Your Key Name Here]", [Your Object Here])

Now, I question for your purposes here if you even need to use viewstate.

If all you're worried about is resetting the list box to the item a user
chose then wouldn't you get the item chosen, recreate the list box, and then
reselect the item?

This would all happen server side in one single trip wouldn't it?

If there was a post back to the client in the middle then you'd need to
store the value in view state, but otherwise why not just store it in a
regular old variable?

Sincerely,

--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
"Mike P" <mr*@telcoelectronics.co.uk> wrote in message
news:ep**************@tk2msftngp13.phx.gbl...
I have the following code in my Page_Load event to populate a listbox
from a database :

if (!(Page.IsPostBack))
{
SqlConnection objConnection = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectTransitTest"]
);
string strTelcoNumber = "SELECT DISTINCT OSValue FROM CallTypeSwitch
ORDER BY OSValue";

//You must open the connection before populating the DataReader
objConnection.Open();

SqlCommand objCmd = new SqlCommand(strTelcoNumber, objConnection);

//Create/Populate the DataReader
SqlDataReader objDataReader = null;
objDataReader = objCmd.ExecuteReader();

//Databind the DataReader to the listbox Web control
lstRoutes.DataSource = objDataReader;
lstRoutes.DataBind();
}

What I want to do is to get rid of the IsPostback so that if the
database is updated the listbox will also be updated. But in order to
do this I need to store the value chosen by the user and set the listbox
to that value on my Submit button click event. I think ViewState might
be the right way to go about this but I have no real experience of using
this in C#. Does any body know how to do this?
Any advice would be much appreciated.

Cheers,

Mike


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 17 '05 #2
Justin,

In the Page_Load event can I set the value of the listbox to the value
entered by the user after I have re-populated it? And if so, what is
the syntax for doing this? Or do I have totally the wrong idea ?
(sorry, I am pretty new to ASP.NET and particularly what I am trying to
do now)

Mike


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Stimp | last post by:
I have a listbox of values that I populate from a database. I want the user to be able to re-order the list (by first selecting an item and then clicking 'up' or 'down' buttons) and then save...
3
by: George | last post by:
Sub ExcelToListBox() Dim xRange As Object Dim ary Dim xValue As String xRange = oXL.Range("A1:A9") 'has letters A-H ary = xRange.value xValue = ary(3, 1) 'xValue = C...
0
by: | last post by:
Greets All, Question on data in datagrid /listbox and looping? I’m trying to decide the best way to write this code. 1 would like the user to make several selections from one listbox/combo box...
0
by: | last post by:
Greets All, Question on data in datagrid /listbox and looping? I’m trying to decide the best way to write this code. 1 would like the user to make several selections from one listbox/combo box...
2
by: dan NY | last post by:
I'm a struggling new VB Applications user that has what may be a simple question, but I've been struggling with it. I'm using a response to a message box YesNo question to cause one of two...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.