Solved

Dropdown list - set selected item from query

Posted on 2013-06-05
4
242 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

705 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

18 Experts available now in Live!

Get 1:1 Help Now