How to prevent full page postback on selectedindex

2019-01-29 07:48发布

问题:

<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>

Whenever the ddlTaskName_onSelectIndexChanged function is executed there is a full page postback rather than just updating the UpdatePanel

ddlTaskName_onSelectIndexChanged function:

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

With the above code, the page does a full postback rather than just partial (only update the GridView) whenever the index is changed in the ddlTaskName

What code can I add/modify to ensure the full postback isn't executed and only update the GridView on index changed.

Thought... Do I need to add them in two separate UpdatePanel?

If I uncomment the triggers, I get the following error: A control with ID 'ddlTaskName' could not be found for the trigger in UpdatePanel 'TasksUpdatePanel'.

I am attaching the dropdownlist to the Gridview like this:

Is it because of this:

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

回答1:

your code seems fine. did you try to comment out asp:Panel tab? if you unccoment triggers, you need to put asp:UpdatePanel around gridview



回答2:

As per this post it looks like your asp:Panel could be the culprit with the ClientIDMode="Static". Try changing this so it inherits.



回答3:

You'd need to specify ChildrenAsTriggers="true" in your UpdatePanel tag. The error you're getting is because your dropdown doesn't physically exist in the markup, which is what a Trigger line expects to find during compliation/runtime - instead, you are adding it dynamically as a control in your RowCreated function. It might be possible to, in that same function, add a trigger to the UpdatePanel dynamically, if you wanted to try that, instead.