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

dropdownlist problem

P: n/a
Hi all,

Please forgive if this has been answered before, but I haven't been
able to find the solution. I'm working on a project for school that
will use a dropdownlist populated by data from a database. The
dropdownlist will use the onselectedindexchanged event to trigger a
subroutine that will populate a datagrid based on the selected item in
the dropdownlist.

I've got the dropdownlist and the sub to handle populating the
datagrid, but one other aspect of the problem is that all items are to
fire the event that loads the datagrid. The challenge is to have the
event fire when the first item is selected. But by default, the first
item of a datagrid is already selected and clicking on it won't fire
the onselectedidexchanged event. According to the instructor, "this
can be easily accomplished with one line of code, but it was not
covered during the course."

I've been scanning the web and all my books, but can't figure it out.
Any suggestions would be most appreciated...

Nov 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Well, I found a workaround...

if not page.ispostback then add a new item at index 0 with a text value
= "Select...", which becomes the default selected item. Next time
around, I dropdownlist1.items.removeat(0) if the text="Select..."

While this works, it's not the "one line of code" solution it's
supposed to be. I'm still hopeful for that easy one line solution...

Nov 19 '05 #2

P: n/a
I assume that you are populating the dropdown from a database with an SQL
statement. We use a union on our select something like

Select
0 as UserID,
" " as Display Name
Union
Select
....... Whatever your current SQL is

This creates a blank line at the beginning of the DropDown list.

I would be interested in the one line of code as well.

Steven

"br***********@highstream.net" wrote:
Well, I found a workaround...

if not page.ispostback then add a new item at index 0 with a text value
= "Select...", which becomes the default selected item. Next time
around, I dropdownlist1.items.removeat(0) if the text="Select..."

While this works, it's not the "one line of code" solution it's
supposed to be. I'm still hopeful for that easy one line solution...

Nov 19 '05 #3

P: n/a
This may not be the solution but what I do
in the page load

if not page.ispostback
.....
do the drop down list databinding
onselectedindexchanged(nothing,nothing)
end if

This is done in one line since the rest should be doing anways
James

br***********@highstream.net wrote in news:1115398279.674704.25720
@f14g2000cwb.googlegroups.com:
Hi all,

Please forgive if this has been answered before, but I haven't been
able to find the solution. I'm working on a project for school that
will use a dropdownlist populated by data from a database. The
dropdownlist will use the onselectedindexchanged event to trigger a
subroutine that will populate a datagrid based on the selected item in
the dropdownlist.

I've got the dropdownlist and the sub to handle populating the
datagrid, but one other aspect of the problem is that all items are to
fire the event that loads the datagrid. The challenge is to have the
event fire when the first item is selected. But by default, the first
item of a datagrid is already selected and clicking on it won't fire
the onselectedidexchanged event. According to the instructor, "this
can be easily accomplished with one line of code, but it was not
covered during the course."

I've been scanning the web and all my books, but can't figure it out.
Any suggestions would be most appreciated...


Nov 19 '05 #4

P: n/a
EJD
I may have misunderstood the question, but this might help. I have a
dropdownlist in a user control that displays a number of items, and
when an item is selected it redirects to a page that has several
datagrids based on the choice.
If(!IsPostBack)
{
// gets values from database
PopulateDDL():
DropDownList1.DataBind()
DropDownList1.Items.Insert(0, "Please select a
service...");
}
This add a dummy value at the top that means all of the other values
will cause a postback and whatever action I want. It's not elegant,
but it works. HTH.

Eric

Nov 19 '05 #5

P: n/a
EJD
I may have misunderstood the question, but this might help. I have a
dropdownlist in a user control that displays a number of items, and
when an item is selected it redirects to a page that has several
datagrids based on the choice.
If(!IsPostBack)
{
// gets values from database
PopulateDDL():
DropDownList1.DataBind()
DropDownList1.Items.Insert(0, "Please select a
service...");
}
This add a dummy value at the top that means all of the other values
will cause a postback and whatever action I want. It's not elegant,
but it works. HTH.

Eric

Nov 19 '05 #6

P: n/a
Thanks for the help!

I implemented a solution where if not page.ispostback, I added a blank
line at the top, forcing the user to make a selection. Then, if
page.ispostback, I simply eliminate the blank line. I have a feeling
that this is what the instructor was looking for, so that's what I'm
going with. :)

I'll keep you posted if the solution is different.

Brian

Nov 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.