Solved

Dropdown list - set selected item from query

Posted on 2013-06-05
4
287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

617 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