如何在GridView的第一个头文件中插入DropDownList

时间:2014-08-28 21:19:38

标签: c# html asp.net gridview drop-down-menu

我有一个GridView,我有一些数据,还有另一个HTML表,上面有DropDownList来过滤GridView。唯一的问题是,因为它们是两个独立的表,所以列不匹配。为了弥补这一点,我在RowCreated方法中添加了以下内容作为示例:

protected void yourTasksGV_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        GridView hGrid = (GridView)sender;
        GridViewRow gvrRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
        TableHeaderCell tcCell = new TableHeaderCell();
        tcCell.Text = @"<asp:DropDownList ID='ddlTaskName2' CssClass='chosen-select' DataSourceID='dsPopulateTaskName2' AutoPostBack='true' DataValueField='Task Name' runat='server' Width='100%' Font-Size='11px' AppendDataBoundItems='true' OnSelectedIndexChanged='ddlTaskName_onSelectIndexChanged'>
                                    <asp:ListItem Text='All' Value='%'></asp:ListItem>
                                </asp:DropDownList>
                                <asp:SqlDataSource ID='sPopulateTaskName2' runat='server' ConnectionString='<%$ ConnectionStrings:gvConnString %>' SelectCommand=\""SELECT [actionname] from [OnBase].[hsi].[action]\""></asp:SqlDataSource>";
        tcCell.ColumnSpan = 1;
        gvrRow.Cells.Add(tcCell);

        TableHeaderCell tcCell2 = new TableHeaderCell();
        tcCell2.Text = "TEST #2";
        tcCell2.ColumnSpan = 1;
        gvrRow.Cells.Add(tcCell2);


        yourTasksGV.Controls[0].Controls.AddAt(0, gvrRow);
    }
}

创建GridView时,我没有看到任何DropDownList。

我用于过滤的表是:

<table class="taskGridView" runat="server">
    <tr>
        <td>
            <asp:DropDownList ID="ddlTaskName" CssClass="chosen-select" DataSourceID="dsPopulateTaskName" AutoPostBack="true" DataValueField="Task Name" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlTaskName_onSelectIndexChanged">
                <asp:ListItem Text="All" Value="%"></asp:ListItem>
            </asp:DropDownList>
            <asp:SqlDataSource ID="dsPopulateTaskName" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
        </td>
        <td>
            <asp:DropDownList ID="ddlService" CssClass="chosen-select" DataSourceID="dsPopulateService" AutoPostBack="true" DataValueField="Service" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlService_onSelectIndexChanged">
                <asp:ListItem Text="All" Value="%"></asp:ListItem>
            </asp:DropDownList>
            <asp:SqlDataSource ID="dsPopulateService" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
        </td>
        <td>
            <asp:DropDownList ID="ddlStatus" CssClass="chosen-select" DataSourceID="dsPopulateStatus" AutoPostBack="true" DataValueField="Status" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlStatus_onSelectIndexChanged">
                <asp:ListItem Text="All" Value="%"></asp:ListItem>
            </asp:DropDownList>
            <asp:SqlDataSource ID="dsPopulateStatus" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
        </td>
        <td>
            <asp:DropDownList ID="ddlDueDate" CssClass="chosen-select" DataSourceID="dsPopulateDueDate" AutoPostBack="true" DataValueField="Due Date" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlDueDate_onSelectIndexChanged">
                <asp:ListItem Text="All" Value="%"></asp:ListItem>
            </asp:DropDownList>
            <asp:SqlDataSource ID="dsPopulateDueDate" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
        </td>
        <td>
            <asp:DropDownList ID="ddlOwner" CssClass="chosen-select" DataSourceID="dsPopulateOwner" AutoPostBack="true" DataValueField="Owner" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlOwner_onSelectIndexChanged">
                <asp:ListItem Text="All" Value="%"></asp:ListItem>
            </asp:DropDownList>
            <asp:SqlDataSource ID="dsPopulateOwner" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
        </td>
        <td>
            <asp:DropDownList ID="ddlClient" CssClass="chosen-select" DataSourceID="dsPopulateClient" AutoPostBack="true" DataValueField="Client" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlClient_onSelectIndexChanged">
                <asp:ListItem Text="All" Value="%"></asp:ListItem>
            </asp:DropDownList>
            <asp:SqlDataSource ID="dsPopulateClient" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
        </td>
        <td>
            <asp:DropDownList ID="ddlSite" CssClass="chosen-select" DataSourceID="dsPopulateSite" AutoPostBack="true" DataValueField="Site" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlSite_onSelectIndexChanged">
                <asp:ListItem Text="All" Value="%"></asp:ListItem>
            </asp:DropDownList>
            <asp:SqlDataSource ID="dsPopulateSite" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
        </td>
        <td>
            <asp:DropDownList ID="ddlPractice" CssClass="chosen-select" DataSourceID="dsPopulatePractice" AutoPostBack="true" DataValueField="Practice" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlPractice_onSelectIndexChanged">
                <asp:ListItem Text="All" Value="%"></asp:ListItem>
            </asp:DropDownList>
            <asp:SqlDataSource ID="dsPopulatePractice" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
        </td>
        <td>
            <asp:DropDownList ID="ddlProvider" CssClass="chosen-select" DataSourceID="dsPopulateProvider" AutoPostBack="true" DataValueField="Provider" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlProvider_onSelectIndexChanged">
                <asp:ListItem Text="All" Value="%"></asp:ListItem>
            </asp:DropDownList>
            <asp:SqlDataSource ID="dsPopulateProvider" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
        </td>
    </tr>
