By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,501 Members | 1,678 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,501 IT Pros & Developers. It's quick & easy.

listbox question

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.