select listbox item programatically not working?

J

JamesB

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
 
P

Phil H

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.
 
J

Jeremy

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 said:
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.
 
J

JamesB

Jeremy said:
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
 
J

JamesB

JamesB said:
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.
 
J

JamesB

JamesB said:
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...
 
P

Phil H

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




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
 
J

JamesB

Phil H said:
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!
 
J

Jeremy

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
 

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,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top