数据绑定的CheckBoxList [英] DataBound CheckBoxList

查看:73
本文介绍了数据绑定的CheckBoxList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Asp.Net编程的网站,并使用一个ListView显示数据。数据从一个的LinqDataSource来

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

在我的EditItemTemplate中我有包括一个的CheckBoxList:

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>

我在我的数据库称为Type列,它是一个TINYINT。因此,我可以说(字节)的eval(类型)。

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

但我怎么我的数据绑定的eval(类型)到的CheckBoxList所以如果的eval(类型)为3,那么最后两个项目选择?

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

我已经尝试设置结合在的CheckBoxList的OnLoad类型,然后设置所选项目的隐藏价值。但did'nt工作。

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.

推荐答案

这是应该做的方式,与hidded值绑定到类型,但在ListView的ItemDataBound事件。

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)

这篇关于数据绑定的CheckBoxList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