Solved

Dropdown list - set selected item from query

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

752 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