HighTechTalks DotNet Forums  

select listbox item programatically not working?

ASP.net Web Controls microsoft.public.dotnet.framework.aspnet.webcontrols


Discuss select listbox item programatically not working? in the ASP.net Web Controls forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
JamesB
 
Posts: n/a

Default select listbox item programatically not working? - 12-12-2007 , 10:28 AM






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[i].Value.ToString().ToLower() + " against " + ln.ToLower());
if (lstNames.Items[i].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


Reply With Quote
  #2  
Old   
Phil H
 
Posts: n/a

Default Re: select listbox item programatically not working? - 12-12-2007 , 03:06 PM






On 12 Dec, 16:28, "JamesB" <jam... (AT) somewhere (DOT) com.net.com.net> wrote:
Quote:
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[i].Value.ToString().ToLower() + " against " + ln.ToLower());
if (lstNames.Items[i].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.


Reply With Quote
  #3  
Old   
Jeremy
 
Posts: n/a

Default Re: select listbox item programatically not working? - 12-12-2007 , 06:21 PM



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" <google (AT) philphall (DOT) me.uk> wrote

Quote:
On 12 Dec, 16:28, "JamesB" <jam... (AT) somewhere (DOT) com.net.com.net> 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[i].Value.ToString().ToLower() + " against " +
ln.ToLower());
if (lstNames.Items[i].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.


Reply With Quote
  #4  
Old   
JamesB
 
Posts: n/a

Default Re: select listbox item programatically not working? - 12-13-2007 , 03:06 AM




"Jeremy" <jeremy-nospam (AT) ninprodata (DOT) com> wrote

Quote:
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



Reply With Quote
  #5  
Old   
JamesB
 
Posts: n/a

Default Re: select listbox item programatically not working? - 12-13-2007 , 05:59 AM




"JamesB" <jamesb (AT) somewhere (DOT) com.net.com.net> wrote

Quote:
"Jeremy" <jeremy-nospam (AT) ninprodata (DOT) com> wrote in message
news:OO5VQ4RPIHA.5360 (AT) TK2MSFTNGP03 (DOT) phx.gbl...
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.



Reply With Quote
  #6  
Old   
JamesB
 
Posts: n/a

Default Re: select listbox item programatically not working? - 12-13-2007 , 08:31 AM




"JamesB" <jamesb (AT) somewhere (DOT) com.net.com.net> wrote

Quote:
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[i].Value.ToString().ToLower() + " against " +
ln.ToLower());
if (lstNames.Items[i].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...



Reply With Quote
  #7  
Old   
Phil H
 
Posts: n/a

Default Re: select listbox item programatically not working? - 12-13-2007 , 11:48 AM



On 13 Dec, 00:21, "Jeremy" <jeremy-nos... (AT) ninprodata (DOT) com> wrote:
Quote:
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" <goo... (AT) philphall (DOT) me.uk> wrote in message

news:e67ca42b-9fcb-4693-aa6f-a982fe7301f0 (AT) i3g2000hsf (DOT) googlegroups.com...



On 12 Dec, 16:28, "JamesB" <jam... (AT) somewhere (DOT) com.net.com.net> 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[i].Value.ToString().ToLower() + " against " +
ln.ToLower());
if (lstNames.Items[i].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


Reply With Quote
  #8  
Old   
JamesB
 
Posts: n/a

Default Re: select listbox item programatically not working? - 12-14-2007 , 03:00 AM




"Phil H" <google (AT) philphall (DOT) me.uk> wrote

Quote:
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!


Quote:
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...

Quote:
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!



Reply With Quote
  #9  
Old   
Jeremy
 
Posts: n/a

Default Re: select listbox item programatically not working? - 12-14-2007 , 08:45 PM



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" <jamesb (AT) somewhere (DOT) com.net.com.net> wrote

Quote:
"Phil H" <google (AT) philphall (DOT) me.uk> wrote in message
news:e277823b-59c1-4e3e-8d8b-b980b43ba78a (AT) t1g2000pra (DOT) googlegroups.com...
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!


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.