【问题标题】:Gridview from checkboxlist复选框列表中的 Gridview
【发布时间】:2012-12-17 13:36:04
【问题描述】:

我有一个动态创建的Checkboxlist(我使用的是动态数据,我说的是多对多编辑.aspx)。 我在Checkboxlist 中添加了 20 000 多个项目,我需要为此进行分页。

我认为将此checkboxlist 绑定到gridview 会成功吗?我该怎么做?

编辑:我已经阅读了很多关于这个主题的文章,但我找不到任何好的东西。当我尝试将Checkboxlist's source 放入我的GridView.DatSource 时,它会抛出OutOfMemory 异常

EDIT2:我正在尝试这样的事情,但现在,因为我的 checkboxlist 它在 gridview 内,我不知道如何填充它了

代码示例:

asp:GridView ID="GridView1" runat="server">
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
      <asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatColumns="3" OnDataBound="CheckBoxList1_DataBound" />
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>

EDIT3:

我的代码 aspx.cs

  public partial class ManyToMany_EditField :  System.Web.DynamicData.FieldTemplateUserControl
  {

 public void Page_Load(object sender, EventArgs e)

   {
    // Register for the DataSource's updating event
    EntityDataSource ds = (EntityDataSource)this.FindDataSourceControl();

    // This field template is used both for Editing and Inserting
    ds.Updating += new EventHandler<EntityDataSourceChangingEventArgs>(DataSource_UpdatingOrInserting);
    ds.Inserting += new EventHandler<EntityDataSourceChangingEventArgs>(DataSource_UpdatingOrInserting);


}

private void DataSource_UpdatingOrInserting(object sender, EntityDataSourceChangingEventArgs e)
{
    MetaTable childTable = ChildrenColumn.ChildTable;

    // Comments assume employee/territory for illustration, but the code is generic

    // Get the collection of territories for this employee
    RelatedEnd entityCollection = (RelatedEnd)Column.EntityTypeProperty.GetValue(e.Entity, null);

    // In Edit mode, make sure it's loaded (doesn't make sense in Insert mode)
    if (Mode == DataBoundControlMode.Edit && !entityCollection.IsLoaded)
    {
        entityCollection.Load();
    }

    // Get an IList from it (i.e. the list of territories for the current employee)
    // REVIEW: we should be using EntityCollection directly, but EF doesn't have a
    // non generic type for it. They will add this in vnext
    IList entityList = ((IListSource)entityCollection).GetList();

    // Go through all the territories (not just those for this employee)
    foreach (object childEntity in childTable.GetQuery(e.Context))
    {

        // Check if the employee currently has this territory
        bool isCurrentlyInList = entityList.Contains(childEntity);

        // Find the checkbox for this territory, which gives us the new state
        string pkString = childTable.GetPrimaryKeyString(childEntity);
        ListItem listItem = CheckBoxList1.Items.FindByValue(pkString);
        if (listItem == null)
            continue;

        // If the states differs, make the appropriate add/remove change
        if (listItem.Selected)
        {
            if (!isCurrentlyInList)
                entityList.Add(childEntity);
        }
        else
        {
            if (isCurrentlyInList)
                entityList.Remove(childEntity);
        }
    }
}

protected void CheckBoxList1_DataBound(object sender, EventArgs e)
{
    MetaTable childTable = ChildrenColumn.ChildTable;
    var a = childTable.CreateContext();
    var dataSource = childTable.GetQuery().AsQueryable();
    var Context = new testEntities();
    var daZtaSource = Context.GetType();

    GridView1.DataSource = dataSource;

    // Comments assume employee/territory for illustration, but the code is generic

    IList entityList = null;
    ObjectContext objectContext = null;

    if (Mode == DataBoundControlMode.Edit)
    {
        object entity;
        ICustomTypeDescriptor rowDescriptor = Row as ICustomTypeDescriptor;
        if (rowDescriptor != null)
        {
            // Get the real entity from the wrapper
            entity = rowDescriptor.GetPropertyOwner(null);
        }
        else
        {
            entity = Row;
        }

        // Get the collection of territories for this employee and make sure it's loaded
        RelatedEnd entityCollection = Column.EntityTypeProperty.GetValue(entity, null) as RelatedEnd;
        if (entityCollection == null)
        {
            throw new InvalidOperationException(String.Format("The ManyToMany template does not support the collection type of the '{0}' column on the '{1}' table.", Column.Name, Table.Name));
        }
        if (!entityCollection.IsLoaded)
        {
            entityCollection.Load();
        }

        // Get an IList from it (i.e. the list of territories for the current employee)
        // REVIEW: we should be using EntityCollection directly, but EF doesn't have a
        // non generic type for it. They will add this in vnext

        entityList =((IListSource)entityCollection).GetList();


        // Get the current ObjectContext
        // REVIEW: this is quite a dirty way of doing this. Look for better alternative
        ObjectQuery objectQuery = (ObjectQuery)entityCollection.GetType().GetMethod(
            "CreateSourceQuery").Invoke(entityCollection, null);
        objectContext = objectQuery.Context;
    }

    // Go through all the territories (not just those for this employee)
   foreach (object childEntity in childTable.GetQuery(objectContext))
    {
        MetaTable actualTable = MetaTable.GetTable(childEntity.GetType());
        // Create a checkbox for it
        ListItem listItem = new ListItem(
            actualTable.GetDisplayString(childEntity),
            actualTable.GetPrimaryKeyString(childEntity));

        // Make it selected if the current employee has that territory
        if (Mode == DataBoundControlMode.Edit)
        {
            listItem.Selected = entityList.Contains(childEntity);
        }

        CheckBoxList1.Items.Add(listItem);
    }

}

public override Control DataControl
{
    get
    {
        return CheckBoxList1;
    }
}

}

我的代码:aspx

  <%@ Control Language="C#" CodeFile="ManyToMany_Edit.ascx.cs" Inherits="ManyToMany_EditField" %>

<asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatColumns="3" OnDataBound="CheckBoxList1_DataBound" />

【问题讨论】:

标签: c# asp.net gridview dynamic-data


【解决方案1】:

为什么要在 gridview 中添加一个复选框列表?我认为您可以用复选框替换您的复选框列表,并添加一个包含您要显示的数据的列。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" showheader="false" allowpaging="true">
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
      <asp:CheckBox ID="CheckBoxList1" runat="server" checked='<%# Eval("myColumn")%>'/>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="cbxLabel" />
    </Columns>
    </asp:GridView>

你会得到你想要的分页。

并且不要忘记在页面更改期间保存选中的复选框并恢复它。

要对结果进行分页,请查看此线程: How to paginate a gridview and SQL custom query with ROW_NUMBER

【讨论】:

  • 我尝试(第一步)用正确的数据源填充该网格视图(因为我没有要使用的固定数据源;每个页面都不同,这就像所有这些的模板) 但它会引发 OutOfMemoryException
  • 您应该查看您的查询并仅获取必需品数据。例如,如果您希望按页显示 50 个项目,则第一个查询应该只返回第 2 页的第 51 到 100 个项目。
  • 你在gridview中有一个checkboxlist,你在treeview的数据绑定中设置了gridview的数据源???而且您从不调用 databind() 函数..似乎我们有一个循环,不是吗?我认为我提出的解决方案更简单!
猜你喜欢
  • 1970-01-01
  • 2010-09-19
  • 1970-01-01
  • 1970-01-01
  • 2011-08-27
  • 2014-06-29
  • 1970-01-01
  • 2015-09-17
  • 1970-01-01
相关资源
最近更新 更多