如何防止selectindexchange上的整页回发以获取下拉列表

时间:2015-05-20 14:38:08

标签: c# asp.net drop-down-menu updatepanel jquery-chosen

<asp:UpdatePanel runat="server" ClientIDMode="Static" ID="TasksUpdatePanel" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Panel ID="pnlDropDown" runat="server" ClientIDMode="Static" CssClass="pnlDropDown">
            <!-- TASK NAME -->
            <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:Panel>
        <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:TemplateField HeaderStyle-Width="2%">
                    <ItemTemplate>
                        <asp:ImageButton ImageUrl="~/cies.png" runat="server" ID="btnShowDepend" OnCommand="btnShowDepend_Command" CommandName="TaskDepend" CommandArgument='<%#Eval("TestIt") %>' ToolTip="Click to view Dependencies" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:HyperLinkField HeaderStyle-Width="16%" Target="_self" DataNavigateUrlFields="Task Detail" DataTextField="Task Name" DataNavigateUrlFormatString="" HeaderText="Task Detail" SortExpression="Task Name" ItemStyle-CssClass="taskTableColumn" />
                <asp:BoundField HeaderStyle-Width="10%" DataField="Workgroup" HeaderText="Workgroup" SortExpression="Workgroup" ItemStyle-CssClass="taskTableColumn" />
                <asp:BoundField HeaderStyle-Width="7%" DataField="Status" HeaderText="Status" SortExpression="Status" ItemStyle-CssClass="taskTableColumn" />
            </Columns>
        </asp:GridView>
    </ContentTemplate>
    <%--<Triggers>
        <asp:AsyncPostBackTrigger ControlID="ddlTaskName" EventName="onSelectIndexChanged" />
    </Triggers>--%>
</asp:UpdatePanel>

每当执行ddlTaskName_onSelectIndexChanged函数时,都会有一整页回发,而不仅仅是更新UpdatePanel

ddlTaskName_onSelectIndexChanged功能:

protected void ddlTaskName_onSelectIndexChanged(object sender, EventArgs e)
{
    PullData(ViewState["sortExp"].ToString(), ViewState["sortOrder"].ToString(), false); //calls a function to update the GridView
}

使用上面的代码,只要在ddlTaskName

中更改索引,页面就会执行完整的回发而不是仅部分(仅更新GridView)

我可以添加/修改哪些代码以确保不执行完整的回发,并且只在更改索引时更新GridView。

思考......我是否需要将它们添加到两个单独的UpdatePanel中?

如果我取消注释triggers,我会收到以下错误:A control with ID 'ddlTaskName' could not be found for the trigger in UpdatePanel 'TasksUpdatePanel'.

我将下拉列表附加到Gridview,如下所示:

是不是因为这个:

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 tcCellTask = new TableHeaderCell();
        tcCellTask.Controls.Add(ddlTaskName);
        gvrRow.Cells.Add(tcCellTask);

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

3 个答案:

答案 0 :(得分:2)

你的代码似乎很好。你试图评论出asp:Panel选项卡吗? 如果你解开触发器,你需要在gridview

周围放置asp:UpdatePanel

答案 1 :(得分:1)

按照这个post看起来你的asp:Panel可能是ClientIDMode =“Static”的罪魁祸首。尝试更改它,以便继承。

答案 2 :(得分:0)

您需要在ChildrenAsTriggers="true"标记中指定UpdatePanel。您得到的错误是因为您的下拉列表实际上并不存在于标记中,这是Trigger行在compliation / runtime期间期望找到的内容 - 相反,您将动态添加它作为RowCreated中的控件功能。在同一个函数中,如果你想尝试,可以动态地向UpdatePanel添加一个触发器。