Posted: 10/22/2010
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??
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.
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.
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 working
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 workingMay 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 memory
user="mohit kumar"]
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 memory
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 ;).
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?
mohit said: user="mohit kumar"]lol..I am also gonna edit my reply:)
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
hajan said: user="Hajan Selmani"]Better! Becuase adding unnecessary additionan LINE to your reply is waste of memory space in the CodeASP.NET Community
user="Hajan Selmani"]Better! Becuase adding unnecessary additionan LINE to your reply is waste of memory space in the CodeASP.NET Community
Nice.Yes Hajan I will take care of it
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?
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.
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
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.
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 blog
I have bookmarked too.
Thanks Santhosh