如何防止对下拉列表的选定交换进行整页回发
本文关键字:换进 交换 何防止 下拉列表 | 更新日期: 2023-09-27 17:49:55
<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。
认为……我需要在两个单独的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);
}
}
你的代码看起来很好。你有没有试着注释掉asp:Panel标签?如果你不注释触发器,你需要把asp:UpdatePanel放在gridview
根据这篇文章,看起来你的asp:Panel可能是ClientIDMode="Static"的罪魁祸首。
您需要在UpdatePanel
标记中指定ChildrenAsTriggers="true"
。你得到的错误是因为你的下拉菜单在标记中不存在,这是触发行期望在复杂/运行时找到的-相反,你在RowCreated
函数中动态添加它作为控件。在相同的函数中,如果您想尝试的话,也可以动态地向UpdatePanel添加一个触发器。