Solved

Dropdown list - set selected item from query

Posted on 2013-06-05
4
254 Views
Last Modified: 2013-06-18
Hi,

I have a .net page with a ddl.
The datasource query for the ddl is
select tbGroupDiscount.iGroupDiscountid, vGroupname,iProductid from tbGroupDiscount left join tbProductDiscount
on tbProductDiscount.iGroupDiscountid = tbGroupDiscount.iGroupDiscountid and iProductid = @iProductid where iClientid = @iClientid order by vGroupname

 <asp:DropDownList ID="ddlGroupDiscount" runat="server" AutoPostBack="true" AppendDataBoundItems="true"   DataSourceID="GetGroupDiscount" DataTextField="vGroupname" DataValueField="iGroupDiscountid" >
                <asp:ListItem Value="0" Text="Please Select"></asp:ListItem>

after I call databind in my cs, I wish to select an item if there's a value for productid returned in the query
eg query could return

1, group1, null
2, group2, null
3, group3, 99
4, group4, null

null/99 being the productid from the query above.. so if the query has a value for productid I'd like that item to be selected

any ideas welcome?

Cheers
0
Comment
Question by:louise_8
  • 2
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
Roopesh Reddy earned 125 total points
ID: 39225724
Hi,

You may have loop through the values returned from the database and check for the value and update the value as selected for the DropDownList.

You can set the value - DropDownList.SelectedValue - http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.selectedvalue.aspx

Hope it helps u...
0
 

Author Comment

by:louise_8
ID: 39240115
Thanks
I suppose I'm confused because my ddl has a datasource and binds as the page is run
So because I dont create the listitems in the code behind does that mean I need to bind using the code behind or is there a way to select after its initallu bound?

Cheers
0
 
LVL 23

Assisted Solution

by:Roopesh Reddy
Roopesh Reddy earned 125 total points
ID: 39253992
Hi,

So, if the data is already binded, then you may have to loop through the list items and select the desired option!

Hope it helps u...
0
 

Author Closing Comment

by:louise_8
ID: 39258118
thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now