简体   繁体   English

如何防止在下拉列表的selectedindexchange上进行全页回发

[英]How to prevent full page postback on selectedindexchange for dropdownlist

<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函数时,就会有整页的回发,而不仅仅是更新UpdatePanel

ddlTaskName_onSelectIndexChanged function: 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
}

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 使用上面的代码,只要在ddlTaskName更改索引,页面就会执行完整的回发,而不是部分回发(仅更新GridView)。

What code can I add/modify to ensure the full postback isn't executed and only update the GridView on index changed. 我可以添加/修改哪些代码以确保不执行完整的回发,而仅在更改索引时更新GridView。

Thought... Do I need to add them in two separate UpdatePanel? 以为...我需要将它们添加到两个单独的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'. 如果取消注释triggersA control with ID 'ddlTaskName' could not be found for the trigger in UpdatePanel 'TasksUpdatePanel'.出现以下错误: 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: 我将下拉列表附加到Gridview中,如下所示:

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

your code seems fine. 您的代码看起来不错。 did you try to comment out asp:Panel tab? 您是否尝试将asp:Panel标签注释掉了? if you unccoment triggers, you need to put asp:UpdatePanel around gridview 如果您没有触发,则需要在gridview周围放置asp:UpdatePanel

As per this post it looks like your asp:Panel could be the culprit with the ClientIDMode="Static". 根据这篇文章 ,您的asp:Panel可能是ClientIDMode =“ Static”的罪魁祸首。 Try changing this so it inherits. 尝试更改它,使其继承。

You'd need to specify ChildrenAsTriggers="true" in your UpdatePanel tag. 您需要在UpdatePanel标记中指定ChildrenAsTriggers="true" 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. 您收到的错误是因为标记中实际上不存在您的下拉菜单,而这正是触发器行希望在编译/运行期间找到的-相反,您是将其动态添加为RowCreated函数中的RowCreated It might be possible to, in that same function, add a trigger to the UpdatePanel dynamically, if you wanted to try that, instead. 如果您想尝试,可以在同一功能中将触发器动态添加到UpdatePanel。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 回发前如何触发SelectedIndexChange? - How to trigger SelectedIndexChange before postback? 如何在DropDownList的selectedIndexChange操作上显示CheckBoxList? - How to display a CheckBoxList on selectedIndexChange action of a DropDownList? 如何更改另一个下拉列表的SelectedIndexChange上的下拉列表? - How I can change a dropdownlist on SelectedIndexChange of another dropdownlist? Repeater中的DropDownList:如何处理SelectedIndexChange并获取DataItem? - DropDownList inside Repeater: How to handle SelectedIndexChange and get DataItem? 如何在回发时阻止页面刷新? - how to prevent page refresh while doing postback? 所有下拉列表均在一次selectedIndexchange时重置 - All dropdownlist resets at single selectedIndexchange 单击按钮时如何防止下拉列表回发或在更新面板中更新 - How to prevent a dropdownlist from postback or updating inside an updatepanel when button is clicked 转发器整页回发中的下拉菜单 - Dropdowns in repeater full page postback 来自多个 DropDownList 的回发工作但在页面中不刷新 - Postback from multiple DropDownList working but not refreshing in page 如何停止asp.net中的整页回发? - How to stop full page postback in asp.net ?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM