我正在尝试对位于UserControl中的GridView进行子类化。我希望能够在另一个页面上处理由此引起的事件。
基本上,我的代码如下所示:
我的UserControl包含一个GridView:
<%@ Control Language="C#" AutoEventWireup="false" CodeBehind="StdList.ascx.cs" Inherits="UCS_Web.uP.UserControls.StdList" %>
<div>
<asp:Panel ID="Panel1" runat="server">
<asp:GridView ID="_gridView" runat="server" PageSize="6"
GridLines="None" AutoGenerateColumns="False"
OnRowCommand="_gridView_RowCommand" AutoGenerateEditButton="false"
OnDataBound="_gridView_DataBound" OnPreRender="_gridView_PreRender">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" CssClass="gridViewHdr" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
</asp:Panel>
使用 UserControl 的页面会像这样:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="BypassReasonsPage.aspx.cs" Inherits="UCS_Web.uP.Tools.BypassReasonsPage" %>
<%@ Register Src="~/uP/UserControls/StdList.ascx" TagName="List" TagPrefix="uc" %>
<body>
<form id="form1" runat="server">
<div>
<uc:List ID="uc_list" runat="server" />
</div>
</form>
这是代码后端:
uc_list.GridView.DataSource = this.TCW.Copy.bypassReasons;
uc_list.GridView.DataBind();
为了让这个页面正常工作,我包含了一个文件来设置哪些列是数据绑定的等等。
public class BypassReasonsByToolTable : UCS_Web.uP.UserControls.StdList.ICustomTable
{
public DataControlField[] Columns
{
get
{
BoundField col1 = new BoundField();
col1.DataField = "Code";
col1.HeaderText = "Code";
col1.SortExpression = "Code";
col1.ItemStyle.Width = new Unit(50, UnitType.Percentage);
BoundField col2 = new BoundField();
col2.DataField = "Text";
col2.HeaderText = "Text";
col2.SortExpression = "Text";
col2.ItemStyle.Width = new Unit(50, UnitType.Percentage);
TemplateField editReason = new TemplateField();
editReason.ItemTemplate = new addTemplate();
return new DataControlField[] { col1, col2, editReason };
}
}
我希望能够将OnRowCommand、OnRowDelete和其他所有事件处理程序存储在一个单独的文件中,而不是在UserControl的CodeBehind中。 有什么办法可以实现这一点吗?
我尝试将它们作为虚拟类,并在使用它们的页面上重写它们,但这并没有起作用。还有其他方法可以实现这一点吗?
编辑:UserControl CodeBehind
namespace UCS_Web.uP.UserControls
{
public partial class StdList : UserControl
{
private ICustomTable m_custom = null;
protected void _gridView_DataBound(object sender, EventArgs e)
{
if (_gridView.Rows.Count > 0)
{
for (int i = _gridView.Rows.Count + 1; i <= _gridView.PageSize - 1; i++)
{
GridViewRow row = new GridViewRow(
0,
0,
DataControlRowType.DataRow,
//(i % 2 > 0) ? datacontrolrowstate.normal : datacontrolrowstate.alternate);
DataControlRowState.Alternate);
foreach (DataControlField field in _gridView.Columns)
{
TableCell cell = new TableCell();
cell.Text = " ";
row.Cells.Add(cell);
}
//row.Attributes.Add("OnClick", "javascript:alert();");
row.BackColor = System.Drawing.ColorTranslator.FromHtml("#ffffff");
_gridView.Controls[0].Controls.AddAt(i, row);
}
}
}
protected void _gridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
//DO MY DELETE STUFF FOR THIS SPECIFIC PAGE
}
}
}
编辑:我在.cs文件中添加了新的重写函数(尝试了很多变化,但这是当前的版本)
namespace UCS_Web.uP.UserControls
{
public class MyStdList : StdList
{
protected override void _gridView_RowCommand(object sender, GridViewCommandEventArgs e){
Response.Redirect("HERPA DERP!");
}
}
}