listbox question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike P

    #1

    listbox question

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

    if (!(Page.IsPostB ack))
    {
    SqlConnection objConnection = new
    SqlConnection(C onfigurationSet tings.AppSettin gs["strConnectTran sitTest"]
    );
    string strTelcoNumber = "SELECT DISTINCT OSValue FROM CallTypeSwitch
    ORDER BY OSValue";

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

    SqlCommand objCmd = new SqlCommand(strT elcoNumber, objConnection);

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

    //Databind the DataReader to the listbox Web control
    lstRoutes.DataS ource = objDataReader;
    lstRoutes.DataB ind();
    }

    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!
  • S. Justin Gengo

    #2
    Re: listbox question

    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" <mrp@telcoelect ronics.co.uk> wrote in message
    news:epobtWBXDH A.3248@tk2msftn gp13.phx.gbl...[color=blue]
    > I have the following code in my Page_Load event to populate a listbox
    > from a database :
    >
    > if (!(Page.IsPostB ack))
    > {
    > SqlConnection objConnection = new
    > SqlConnection(C onfigurationSet tings.AppSettin gs["strConnectTran sitTest"]
    > );
    > string strTelcoNumber = "SELECT DISTINCT OSValue FROM CallTypeSwitch
    > ORDER BY OSValue";
    >
    > //You must open the connection before populating the DataReader
    > objConnection.O pen();
    >
    > SqlCommand objCmd = new SqlCommand(strT elcoNumber, objConnection);
    >
    > //Create/Populate the DataReader
    > SqlDataReader objDataReader = null;
    > objDataReader = objCmd.ExecuteR eader();
    >
    > //Databind the DataReader to the listbox Web control
    > lstRoutes.DataS ource = objDataReader;
    > lstRoutes.DataB ind();
    > }
    >
    > 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![/color]


    Comment

    • Mike P

      #3
      Re: listbox question

      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!

      Comment

      Working...