C# 为什么自定义控件的组件没有启动?
我正在制作一个C# 为什么自定义控件的组件没有启动?,c#,asp.net,gridview,dynamic,custom-controls,C#,Asp.net,Gridview,Dynamic,Custom Controls,我正在制作一个GenericTable,作为GridView的自定义实现,它将显示插入的任何对象列表的值 要在aspx页面上使用控件,它需要是一个UserControl,因此GridView作为组件包含在通用表中: <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="GenericTable.ascx.cs" Inherits="CASH.WebApplication.Controls.GenericTable" %&g
GenericTable
,作为GridView
的自定义实现,它将显示插入的任何对象列表的值
要在aspx页面上使用控件,它需要是一个UserControl,因此GridView
作为组件包含在通用表中:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="GenericTable.ascx.cs" Inherits="CASH.WebApplication.Controls.GenericTable" %>
<div style="width: 100%; overflow: scroll">
<asp:GridView ID="grid" runat="server"></asp:GridView>
</div>
当我尝试激活新创建的GenericTable
时,在该代码之后,网格
为空
有没有一种方法可以初始化这个控件位于aspx代码中时发生的相同魔术
更新:可能问题在于如何在回发之间存储表,目前我正在使用会话,也许有更好的方法来记住用户输入?
整个通用代码:
using Project.DomainModel.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CASH.WebApplication.Controls
{
public partial class GenericTable : UserControl
{
private PropertyInfo[] properties;
//private GridView gridView;
private DataTable table = new DataTable();
private Dictionary<int, int> ingedrukt = new Dictionary<int, int>();
protected void Page_Init(object sender, EventArgs e)
{
grid.RowCommand += WeergaveDossiers_RowCommand;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
for (int i = 0; i < grid.Rows.Count; i++)
{
grid.Rows[i].Cells[0].ColumnSpan = 0;
}
}
else
{
properties = (PropertyInfo[])Session["properties"];
table = (DataTable)Session["table"];
ingedrukt = (Dictionary<int, int>)Session["ingedrukt"];
foreach (var knop in ingedrukt)
{
DetailRijToevoegen(knop.Key, knop.Value);
}
}
grid.DataBind();
}
protected void SaveInSession()
{
Session["properties"] = properties;
Session["table"] = table;
Session["ingedrukt"] = ingedrukt;
}
protected void WeergaveDossiers_RowCommand(object sender, GridViewCommandEventArgs e)
{
int row = int.Parse((string)e.CommandArgument) + 1;
int col = GetKolomIndex(e.CommandName) + 1;
if (ingedrukt.ContainsKey(row))
{
if (ingedrukt[row] != col)
{
//DetailRijVerwijderen(row);
//ingedrukt.Remove(row);
//ingedrukt[row] = col;
}
}
else
{
ingedrukt[row] = col;
}
//DetailRijToevoegen(row, col);
SaveInSession();
}
protected void DetailRijToevoegen(int row, int col)
{
var data = table.NewRow();
var child = new GenericTable();
child.grid = new GridView();
data[0] = child;
table.Rows.InsertAt(data, row);
grid.DataSource = table;
grid.DataBind();
var cells = grid.Rows[row].Cells;
// Only keep the first cell
while (cells.Count > 1)
{
cells.RemoveAt(1);
}
child.MaakTable(new List<object>() { table.Rows[row][col] });
grid.Columns[0].Visible = true;
grid.Rows[row].Cells[0].ColumnSpan = table.Columns.Count;
}
protected void DetailRijVerwijderen(int row)
{
}
protected int GetKolomIndex(string naam)
{
for (int i = 0; i < properties.Length; i++)
{
if (properties[i].Name == naam)
{
return i;
}
}
throw new InvalidDataException("Kolom naam " + naam + " niet bekend");
}
public void MaakTable(IEnumerable<object> data)
{
properties = data.First().GetType().GetProperties().Where(p => p.CanRead).ToArray();
grid.AutoGenerateColumns = false;
var details = new BoundField();
details.DataField = "Details";
grid.Columns.Add(details);
table.Columns.Add(new DataColumn("Details", typeof(object)));
foreach (var veld in properties)
{
table.Columns.Add(new DataColumn(veld.Name, (veld.Name == "Id" ? typeof(object) : veld.PropertyType)));
grid.Columns.Add(MaakKolom(veld));
}
foreach (var entry in data)
{
var row = table.NewRow();
int col = 0;
foreach (var veld in properties)
{
row[++col] = veld.GetValue(entry);
}
table.Rows.Add(row);
}
grid.DataSource = table;
SaveInSession();
}
protected DataControlField MaakKolom(PropertyInfo veld)
{
DataControlField field;
if (typeof(Entity).IsAssignableFrom(veld.PropertyType))
{
field = new ButtonField();
((ButtonField)field).DataTextField = veld.Name;
((ButtonField)field).ButtonType = ButtonType.Button;
((ButtonField)field).CommandName = veld.Name;
}
else if (veld.PropertyType == typeof(bool))
{
field = new CheckBoxField();
((CheckBoxField)field).DataField = veld.Name;
}
else if (veld.PropertyType.IsEnum)
{
field = new TemplateField();
//((TemplateField)field).ItemTemplate = (ITemplate)new Label()
//{
// Text = "#DataBinder.Eval(\"" + veld.Name + "\")",
//};
}
else if (veld.PropertyType == typeof(DateTime))
{
field = new TemplateField();
//field.DatePicker = true;
}
else
{
field = new BoundField();
((BoundField)field).DataField = veld.Name;
}
field.HeaderText = veld.Name;
return field;
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
}
}
}
}
使用Project.DomainModel.Models;
使用制度;
使用System.Collections.Generic;
使用系统数据;
使用System.IO;
使用System.Linq;
运用系统反思;
使用System.Web.UI;
使用System.Web.UI.WebControl;
命名空间CASH.WebApplication.Controls
{
公共部分类GenericTable:UserControl
{
私有财产信息[]财产;
//私有GridView GridView;
私有数据表=新数据表();
私有字典ingedrukt=新字典();
受保护的无效页_Init(对象发送方,事件参数e)
{
grid.RowCommand+=WeergaveDossiers\u RowCommand;
}
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
对于(int i=0;i1)
{
去除细胞(1);
}
MaakTable(新列表(){table.Rows[row][col]});
grid.Columns[0]。Visible=true;
grid.Rows[row]。单元格[0]。ColumnSpan=table.Columns.Count;
}
受保护的无效详细信息rijverwijderen(int行)
{
}
受保护的整数GetKolomIndex(字符串naam)
{
for(int i=0;ip.CanRead.ToArray();
grid.AutoGenerateColumns=false;
var details=newboundfield();
details.DataField=“details”;
grid.Columns.Add(详细信息);
table.Columns.Add(新的数据列(“详细信息”,typeof(object)));
foreach(属性中的变量级别)
{
table.Columns.Add(新数据列(veld.Name,(veld.Name==“Id”?typeof(对象):veld.PropertyType));
grid.Columns.Add(MaakKolom(veld));
}
foreach(数据中的var条目)
{
var row=table.NewRow();
int col=0;
foreach(属性中的变量级别)
{
行[++列]=veld.GetValue(条目);
}
table.Rows.Add(行);
}
grid.DataSource=表;
SaveInSession();
}
受保护的DataControlField MaakKolom(属性信息级别)
{
数据控制字段;
if(typeof(Entity).IsAssignableFrom(veld.PropertyType))
{
字段=新按钮字段();
((ButtonField)字段).DataTextField=veld.Name;
((ButtonField)字段).ButtonType=ButtonType.Button;
((ButtonField)字段).CommandName=veld.Name;
}
else if(veld.PropertyType==typeof(bool))
{
字段=新的CheckBoxField();
((CheckBoxField)字段).DataField=veld.Name;
}
else if(veld.PropertyType.IsEnum)
{
字段=新模板字段();
//((TemplateField)字段)。ItemTemplate
using Project.DomainModel.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CASH.WebApplication.Controls
{
public partial class GenericTable : UserControl
{
private PropertyInfo[] properties;
//private GridView gridView;
private DataTable table = new DataTable();
private Dictionary<int, int> ingedrukt = new Dictionary<int, int>();
protected void Page_Init(object sender, EventArgs e)
{
grid.RowCommand += WeergaveDossiers_RowCommand;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
for (int i = 0; i < grid.Rows.Count; i++)
{
grid.Rows[i].Cells[0].ColumnSpan = 0;
}
}
else
{
properties = (PropertyInfo[])Session["properties"];
table = (DataTable)Session["table"];
ingedrukt = (Dictionary<int, int>)Session["ingedrukt"];
foreach (var knop in ingedrukt)
{
DetailRijToevoegen(knop.Key, knop.Value);
}
}
grid.DataBind();
}
protected void SaveInSession()
{
Session["properties"] = properties;
Session["table"] = table;
Session["ingedrukt"] = ingedrukt;
}
protected void WeergaveDossiers_RowCommand(object sender, GridViewCommandEventArgs e)
{
int row = int.Parse((string)e.CommandArgument) + 1;
int col = GetKolomIndex(e.CommandName) + 1;
if (ingedrukt.ContainsKey(row))
{
if (ingedrukt[row] != col)
{
//DetailRijVerwijderen(row);
//ingedrukt.Remove(row);
//ingedrukt[row] = col;
}
}
else
{
ingedrukt[row] = col;
}
//DetailRijToevoegen(row, col);
SaveInSession();
}
protected void DetailRijToevoegen(int row, int col)
{
var data = table.NewRow();
var child = new GenericTable();
child.grid = new GridView();
data[0] = child;
table.Rows.InsertAt(data, row);
grid.DataSource = table;
grid.DataBind();
var cells = grid.Rows[row].Cells;
// Only keep the first cell
while (cells.Count > 1)
{
cells.RemoveAt(1);
}
child.MaakTable(new List<object>() { table.Rows[row][col] });
grid.Columns[0].Visible = true;
grid.Rows[row].Cells[0].ColumnSpan = table.Columns.Count;
}
protected void DetailRijVerwijderen(int row)
{
}
protected int GetKolomIndex(string naam)
{
for (int i = 0; i < properties.Length; i++)
{
if (properties[i].Name == naam)
{
return i;
}
}
throw new InvalidDataException("Kolom naam " + naam + " niet bekend");
}
public void MaakTable(IEnumerable<object> data)
{
properties = data.First().GetType().GetProperties().Where(p => p.CanRead).ToArray();
grid.AutoGenerateColumns = false;
var details = new BoundField();
details.DataField = "Details";
grid.Columns.Add(details);
table.Columns.Add(new DataColumn("Details", typeof(object)));
foreach (var veld in properties)
{
table.Columns.Add(new DataColumn(veld.Name, (veld.Name == "Id" ? typeof(object) : veld.PropertyType)));
grid.Columns.Add(MaakKolom(veld));
}
foreach (var entry in data)
{
var row = table.NewRow();
int col = 0;
foreach (var veld in properties)
{
row[++col] = veld.GetValue(entry);
}
table.Rows.Add(row);
}
grid.DataSource = table;
SaveInSession();
}
protected DataControlField MaakKolom(PropertyInfo veld)
{
DataControlField field;
if (typeof(Entity).IsAssignableFrom(veld.PropertyType))
{
field = new ButtonField();
((ButtonField)field).DataTextField = veld.Name;
((ButtonField)field).ButtonType = ButtonType.Button;
((ButtonField)field).CommandName = veld.Name;
}
else if (veld.PropertyType == typeof(bool))
{
field = new CheckBoxField();
((CheckBoxField)field).DataField = veld.Name;
}
else if (veld.PropertyType.IsEnum)
{
field = new TemplateField();
//((TemplateField)field).ItemTemplate = (ITemplate)new Label()
//{
// Text = "#DataBinder.Eval(\"" + veld.Name + "\")",
//};
}
else if (veld.PropertyType == typeof(DateTime))
{
field = new TemplateField();
//field.DatePicker = true;
}
else
{
field = new BoundField();
((BoundField)field).DataField = veld.Name;
}
field.HeaderText = veld.Name;
return field;
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
}
}
}
}
<asp:GridView runat="server"
ID="grid"
AutoGenerateColumns="true" />
if (grid != null)
{
// Do stuff with grid
}