Data Binding Programatically to a CheckBoxList

Posts   
 
    
Posts: 24
Joined: 19-Feb-2007
# Posted on: 19-Feb-2007 22:54:14   

Asp.net 2.0 LLBL 2.0.0.0 SQL 2000

Hello,

When I bind to the CheckBoxList in the ASPX page the checkbox DataValueField properly is properly set to the TaskID

<asp:CheckBoxList ID="CheckBoxList1" runat="server" DataTextField="Task" DataValueField="TaskId" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged" AutoPostBack="True" ></asp:CheckBoxList>

    <llblgenpro:LLBLGenProDataSource id="LLBLGenProDataSource2" runat="server" DataContainerType="EntityCollection"
    EntityCollectionTypeName="DAL.CollectionClasses.TaskCollection, App_Code._o46fjru">
</llblgenpro:LLBLGenProDataSource>

----- But when I try the same thing programatically and add a Ipredicate filter to the ----

protected void CheckListBox1_LoadData() { ArrayList alExistingTasks = GetExistingTasks(Request.QueryString["processID"]); // get the task data TaskCollection tc = new TaskCollection(); // create the filter IPredicateExpression filter = new PredicateExpression();

    // set filter excluding tasks that are already assigned to process
    foreach (int x in alExistingTasks)
    {           
        filter.Add(TaskFields.TaskId != x);
    }       
    // apply filter


    tc.GetMulti(filter);
    // set the datasource
    CheckBoxList1.DataSource = tc;
    // bind data to control
    CheckBoxList1.DataTextField = "Task";

    CheckBoxList1.DataValueField = "TaskId";

    CheckBoxList1.DataBind();

    GridView1.DataSource = tc;
    GridView1.DataBind();

}

The list populates and filters properly but CheckBoxList1_SelectedIndexChanged errors with "Input string was not in a correct format. "

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{       
    ProcessTaskEntity pt = new ProcessTaskEntity();
    pt.ProcessId = Convert.ToInt32(Request.QueryString["processID"]);
    pt.TaskId = Convert.ToInt32(CheckBoxList1.SelectedValue);
    pt.Save();  
}

When I write the value CheckBoxList1.SelectedValue it is null, but when the ASPX databinding is used it has the integer value...

Posts: 1255
Joined: 10-Mar-2006
# Posted on: 19-Feb-2007 23:58:41   

Couple of things:

1) Your version of LLBLGen might be 2.0, but the build of the library is what is needed. Right click the support dll and choose properties.

2) You do not show the code in your page_load, which I am assuming calls this routine below. I am guessing you are calling it always, when it should be:

if (!IsPostBack) CheckListBox1_LoadData();

Without that it reloads the checkbox list and the selected value should be null!

3) If you build an array of integers, then you can simply do

int[] alExistingTasks = GetExistingTasks(Request.QueryString["processID"]);

tc.GetMulti(TaskFields.TaskId != alExistingTasks);

hope this helps!

Posts: 24
Joined: 19-Feb-2007
# Posted on: 20-Feb-2007 00:28:30   

Thanks ! That was it... I was an Classic ASP pro, but am now learning C# and .Net, Thanks for the help. It sucks to know what needs to be done but no know how to do it... =) Also thanks for the code improvement example on my IPredicate.