Hi,
I have an issue when setting the value of an UnboundCheckBoxField from code-behind. Here is some sample code:
Markup:
<%@ Page Language="C#" AutoEventWireup="false" CodeBehind="Default.aspx.cs" Inherits="UnboundCheckBoxFields._Default" %><%@ Register Assembly="Infragistics35.Web.v11.2, Version=11.2.20112.1019, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb" Namespace="Infragistics.Web.UI" TagPrefix="ig" %><%@ Register Assembly="Infragistics35.Web.v11.2, Version=11.2.20112.1019, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb" Namespace="Infragistics.Web.UI.GridControls" TagPrefix="ig" %><!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></title></head><body> <form id="form1" runat="server"> <div> <ig:WebScriptManager ID="WebScriptManager1" runat="server"> </ig:WebScriptManager> <ig:WebDataGrid ID="WebDataGrid1" runat="server" Height="350px" Width="400px" AutoGenerateColumns="false"> <Columns> <ig:UnboundCheckBoxField Key="Check" ></ig:UnboundCheckBoxField> <ig:BoundDataField Key="Value" DataFieldName="BoundFieldName" ></ig:BoundDataField> </Columns> </ig:WebDataGrid> </div> <asp:Button ID="btnPostBack" runat="server" /> </form></body></html>
Code-behind:
public partial class _Default : System.Web.UI.Page { protected override void OnInit(EventArgs e) { base.OnInit(e); Load += Page_Load; } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { List<MyClass> list = new List<MyClass>(); for (int i = 0; i < 10; i++) { list.Add(new MyClass(i)); } WebDataGrid1.DataSource = list; WebDataGrid1.DataBind(); foreach (GridRecord gridRecord in WebDataGrid1.Rows) { gridRecord.Items[0].Value = Convert.ToInt32(gridRecord.Items[1].Text) % 2 == 0; } } } } public class MyClass { public int BoundFieldName { get; set; } public MyClass(int boundFieldValue) { BoundFieldName = boundFieldValue; } }
The rows with values 0,2,4,6,8 are checked, but doing postback by clicking the button removes all rows from the grid. If I set EnableDataViewState to true the data is displayed after postback, but no rows are checked, neither on initial load nor after postback.
Is there another way to solve this?
I am using version 11.2.20112.1019.
Hi 555nase,
I noticed that you do not have a DataKeyFields property set. This is needed to properly store the unbound values. Once I set it "BoundFieldName" and set data view state to true, I got the values persisting over the postback.
regards,David Young
Thank you,now it works!