Asp.net 是否可以将带有dropdownlist的gridview作为一个新控件?

Asp.net 是否可以将带有dropdownlist的gridview作为一个新控件?,asp.net,controls,custom-controls,Asp.net,Controls,Custom Controls,这部分是指: 我觉得再问一个问题就够不一样了 我的想法是,我可以创建一个全新的控件,其中包含一个ddl,并直接引用它,而不是向gridview添加一个dropdownlist(ddl),然后使用上面的技术 我想这更像是一个如何创建asp.NET2.0+控件的问题,但我所问的可能吗?您是否可以创建一个“新”gridview控件,它恰好总是包含一个ddl,并且在没有findcontrol和所有其他控件的情况下(以某种方式)引用它 我意识到这将是一个高度定制的独特的应用程序。我只是想看看是否可能,因

这部分是指:

我觉得再问一个问题就够不一样了

我的想法是,我可以创建一个全新的控件,其中包含一个ddl,并直接引用它,而不是向gridview添加一个dropdownlist(ddl),然后使用上面的技术

我想这更像是一个如何创建asp.NET2.0+控件的问题,但我所问的可能吗?您是否可以创建一个“新”gridview控件,它恰好总是包含一个ddl,并且在没有findcontrol和所有其他控件的情况下(以某种方式)引用它

我意识到这将是一个高度定制的独特的应用程序。我只是想看看是否可能,因为我可能想用它来创建其他控件


谢谢。

这取决于您对“新网格视图”的定义。答案是肯定的,但要付出代价

如果控件基于WebControl,则可以编写具有任何功能的新网格控件。不知何故,我认为这不是你想要的

如果您想从现有的GridView继承并添加额外的控件,那么它也是可行的,但有很大的限制。原因是GridView的实现打破了所有可能的可扩展性准则。我猜是因为他们从来没有打算延长。例如,它们几乎每次都会清除控件集合,并明确希望控件[0]是一个表。我想,如果您决定停留在表格布局的范围内(页眉、页脚和所有),那么您可能会有更多的发挥空间

最后,您可以创建一个包装器,它有一个GridView作为其私有成员,只需公开您可能需要的所有内容以及更多内容。但这很快就会变得难看

下面是第二种方法的粗略演示(工作)。请注意,下拉列表位于末尾。可以重写Render方法,但必须重新创建大量MS代码

ExtendedGridView

public class ExtendedGridView : GridView
{
    protected DropDownList DropDown { get; set; }

    public ExtendedGridView() : base()
    {
        this.DropDown = new DropDownList();
        this.DropDown.Items.Add("white");
        this.DropDown.Items.Add("red");
        this.DropDown.Items.Add("blue");
        this.DropDown.Items.Add("green");
        this.DropDown.AutoPostBack = true;
        this.DropDown.ID = "dropdown";
        this.DropDown.SelectedIndexChanged += new EventHandler(DropDown_SelectedIndexChanged);
    }

    void DropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
        BackColor = System.Drawing.Color.FromName(this.DropDown.SelectedValue);
    }

    protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
    {
        int itemCount = base.CreateChildControls(dataSource, dataBinding);
        Controls.Add(this.DropDown);
        return itemCount;
    }
}
SomePage.aspx

<%@ Register TagPrefix="my" Namespace="MyProject" Assembly="MyProject" %>
<my:ExtendedGridView id="myGridView" runat="server" onpageindexchanging="myGridView_PageIndexChanging"></my:ExtendedGridView>
protected void Page_Load(object sender, EventArgs e)
{
    myGridView.DataSource = new string[] { "aaa", "bbb", "ccc", "ddd", "eee" };
    myGridView.AllowPaging = true;
    myGridView.PageSize = 2;
    myGridView.DataBind();
}

protected void myGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    myGridView.PageIndex = e.NewPageIndex;
    myGridView.DataBind();
}