Difference between DataTextField and DataValue Field?

Posted by Arjunan_Csharp under ASP.NET on 11/22/2012 | Points: 10 | Views : 4041 | Status : [Member] | Replies : 3
Difference between DataTextField and DataValue Field?.Please tell me the difference and show a simple example using dropdownList.




Responses

Posted by: Sandhyab on: 11/22/2012 [Member] Starter | Points: 25

Up
0
Down
The DataTextField is the "Text" that is displayed to the user.
The DataValueField is the selection value associated with the displayed text.
Here example is binding enum values to the dropdown.
protected void Page_Load(object sender, EventArgs e)

{
Dictionary<string, int> CustomerTypeList = new Dictionary<string, int>();

FieldInfo fi;
DescriptionAttribute da;
foreach (colors enumValue in Enum.GetValues(typeof(colors)))
{
fi = typeof(colors).GetField((enumValue.ToString()));
da = (DescriptionAttribute)Attribute.GetCustomAttribute(fi,typeof(DescriptionAttribute));
if (da != null)
{
CustomerTypeList.Add(da.Description, (int)enumValue);
}
}
DropDownList1.DataSource = CustomerTypeList;
DropDownList1.DataTextField = "Key";
DropDownList1.DataValueField = "Value";
DropDownList1.DataBind();
}

public enum colors
{
[DescriptionAttribute("Blue")]
Blue = 0,

[DescriptionAttribute("Green")]
Green = 1,

[DescriptionAttribute("Voilet")]
Voilet = 2,

[DescriptionAttribute("BabyPink")]
BabyPink=4,

[DescriptionAttribute("Purple")]
Purple=3
}

Output:
Items in drop down will be,
Blue
Green
Voilet
Purple
BabyPink





Arjunan_Csharp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: aswinialuri-19361 on: 11/23/2012 [Member] Starter | Points: 25

Up
0
Down
1) DataTextFiled: The text that is displayed in the dropdown for the user to select.
2) DataValueField : The Value for that Item/ Text displayed which can be used by the programming logic .

Mark as Answer if it helps you
Thanks&Regards
Aswini Aluri

Arjunan_Csharp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vivekjj on: 11/23/2012 [Member] Starter | Points: 25

Up
0
Down
DataTextFiled : its used for which you need to show in dropdown
DataValueField : its used for to store the values for the shown text in dropdown

if u need to get some particular values from that dropdown means u can use the DataValueField .Suppose if u use DataTextFiled means for ex: if two names are same means its have some trouble to use. so if u use DataValueField means u can neglect that trouble

Arjunan_Csharp, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response