Question

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.

Was it helpful?

Solution

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)

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top