Law & Legal & Attorney Wills & trusts

How to Create DropDownList With Key Search in C

    • 1). Define a "Dictionary" object in your C# code and assign a "key" and a "value" to each row of the dictionary. The "key" and "value" can come from a database or be hard-coded. The values are hard-coded in the example below:

      Dictionary<string, string> list = new Dictionary<string, string>();
      list.Add("key 1", "value 1");
      list.Add("key 2", "value 2");
      list.Add("key 3", "value 3");
      list.Add("key 4", "value 4");

    • 2). Define the "Drop-down List" object and set the "Dictionary" object named "list" as the datasource. Name the drop-down "ddl." Set the "DataTextField" of "ddl" to the "key" from "list" and the "DataValueField" to the "value" from "list."

      ddl.DataSource = list;
      ddl.DataTextField = "Key";
      ddl.DataValueField = "Value";
      ddl.DataBind();

    • 3). Search the "Dictionary" object using "ContainsKey" method. For example, you may want to search through the "Dictionary" to locate a specific value prior to adding it to the "drop-down list." "ContainsKey" is one of the fastest search methods of the "Dictionary" object.

      if (list.ContainsKey("key 2"))

    • 4). Search the "drop-down list" object using the "FindByValue" method. This search method is case-sensitive and searches for the entire search parameter in its entirety.

      if (ddl.Items.FindByValue("value 3") != null)
      {
      ddl.SelectedValue = "value 3";
      }

      If you entered "value" as the search parameter, null would have been returned, because there are no values in "ddl" that are equal to ONLY the word "value."

SHARE
RELATED POSTS on "Law & Legal & Attorney"
Estate Planning For A Loved One With Special Needs - Seven Factors to Consider
Estate Planning For A Loved One With Special Needs - Seven Factors to Consider
How Montana Living Trusts Are Handled
How Montana Living Trusts Are Handled
Basic Estate Planning - 14 Ways to Make a Mess of Your Estate
Basic Estate Planning - 14 Ways to Make a Mess of Your Estate
Know Your Rights To Legal Compensation In Australia
Know Your Rights To Legal Compensation In Australia
The Oyster Fisheries Act
The Oyster Fisheries Act
1040 Instructions for Rhode Island
1040 Instructions for Rhode Island
How to Invest in Condo Real Estate
How to Invest in Condo Real Estate
Formula to Find Total Bond Expenses
Formula to Find Total Bond Expenses
How to Get a Copy of a 1099 Form From 2009
How to Get a Copy of a 1099 Form From 2009
How do I Change Illinois Voter Address Online?
How do I Change Illinois Voter Address Online?
How to Write a Letter for a Parole Board Hearing
How to Write a Letter for a Parole Board Hearing
How to Determine Country of Origin From Barcodes
How to Determine Country of Origin From Barcodes
How to Find Georgia Property Survey Records
How to Find Georgia Property Survey Records
What Is a Legal Conflict of Interest?
What Is a Legal Conflict of Interest?
How to Create a Stronger Truss
How to Create a Stronger Truss
Moving to Avoid Estate Taxes and Income Taxes? Absolutely Says One Maryland Couple
Moving to Avoid Estate Taxes and Income Taxes? Absolutely Says One Maryland Couple
Tying the Financial Knot - What to Know Before You Get Married
Tying the Financial Knot - What to Know Before You Get Married
How to Get Copies of Legal Proceedings
How to Get Copies of Legal Proceedings
How to Record a Mortgage in Arkansas
How to Record a Mortgage in Arkansas
How to Create DropDownList With Key Search in C
How to Create DropDownList With Key Search in C
How Do I Force Dragon 7 to Write Numbers Numerically?
How Do I Force Dragon 7 to Write Numbers Numerically?

Leave Your Reply

*