select listbox item programatically not working?

Discussion in 'ASP .Net Web Controls' started by JamesB, Dec 12, 2007.

  1. JamesB

    JamesB Guest

    I am new to web stuff, having previously done forms based apps... anyway, my
    web app currently has a listbox on a page that is populated from a database
    table.
    That works fine, I can see all the records, however I want to set the
    listbox to the name of the logged in user.

    I have done this:

    String ln = Page.User.Identity.Name;
    for (int i=0;i<lstNames.Items.Count;i++)
    {
    //Response.Write(" checking " +
    lstNames.Items.Value.ToString().ToLower() + " against " + ln.ToLower());
    if (lstNames.Items.Value.ToString().ToLower() ==
    ln.ToLower())
    {
    Response.Write("Setting item " + i + " as selected.");
    lstNames.SelectedIndex = i;

    }
    }

    My response.write says it is setting the index to 20 (which is correct for
    my username) however the listbox still shows the first name in the list.
    Do I need to do something to force the listbox to redraw or is there some
    other way? Seems like it should be so simple (and probably is...)!

    Thanks

    James
    JamesB, Dec 12, 2007
    #1
    1. Advertising

  2. JamesB

    Phil H Guest

    On 12 Dec, 16:28, "JamesB" <> wrote:
    > I am new to web stuff, having previously done forms based apps... anyway, my
    > web app currently has a listbox on a page that is populated from a database
    > table.
    > That works fine, I can see all the records, however I want to set the
    > listbox to the name of the logged in user.
    >
    > I have done this:
    >
    > String ln = Page.User.Identity.Name;
    > for (int i=0;i<lstNames.Items.Count;i++)
    > {
    > //Response.Write(" checking " +
    > lstNames.Items.Value.ToString().ToLower() + " against " + ln.ToLower());
    > if (lstNames.Items.Value.ToString().ToLower() ==
    > ln.ToLower())
    > {
    > Response.Write("Setting item " + i + " as selected.");
    > lstNames.SelectedIndex = i;
    >
    > }
    > }
    >
    > My response.write says it is setting the index to 20 (which is correct for
    > my username) however the listbox still shows the first name in the list.
    > Do I need to do something to force the listbox to redraw or is there some
    > other way? Seems like it should be so simple (and probably is...)!
    >
    > Thanks
    >
    > James


    James, with all due respect you seem seriously confused about how web
    applications work. You must study it properly, in particular the
    execution model and how to control the property values and appearance
    of web server controls. Your use of response.write is totally
    inappropriate - once you've assigned values to the control items the
    rendering will take care of itself.

    I and others could propose a programmatic solution to meet your
    requirements but I fear it would be a waste of time.
    Phil H, Dec 12, 2007
    #2
    1. Advertising

  3. JamesB

    Jeremy Guest

    Phil, pardon me for butting in, I don't suppose you considered the
    possibility that James' use of Response.Write was as a debugging tool, and
    not meant for production? I can't say of course, not being James, but
    couldn't I don't think it is useful to insult people.

    Jeremy


    "Phil H" <> wrote in message
    news:...
    > On 12 Dec, 16:28, "JamesB" <> wrote:
    >> I am new to web stuff, having previously done forms based apps... anyway,
    >> my
    >> web app currently has a listbox on a page that is populated from a
    >> database
    >> table.
    >> That works fine, I can see all the records, however I want to set the
    >> listbox to the name of the logged in user.
    >>
    >> I have done this:
    >>
    >> String ln = Page.User.Identity.Name;
    >> for (int i=0;i<lstNames.Items.Count;i++)
    >> {
    >> //Response.Write(" checking " +
    >> lstNames.Items.Value.ToString().ToLower() + " against " +
    >> ln.ToLower());
    >> if (lstNames.Items.Value.ToString().ToLower() ==
    >> ln.ToLower())
    >> {
    >> Response.Write("Setting item " + i + " as
    >> selected.");
    >> lstNames.SelectedIndex = i;
    >>
    >> }
    >> }
    >>
    >> My response.write says it is setting the index to 20 (which is correct
    >> for
    >> my username) however the listbox still shows the first name in the list.
    >> Do I need to do something to force the listbox to redraw or is there some
    >> other way? Seems like it should be so simple (and probably is...)!
    >>
    >> Thanks
    >>
    >> James

    >
    > James, with all due respect you seem seriously confused about how web
    > applications work. You must study it properly, in particular the
    > execution model and how to control the property values and appearance
    > of web server controls. Your use of response.write is totally
    > inappropriate - once you've assigned values to the control items the
    > rendering will take care of itself.
    >
    > I and others could propose a programmatic solution to meet your
    > requirements but I fear it would be a waste of time.
    Jeremy, Dec 13, 2007
    #3
  4. JamesB

    JamesB Guest

    "Jeremy" <> wrote in message
    news:...
    > Phil, pardon me for butting in, I don't suppose you considered the
    > possibility that James' use of Response.Write was as a debugging tool, and
    > not meant for production? I can't say of course, not being James, but
    > couldn't I don't think it is useful to insult people.
    >
    > Jeremy


    Jeremy- you're right - the "reponse.write" was simply so I could check my
    lstNames.SelectedIndex = i; command was setting the index to the correct
    value I was expecting. I put that in when the listbox kept showing the first
    value. I probably should have explained that though :)

    Phil is right to a point - this is my first time messing around with web
    stuff, so there *is* new stuff to learn, but from what I can gather, setting
    the index of a control should still work?

    James
    JamesB, Dec 13, 2007
    #4
  5. JamesB

    JamesB Guest

    "JamesB" <> wrote in message
    news:4760f554$0$21094$...
    >
    > "Jeremy" <> wrote in message
    > news:...
    >> Phil, pardon me for butting in, I don't suppose you considered the
    >> possibility that James' use of Response.Write was as a debugging tool,
    >> and not meant for production? I can't say of course, not being James,
    >> but couldn't I don't think it is useful to insult people.
    >>
    >> Jeremy

    >
    > Jeremy- you're right - the "reponse.write" was simply so I could check my
    > lstNames.SelectedIndex = i; command was setting the index to the correct
    > value I was expecting. I put that in when the listbox kept showing the
    > first value. I probably should have explained that though :)
    >
    > Phil is right to a point - this is my first time messing around with web
    > stuff, so there *is* new stuff to learn, but from what I can gather,
    > setting the index of a control should still work?
    >



    Some extra info - if I reponse.write the selecteditem.value, it is actually
    the correct data - it's just the listbox doesn't actually show the relevant
    item.
    JamesB, Dec 13, 2007
    #5
  6. JamesB

    JamesB Guest

    "JamesB" <> wrote in message
    news:47600b73$0$8427$...
    >I am new to web stuff, having previously done forms based apps... anyway,
    >my web app currently has a listbox on a page that is populated from a
    >database table.
    > That works fine, I can see all the records, however I want to set the
    > listbox to the name of the logged in user.
    >
    > I have done this:
    >
    > String ln = Page.User.Identity.Name;
    > for (int i=0;i<lstNames.Items.Count;i++)
    > {
    > //Response.Write(" checking " +
    > lstNames.Items.Value.ToString().ToLower() + " against " +
    > ln.ToLower());
    > if (lstNames.Items.Value.ToString().ToLower() ==
    > ln.ToLower())
    > {
    > Response.Write("Setting item " + i + " as selected.");
    > lstNames.SelectedIndex = i;
    >
    > }
    > }
    >
    > My response.write says it is setting the index to 20 (which is correct for
    > my username) however the listbox still shows the first name in the list.
    > Do I need to do something to force the listbox to redraw or is there some
    > other way? Seems like it should be so simple (and probably is...)!
    >



    I think I've fixed this - I have un-databound the control, instead I fill it
    with data manually using a method in the .cs file. This is only called if
    IsPostBack is false. Seems to work...
    JamesB, Dec 13, 2007
    #6
  7. JamesB

    Phil H Guest

    On 13 Dec, 00:21, "Jeremy" <> wrote:
    > Phil, pardon me for butting in, I don't suppose you considered the
    > possibility that James' use of Response.Write was as a debugging tool, and
    > not meant for production? I can't say of course, not being James, but
    > couldn't I don't think it is useful to insult people.
    >
    > Jeremy
    >
    > "Phil H" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > On 12 Dec, 16:28, "JamesB" <> wrote:
    > >> I am new to web stuff, having previously done forms based apps... anyway,
    > >> my
    > >> web app currently has a listbox on a page that is populated from a
    > >> database
    > >> table.
    > >> That works fine, I can see all the records, however I want to set the
    > >> listbox to the name of the logged in user.

    >
    > >> I have done this:

    >
    > >> String ln = Page.User.Identity.Name;
    > >> for (int i=0;i<lstNames.Items.Count;i++)
    > >> {
    > >> //Response.Write(" checking " +
    > >> lstNames.Items.Value.ToString().ToLower() + " against " +
    > >> ln.ToLower());
    > >> if (lstNames.Items.Value.ToString().ToLower() ==
    > >> ln.ToLower())
    > >> {
    > >> Response.Write("Setting item " + i + " as
    > >> selected.");
    > >> lstNames.SelectedIndex = i;

    >
    > >> }
    > >> }

    >
    > >> My response.write says it is setting the index to 20 (which is correct
    > >> for
    > >> my username) however the listbox still shows the first name in the list.
    > >> Do I need to do something to force the listbox to redraw or is there some
    > >> other way? Seems like it should be so simple (and probably is...)!

    >
    > >> Thanks

    >
    > >> James

    >
    > > James, with all due respect you seem seriously confused about how web
    > > applications work. You must study it properly, in particular the
    > > execution model and how to control the property values and appearance
    > > of web server controls. Your use of response.write is totally
    > > inappropriate - once you've assigned values to the control items the
    > > rendering will take care of itself.

    >
    > > I and others could propose a programmatic solution to meet your
    > > requirements but I fear it would be a waste of time.- Hide quoted text -

    >
    > - Show quoted text -


    Jeremy, James

    If my post came across as insulting then I'm very sorry, that
    certainly wasn't intended. I'm afraid it's the limitations of this
    form of communication (not face to face) and easy to give the wrong
    impression.

    As to the problem James raised originally I have had time to look at
    it carefully and on reflection I now see more clearly how the response
    object was being used, so again sorry about that.

    However, I hope you will be pleased to know that I do have an answer
    as to why it wasn't working. Assigning a value to the selectedIndex
    property is fine but it depends critically on when it happens. That
    I'm afraid James is one of the quirks of web applications, they're not
    like windows apps. Web servers have a mind of their own when it comes
    to event handling.

    The answer is to do the assignment in the DropDownList "Databound"
    event. That occurs after the list has been loaded with data and
    control is reliquished to the programmer to modify its state if you
    wish. Perhaps you would like to give it a try.

    Phil
    Phil H, Dec 13, 2007
    #7
  8. JamesB

    JamesB Guest

    "Phil H" <> wrote in message
    news:...
    > Jeremy, James
    >
    > If my post came across as insulting then I'm very sorry, that
    > certainly wasn't intended. I'm afraid it's the limitations of this
    > form of communication (not face to face) and easy to give the wrong
    > impression.

    That's ok - I'm aware it's all to easy for stuff to get misinterpreted in
    this sort of channel!


    > As to the problem James raised originally I have had time to look at
    > it carefully and on reflection I now see more clearly how the response
    > object was being used, so again sorry about that.
    >
    > However, I hope you will be pleased to know that I do have an answer
    > as to why it wasn't working. Assigning a value to the selectedIndex
    > property is fine but it depends critically on when it happens. That
    > I'm afraid James is one of the quirks of web applications, they're not
    > like windows apps. Web servers have a mind of their own when it comes
    > to event handling.

    Makes sense - I guess there are 2 points of action to worry about as well,
    the client end and the server end...

    > The answer is to do the assignment in the DropDownList "Databound"
    > event. That occurs after the list has been loaded with data and
    > control is reliquished to the programmer to modify its state if you
    > wish. Perhaps you would like to give it a try.
    >


    OK, that seems a good explanation- I'll take a look.
    Thanks for your help!
    JamesB, Dec 14, 2007
    #8
  9. JamesB

    Jeremy Guest

    Phil, I appreciate your response. I was a little testy myself, having been
    dissed a couple of times in as many days for merely being a newbie, and
    maybe I over-reacted. Must be the season!

    Jeremy

    "JamesB" <> wrote in message
    news:4762463c$0$13934$...
    >
    > "Phil H" <> wrote in message
    > news:...
    >> Jeremy, James
    >>
    >> If my post came across as insulting then I'm very sorry, that
    >> certainly wasn't intended. I'm afraid it's the limitations of this
    >> form of communication (not face to face) and easy to give the wrong
    >> impression.

    > That's ok - I'm aware it's all to easy for stuff to get misinterpreted in
    > this sort of channel!
    Jeremy, Dec 15, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RockNRoll
    Replies:
    3
    Views:
    7,340
  2. Simon Prince
    Replies:
    2
    Views:
    3,328
    =?Utf-8?B?TWljaGFlbCBUYXlsb3I=?=
    Oct 19, 2004
  3. Joseph T.S.Lim

    Select datagrid item programatically in ASP.NET

    Joseph T.S.Lim, Jul 1, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    267
    Teemu Keiski
    Jul 1, 2003
  4. rpress

    How to deselect item on single select listbox?

    rpress, Jan 3, 2006, in forum: ASP .Net Web Controls
    Replies:
    5
    Views:
    1,459
    rpress
    Jan 9, 2006
  5. hiroshi ochi
    Replies:
    3
    Views:
    299
    Hiroshi Ochi
    May 13, 2004
Loading...

Share This Page