listbox question

M

Mike P

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
 
S

S. Justin Gengo

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
 
M

Mike P

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top