views:

213

answers:

1

This is partly in reference to this:

http://stackoverflow.com/questions/485390/why-isnt-the-selectedindexchanged-event-firing-from-a-dropdownlist-in-a-gridview/641715#641715

I thought it different enough to ask another question.

My thought is that instead of adding a dropdownlist (ddl) to a gridview and then using the technique above that I could create a brand new control that has a ddl in it and the reference it directly.

This is more of a how do I create asp.net 2.0+ controls, I think, but is what I am asking possible? Can you make a "new" gridview control that just happens to always have a ddl in it and just refer to it (somehow) without findcontrol and all the rest?

I realize it would be highly customized for a unique app. I am just trying to see if it is possible as I may want to use this to create other controls.

Thank you.

+2  A: 

It depends on your definition of a "new GridView". The answer is yet, but at a cost.

If you base your control on WebControl, you can write a new grid control with any functionality. Somehow, I don't think this is what you have in mind.

If you want to inherit from the existing GridView and add extra controls, then it is also doable, but with heavy limitations. The reason is because GridView's implementation breaks every possible guideline for extensibility. I guess because they never meant it to be extended. For instance, they clear Controls collection almost on every occasion and explicitly expect for the Controls[0] to be a Table. I suppose, if you decide to stay within confines of the table layout (header, footer and all), then you may have more room to play.

Finally, you could create a wrapper, which has a GridView as its private member and simply expose everything you may need plus more. But that gets ugly very quickly.

Here is a crude demonstration (working) of the second approach. Note that the drop down is at the end. You can override the Render method, but you'd have to recreate a lot of MS code.

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>

SomePage.aspx.cs

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();
}
Ruslan