views:

853

answers:

2

Hi,

I have a website programmed in Asp.Net and use a ListView for displaying data. The data is coming from a LinqDataSource.

In my EditItemTemplate I have a CheckBoxList which consist of:

<asp:CheckBoxList runat="server" ID="TypeCheckBoxList" RepeatColumns="2">
 <asp:ListItem Value="128">6.-10. klasse<br />Norddjurs vejleder</asp:ListItem>
 <asp:ListItem Value="64">6.-10. klasse<br />Syddjurs vejleder</asp:ListItem>
 <asp:ListItem Value="32">Gået ud af skolen<br/>Norddjurs vejleder</asp:ListItem>
 <asp:ListItem Value="16">Gået ud af skolen<br/>Syddjurs vejleder</asp:ListItem>
 <asp:ListItem Value="8">Ekstra støtte<br/>Norddjurs vejleder</asp:ListItem>
 <asp:ListItem Value="4">Ekstra støtte<br />Syddjurs vejleder</asp:ListItem>
 <asp:ListItem Value="2">Kontakt</asp:ListItem>
 <asp:ListItem Value="1">Om os<br />Medarbejdere</asp:ListItem>
</asp:CheckBoxList>

I have a column called Type in my db and it is a tinyint. Therefore I can say (byte)Eval("Type").

But how do I databind my Eval("Type") to the CheckBoxList so if Eval("Type") is 3, then the two last items are selected?

I have tried setting a hidden value which binds to Type and then in the CheckBoxList OnLoad setting the selected items. But that did'nt work.

A: 

First you should write a javascript function like this

function Selected(value,type)

{

   if(value<type)
    return true;
   else
    return false;
}



<asp:ListItem Value="32" Selected= javascript:function Selected(32,Eval("Type"))>Gået ud af skolen<br />Norddjurs vejleder</asp:ListItem>    
<asp:ListItem Value="16" Selected= javascript:function Selected(16,Eval("Type")>Gået ud af skolen<br />Syddjurs vejleder</asp:ListItem>

little bit modification may be required to finalize it..major focus on Selected attribute

Jaswant Agarwal
okay, I will try that. However, I would like a server-side solution better.
lasseespeholt
I don't think it works. Are you sure javascript works in that attribute?
lasseespeholt
+2  A: 

That's the way to do it, with the hidded value binding to Type, but on the ItemDataBound event of the ListView.

So the event would look something like this:

protected void ListViewId_ItemDataBound (object sender, ListViewItemEventArgs e)
{
    HiddenField hdfType = (HiddenField)e.Item.FindControl("hdfType");
    CheckBoxList TypeCheckBoxList = (HiddenField)e.Item.FindControl("TypeCheckBoxList");

    // and you put the hidden just for EditItem and do:
    if (hdfType != null)
        foreach (ListItem item in TypeCheckBoxList.Items)
            if (int.Parse(item.Value) < int.Parse(hdfType.Value))
                item.Selected = true;
}

(I wrote all of this from my head, so there might be some small mistakes)

Dan Dumitru
I'll look into it :) but it is only for EditItem there is a checkboxlist. Not all items.
lasseespeholt
You can put the HiddenField only for the EditItem.And then do all the checking/selecting in the braces after:if (hdfType != null){ ...}
Dan Dumitru
It sure does work. Thank you very much! :)
lasseespeholt
You're welcome! :)
Dan Dumitru