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

selectedvalue not working..

P: 65

Currently i am using a dropdowlist which is actually databinded from a the datatbase. During my page load and by giving the !ispostback im binding the values into my DDL..

i get to this page from a previuos one using a query string Featureid=1. So after my DDL has been populated i use the following code:

if (Request.QueryString["FeatureId"] != null && Request.QueryString["FeatureId"].ToString() != "")
dropTopArea.SelectedValue = GetName(Convert.ToInt16(FeatureId));// Droptoparea is the id of the DDL.


where getname actually goes into the database and searches for the feature name using the FeatureId and returns the FeatureName. I debugged to check if it was reurning the right value and it was doing so.

However my DDL always loads up with the first element and not the one specified in the QueryString.

Could anyone help
Apr 26 '07 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 5K+
P: 9,731
Hi Nitinkcv,

You aren't setting the selected value properly.
I've provided a small snippet of code to help you get on track.
Its written in VB but the same concepts apply to your C# application.

Expand|Select|Wrap|Line Numbers
  1. Public Shared Sub SetDdlSelectedValue(ByVal theList As System.Web.UI.WebControls.ListControl, ByVal selectedValue As String)
  2.    'Sub Description:
  3.    '   This sub is responsible for setting the selected value in the dropDownList provided.
  4.    'Parameters:
  5.    '   theList: the DropDownList
  6.    '   SelectedValue: the value to be selected in the dropDownList
  7.    'Retrun Value:
  8.    '   Although this doesn't return anything, the dropDownList passed in is passed in by its reference pointer...
  9.    '   which means it is modified to contain a highlighted value of that supplied
  11.    Try
  12.        theList.ClearSelection()
  13.        theList.Items.FindByText(selectedValue).Selected = True
  14.    Catch ex As Exception
  16.    End Try
  18.  End Sub
Please note that the System.Web.UI.WebControls.ListControl is the base class for a DropDownList. Therefore you can pass this function a dropDownList and the selection will still be set.

Hope this helps you!

Apr 26 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.