我正在开发一个 ASP.NET Web 自定义控件,它继承自 System.Web.Ui.WebControls.GridView。
这是源代码:
public sealed class FaqListEditor : GridView
{
private readonly string _connectionString;
private readonly EntityDataSource _faqEntityDataSource = new EntityDataSource();
public FaqListEditor()
{
_connectionString = ConfigurationManager.ConnectionStrings["FaqEntities"] != null
? ConfigurationManager.ConnectionStrings["FaqEntities"].ConnectionString
: null;
this.InitilizeDataSource();
this.InitializeGrid();
this.InitializeGridColumns();
this.InitializeGridEvents();
}
private void InitilizeDataSource()
{
this._faqEntityDataSource.AutoPage = true;
this._faqEntityDataSource.AutoSort = true;
this._faqEntityDataSource.ConnectionString = this._connectionString;
this._faqEntityDataSource.DefaultContainerName = "FaqEntities";
this._faqEntityDataSource.EnableInsert = true;
this._faqEntityDataSource.EnableUpdate = true;
this._faqEntityDataSource.EnableDelete = true;
this._faqEntityDataSource.EntitySetName = "FaqItems";
this._faqEntityDataSource.EntityTypeFilter = "FaqItem";
this._faqEntityDataSource.Include = "FaqCategory";
}
private void InitializeGrid()
{
this.AllowPaging = true;
this.AllowSorting = true;
this.AutoGenerateColumns = false;
this.AutoGenerateEditButton = false;
this.DataKeyNames = new[]
{
"Id"
};
this.ShowHeaderWhenEmpty = true;
this.ShowHeader = true;
this.ShowFooter = true;
}
private void InitializeGridEvents()
{
this.RowEditing += this.HandleRowEditing;
this.RowUpdating += this.HandleRowUpdating;
this.RowDeleting += this.HandleRowDeleting;
this.RowCancelingEdit += this.HandleRowCancelingEdit;
}
private void InitializeGridColumns()
{
this.Columns.Add(new CommandField
{
ButtonType = ButtonType.Link,
ShowDeleteButton = true,
ShowInsertButton = true,
ShowEditButton = true,
ShowCancelButton = true
});
this.Columns.Add(new BoundField
{
DataField = "CategoryId",
HeaderText = "CategoryId",
SortExpression = "CategoryId"
});
this.Columns.Add(new BoundField
{
DataField = "Question",
HeaderText = "Question",
SortExpression = "Question"
});
this.Columns.Add(new BoundField
{
DataField = "Answer",
HeaderText = "Answer",
SortExpression = "Answer"
});
this.Columns.Add(new TemplateField
{
HeaderText = "Start Date",
SortExpression = "StartDate",
ItemTemplate = new DateTimePickerTemplate(ListItemType.Item, "StartDate"),
EditItemTemplate = new DateTimePickerTemplate(ListItemType.EditItem, "StartDate")
});
this.Columns.Add(new TemplateField
{
HeaderText = "End Date",
SortExpression = "EndDate",
ItemTemplate = new DateTimePickerTemplate(ListItemType.Item, "EndDate"),
EditItemTemplate = new DateTimePickerTemplate(ListItemType.EditItem, "EndDate")
});
this.Columns.Add(new CheckBoxField
{
DataField = "IsPublished",
HeaderText = "Published",
SortExpression = "IsPublished"
});
}
private void InitializeBinding()
{
this.DataSource = this._faqEntityDataSource;
this.DataBind();
}
private void HandleRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
e.Cancel = true;
((GridView)sender).EditIndex = -1;
this.InitializeBinding();
}
private void HandleRowEditing(object sender, GridViewEditEventArgs e)
{
((GridView)sender).EditIndex = e.NewEditIndex;
this.InitializeBinding();
}
private void HandleRowDeleting(object sender, GridViewDeleteEventArgs e)
{
((GridView)sender).EditIndex = -1;
this.InitializeBinding();
}
private void HandleRowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = Rows[e.RowIndex];
((GridView)sender).EditIndex = -1;
this.InitializeBinding();
}
protected override void OnInit(EventArgs e)
{
if (!this.Page.IsPostBack)
this.InitializeBinding();
}
}
}
基本上,我设法检索数据并显示。我还可以将gridview 切换到editmode。但是,当我按下行“保存”命令时,什么也没有发生。页面重新加载,gridview 保持在编辑模式。(甚至没有触发 GridView 更新事件!)
你能在代码中看到任何明显的错误吗?
有什么建议吗?