user467384
user467384

Reputation: 1167

CheckBoxList Value gets overwritten by Text on DataBind ASP.NET web forms

I have a CheckBoxList that I want to populate using a collection of ListItems with Text and Values defined.

var temp = types.Select(x => new ListItem(x["Description"].ToString(), x["TypeCode"].ToString()));
chbox.DataSource = temp;
chbox.DataBind();

The ListItems in temp have the correct Text and Value property values, but after chbox.DataBind(), all of the Value properties are populated with the Text property value.

So if the ListItems in temp look like

Text    Value
LetterA A
LetterB B

The ListItems in chbox.Items look like

Text    Value
LetterA LetterA
LetterB LetterB

Upvotes: 1

Views: 1001

Answers (1)

Nilesh Thakkar
Nilesh Thakkar

Reputation: 2895

You may want to define

 DataTextField="TextField" DataValueField="ValueField"

as shown below:

 <asp:CheckBoxList ID="CheckBoxList1" runat="server" DataTextField="TextField" DataValueField="ValueField">
        </asp:CheckBoxList>

Please check out DataTextField and DataValueField

Upvotes: 3

Related Questions