</table>

我正在从Page_Load上的代码隐藏中填充SelectCommand。我也在Page_Load上调用了另一个函数,该函数引用了DropDownList,但我得到了The name 'DropDownList ID' does not exist in current content

我的GridView:

<asp:GridView ShowHeaderWhenEmpty="false" AlternatingRowStyle-BackColor="#EBE9E9" AutoGenerateColumns="false" OnSorting="yourTasksGV_Sorting" AllowSorting="true" ID="yourTasksGV" runat="server" ClientIDMode="Static" EmptyDataText="You currently have no tasks assigned to you" OnRowDataBound="yourTasksGV_RowDataBound" OnRowCreated="yourTasksGV_RowCreated">
    <Columns>
        <asp:HyperLinkField Target="_self" DataNavigateUrlFields="Task Detail" DataTextField="Task Name" DataNavigateUrlFormatString="" HeaderText="Task Detail" SortExpression="Task Name" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Service" HeaderText="Service" SortExpression="Service" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Due Date" HeaderText="Due" SortExpression="Due Date" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Owner" HeaderText="Owner" SortExpression="Owner" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Client" HeaderText="Client" SortExpression="Client" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Site" HeaderText="Site" SortExpression="Site" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Practice" HeaderText="Practice" SortExpression="Practice" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Provider" HeaderText="Provider" SortExpression="Provider" ItemStyle-CssClass="taskTableColumn" />
    </Columns>
</asp:GridView>

我想要做的是使用DropDownList表并将其插入正在生成的Header正上方的GridView HEADER中。

正如你在下面的图片中看到的那样DropDownList应该去的空标题就在那里,但我什么也看不见。我希望将DropDownList与all选项放在一起。

enter image description here

1 个答案:

答案 0 :(得分:1)

不是将标记添加到单元格文本中,而是在页面生命周期中无法正确评估,就像在aspx源中声明它一样,将控件添加到Controls集合中一个现有的控制。

例如,

DropDownList ddlTaskName2 = new DropDownList();
tcCell.Controls.Add(ddlTaskName2);

这可以让您解决下拉菜单无法显示的问题:

enter image description here

下面是一个可以使用的最小工作示例,它为GridView标题单元格添加了一个下拉列表。

标记:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewHeaderControl.aspx.cs"
    Inherits="WebApplication7_JQuery.GridViewHeaderControl" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView runat="server" ID="gvMain" OnRowCreated="gvMain_RowCreated" />
    </div>
    </form>
</body>
</html>

代码隐藏:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            gvMain.DataSource = new List<object> { new { ID = 1, Name = "First" }, new { ID = 2, Name = "Second" } };
            gvMain.DataBind();
        }
    }

    protected void gvMain_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            GridView hGrid = (GridView)sender;
            GridViewRow gvrRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);

            DropDownList ddl = new DropDownList();
            ddl.Items.Add("Option 1");

            TableHeaderCell tcCell = new TableHeaderCell();
            tcCell.Controls.Add(ddl);
            gvrRow.Cells.Add(tcCell);
            gvMain.Controls[0].Controls.AddAt(0, gvrRow);
        }            
    }

根据您在问题中作为单元格文本加载的标记,根据需要设置下拉列表的其他属性。

您可能需要在设置datasourceid后显式编码ddl.DataBind(),具体取决于您何时进行其他绑定。

这里动态创建ddl;但是,您可能希望在标记中声明它,以便它获得一个设计时变量,您可以在代码隐藏中的任何位置引用它以进行绑定和其他目的:

<asp:GridView runat="server" ID="gvMain" OnRowCreated="gvMain_RowCreated" />
<asp:DropDownList runat="server" ID="ddlTaskName" />

tcCell.Controls.Add(ddlTaskName);