Loading ...

Dictionary in c#

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  getting started / general asp.net   » Dictionary in c#

Dictionary in c#

Posts under the topic: Dictionary in c#

Posted: 10/22/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

Hello Experts,

I am binding a dropdownlist from Dictionary but i didn't get the output which I want.Kindly take a look-

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestQuestion.aspx.cs" Inherits="WebProjects.TestQuestion" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="ddlDate" runat="server" ></asp:DropDownList>
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Web.UI;

namespace WebProjects
{
    public partial class TestQuestion : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ddlDate.DataSource = GetDate();
            ddlDate.DataBind();
            
        }
        private Dictionary<string,int> GetDate()
        {
            Dictionary<string, int> date = new Dictionary<string, int>();
            for (int i = 1; i <= 31; i++)
            {
                date.Add(i.ToString(),i);
            }
            return date;
        }
    }
}


output - 

But  I want only "1" in dropdownlist. How I Can use DataTextField and DataSourceID??

 


Posted: 10/22/2010

Professional 8505  points  Professional
  • Joined on: 5/3/2010
  • Posts: 391
  Answered

Rewrite it like this:

 

        protected void Page_Load(object sender, EventArgs e)
        { 
ddlDate.DataSource = GetDate(); ddlDate.DataValueField = "Key"; ddlDate.DataTextField = "Value"; ddlDate.DataBind(); } private Dictionary<string, int> GetDate() { Dictionary<string, int> date = new Dictionary<string, int>(); for (int i = 1; i <= 31; i++) { date.Add(i.ToString(), i); } return date; }

I've only modified the Page_Load method where I've added ddlDate.DataValueField="Key" and ddlDate.DataTextField="Value" ;) - in order to tell DropDownList which one is Value and which Text.

Hope this helps.


Posted: 10/22/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

 

hajan said:

user="Hajan Selmani"]I've only modified the Page_Load method where I've added ddlDate.DataValueField="Key" and ddlDate.DataTextField="Value" ;) - in order to tell DropDownList which one is Value and which Text.

 

Thank you Hajan. Its workingSmile

 


Posted: 10/22/2010

Professional 8505  points  Professional
  • Joined on: 5/3/2010
  • Posts: 391

mohit said:

user="mohit kumar"]

 

hajan said:

user="Hajan Selmani"]I've only modified the Page_Load method where I've added ddlDate.DataValueField="Key" and ddlDate.DataTextField="Value" ;) - in order to tell DropDownList which one is Value and which Text.

Thank you Hajan. Its workingSmile

May I know why did you create instance of dictionary in page load method as I am returning dictionary from GetData method.Don't you think that its wasting of memoryWink

Remove it :).

I've just written the code quite fast and forgot to remove the additional instance which is not needed at all.

The post is edited now ;).


Posted: 10/22/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

In this situation I am binding from Dictionary.But if I am binding data from database(assume ID and Value are two columns ).So what is the syntax of DataTextField?

 


Posted: 10/22/2010

Professional 8505  points  Professional
  • Joined on: 5/3/2010
  • Posts: 391

 

mohit said:

user="mohit kumar"]

lol..I am also gonna edit my reply:)

 

Better! Becuase adding unnecessary additionan LINE to your reply is waste of memory space in the CodeASP.NET Community Cool Laughing Wink


Posted: 10/22/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

hajan said:

user="Hajan Selmani"]Better! Becuase adding unnecessary additionan LINE to your reply is waste of memory space in the CodeASP.NET Community Cool Laughing

Nice.Yes Hajan I will take care of itCool


Posted: 10/22/2010

Professional 8338  points  Professional
  • Joined on: 4/15/2009
  • Posts: 424
  Answered

mohit said:

user="mohit kumar"]But if I am binding data from database(assume ID and Value are two columns ).So what is the syntax of DataTextField?

Just set the database column field as the DataTextField. Check this out: Binding DropDownList, ListBox and CheckBoxList Control the ADO.NET way.


Posted: 10/22/2010

Lurker 50  points  Lurker
  • Joined on: 10/22/2010
  • Posts: 5
  Answered

If you want to show dates in DropDownList, you can also do it this way

ddlDate.DataSource = Enumerable.Range(1, 31);
ddlDate.DataBind();



Posted: 10/25/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

Vinz said:

user="Vincent Maverick Durano"]Just set the database column field as the DataTextField. Check this out: Binding DropDownList, ListBox and CheckBoxList Control the ADO.NET way.

Thank you Vinz for such a nice blogSmile

I have bookmarked too.


Posted: 10/25/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

Thanks SanthoshSmile


Page 1 of 1 (11 items)