2013-04-15 23 views
4

我必须在asp.net中为其创建一个Class(MultiGrid)代码(C#)的Multi-select DataGrid。多选数据网格

namespace Portal 
{ 
public class MultiGrid : DataGrid 
{ 
    // Constructor that sets some styles and graphical properties  
    public MultiGrid() 
    { 
     AllowMultiSelect = false; 
     AllowMultiSelectFooter = false; 

     // Set event handlers 
     Init += new EventHandler(OnInit); 
     ItemCreated += new DataGridItemEventHandler(OnItemCreated); 
    } 

    // PROPERTY: SelectedItems 
    public ArrayList SelectedItems 
    { 
     get 
     { 
      if (!AllowMultiSelect) return null; 

      ArrayList a = new ArrayList(); 
      foreach (DataGridItem dgi in Items) 
      { 
       CheckBox cb = (CheckBox)dgi.Cells[0].Controls[0]; 
       if (cb.Checked) 
        a.Add(dgi); 
      } 
      return a; 
     } 
    } 

    // PROPERTY: AllowMultiSelect 
    public bool AllowMultiSelect = false; 

    // PROPERTY: AllowMultiSelectFooter 
    public bool AllowMultiSelectFooter = false; 

    // METHOD: ClearSelection 
    public void ClearSelection() 
    { 
     foreach (DataGridItem dgi in Items) 
     { 
      CheckBox cb = (CheckBox)dgi.Cells[0].Controls[0]; 
      cb.Checked = false; 
     } 
    } 

    /////////////////////////////////////////////////////////////////// 
    // Event Handlers  // EVENT HANDLER: Init    
    private void OnInit(Object sender, EventArgs e) 
    { 
     // Add a templated column that would allow for selection. 
     // The item template contains a checkbox. It also features a 
     // templated footer containing links for Unselect/Select all 
     if (AllowMultiSelect) 
      AddSelectColumn(); 
    } 

    // EVENT HANDLER: Deselect 
    private void OnDeselect(Object sender, EventArgs e) 
    { 
     ClearSelection(); 
    } 

    // EVENT HANDLER: ItemCreated    
    private void OnItemCreated(Object sender, DataGridItemEventArgs e) 
    { 
     // Get the newly created item 
     ListItemType itemType = e.Item.ItemType; 

     /////////////////////////////////////////////////////////////// 
     // FOOTER 
     if (itemType == ListItemType.Footer && AllowMultiSelectFooter 
      && AllowMultiSelect) 
     { 
      // Look for a link button called "lnkSelect" in the context 
      // of the grid item that represents the footer 
      LinkButton lb = (LinkButton) 
       e.Item.FindControl("lnkDeselect"); 

      // Now you hold the living instance of the link 
      // button in the footer and can bind it to any code in the 
      // context of the MultiGrid control 
      lb.Click += new EventHandler(OnDeselect); 

      // Force ShowFooter to true 
      ShowFooter = true; 

      // Removes all the cells but the first 
      TableCell cell = e.Item.Cells[0]; 
      for (int i = 1; i < Columns.Count; i++) 
      { 
       e.Item.Cells.RemoveAt(1); 
      } 
      cell.ColumnSpan = Columns.Count; 
     } 
    } 

    /////////////////////////////////////////////////////////////////// 
    // Helper Functions 

    private void AddSelectColumn() 
    { 
     // Create the new templated column 
     TemplateColumn tc = new TemplateColumn(); 
     tc.ItemStyle.BackColor = Color.SkyBlue; 
     tc.ItemTemplate = new SelectColumnTemplate(); 
     tc.FooterTemplate = new SelectFooterTemplate(); 
     Columns.AddAt(0, tc); 
    } 
} 
/////////////////////////////////////////////////////////////////////// 
// Template Classes 

public class SelectColumnTemplate : ITemplate 
{ 
    public void InstantiateIn(Control container) 
    { 
     CheckBox cb = new CheckBox(); 
     container.Controls.Add(cb); 
    } 
} 

public class SelectFooterTemplate : ITemplate 
{ 
    public void InstantiateIn(Control container) 
    { 
     LinkButton lb = new LinkButton(); 
     lb.Text = "Deselect all"; 
     lb.ID = "lnkDeselect"; 
     container.Controls.Add(lb); 
    } 
} 
} 

问题:我需要在aspx文件访问此类,但它显示“元素‘多重网格’不是一个已知元素”。我的aspx代码。

<%@ Register TagPrefix="expo" Namespace="Portal" Assembly="MultiGrid" %> 
<expo:MultiGrid id="grid" runat="server" 
AutoGenerateColumns="false" 
AllowMultiSelect="true" 
AllowMultiSelectFooter="true" 
font-size="x-small" font-names="verdana" 
BorderStyle="solid" BorderWidth="1" 
GridLines="both"> 

任何帮助将不胜感激。

回答

0

尝试重建您的解决方案,然后关闭Visual Studio,然后再次打开以查看它是否有效。

如果没有尝试下面的链接:

Resolving Validation Element

+0

它不工作这个问题仍然呈现“元素‘多重网格’不是一个已知元素”。 –