ASP.NET DropDownList control in GridView causes Full postback on SelectedIndexChanged event
I have a dropdownlist control(SourceDD) in my gridview inside an update panel, and everytime I click on the dropdown the OnSelectedIndexChanged event fires like i开发者_如何学Ct should. But before that, it does a full postback and runs through the entire Page_Load code, which is not what I want. Basically I want it to just run the OnSelectedIndexChanged event and that's it, not cause a full post back. In my event I am just enabling/disabling the next column(SymbolDD) based on the selection they make in SourceDD, so there is nothing special in the event code. Please lmk if there is a way to NOT do a full postback inside the updatepanel that contains a gridview. Thanks a lot...
<asp:UpdatePanel ID="TestsPanel" runat="server" Visible="true" UpdateMode="Conditional" EnableViewState="false" ChildrenAsTriggers="true">
<ContentTemplate> <asp:GridView ID="TestGridView" runat="server" Visible="true" CssClass="GridViewRows" AlternatingRowStyle-CssClass="TableRowEven"
AutoGenerateColumns="false">
<HeaderStyle CssClass="TableHead" />
<Columns>
<asp:TemplateField Headertext="Source">
<ItemTemplate>
<asp:DropDownList runat="server" ID="SourceDD" AutoPostBack="true" OnSelectedIndexChanged="SourceDD_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Headertext="Symbol">
<ItemTemplate>
<asp:DropDownList runat="server" ID="SymbolDD">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
Use javascript/ajax to handle the event if you don't want the post back or try fixing your code behind to properly handle the partial post back.
try these links: http://encosia.com/2007/07/11/why-aspnet-ajax-updatepanels-are-dangerous/ http://www.asp.net/ajax/tutorials/understanding-partial-page-updates-with-asp-net-ajax
I agree with Dustin. Use javascript, you'll have to put it on your controls GridViewRowDataBound event so that you enable the correct controls.
Something like
RowDatabound(object sender, GridViewRowEventArgs e)
{
((DropDownList)e.FindControl("SourceDD")).Attributes("onchange", <onchangelogic>);
//use something like "document.getElementById('" +(DropDownList)e.FindControl("SymbolDD")).ClientID + "').enabled = true;"
//or maybe it was .disabled = false....
}
Actually, there is a solution to this problem. You can add an UpdatePanel to the ItemTemplate of the TemplateField of the GridView and add your DropDownList to this UpdatePanel. Then add the AsyncPostBackTrigger for the DropDownList "SelectedIndexChanged" event. This ensures that the post backs happening on change of the selected item of the DropDownList are partial (i.e the whole page does not refresh).
精彩评论