I'm using ASP.NET AJAX. There is UpdatePanel on the page. It contains GridView. When I press "Edit", "Update" or "Cancel" button in the GridView then UpdatePanel reloads. Also executes code "if (!IsPostBack) { ... }" in Page_Load event on the server - but I don't need it!!! This code must run only once when page is loading for the first time (some time isPostBack=true, some time =false. But it must be =true everytime!),. This happens not every time when I click on EDIT / CANCEL but from time to time. I made some experiments and found the couse of such behavior. Here is the code:
Default.aspx
<%@dotnet.itags.org. Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>Test Ajax + GridView</title></head><body> <form id="form1" runat="server"> <ajaxToolkit:ToolkitScriptManager ID="ScriptManager1" runat="server" /> <asp:SqlDataSource ID="s1" runat="server" ConnectionString="...connectiong to Northwind..." SelectCommand="SELECT top 5 * FROM Categories" /> <div> <asp:UpdatePanel ID="update_lgota" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:GridView ID="grid_lgota" runat="server" AutoGenerateColumns="true" DataSourceID="s1"> <Columns> <asp:CommandField ButtonType="Image" ShowEditButton="True" EditImageUrl="~/images/btn_edit.gif" UpdateImageUrl="~/images/btn_update.gif" CancelImageUrl="~/images/btn_cancel.gif" /> </Columns> </asp:GridView> </ContentTemplate> </asp:UpdatePanel> </div> </form></body></html>
Default.aspx.cs
using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partialclass _Default : System.Web.UI.Page {protected void Page_Load(object sender, EventArgs e) {if (!IsPostBack) {string s ="FIRST"; } }}
Put Breakpoint (F9) into if (!isPostBack) { ... }. Then run project and press "EDIT" in the GridView. Some times (not everytime) this breakpoint will activates. So we get isPostBack=false during postback - this couse some logic mistakes on the page.
BUT If you delete a line:
ButtonType="Image"
from the Default.aspx file then everything begin to work correctly, isPostBack=true everytime! This is very strange, may be this is ERROR or BUG in VisualStudio OR maybe I do everything in the wrong way. Please explain me what's wrong in my code? Why isPostBack has incorrect value when I'm using ButtonType="Image"? Why IsPostBack=false on postback? (the same problem if I use IsAsynhPostBack and IsCallBack)
I have installed:
- Microsoft Visual Web Developer 2005 SP1
- ASP.NET 2.0 AJAX Extensions 1.0
- AjaxControlToolkit 1.0.10606.0
I think what you're looking for instead of(!IsPostBack) isIf (!ScriptManager.GetCurrent(Page).IsInAsyncPostBack).
http://www.asp.net/learn/ajax-videos/video-173.aspx
That is a bit odd. IsPostBack should definitely be true there.
You might consider changing that CommandField to a TemplateField to test. I have a GridView in an UpdatePanel that I just ran through the debugger to test, and its templated edit/update/cancel buttons do correctly cause Page.IsPostBack to be true during the partial postbacks they trigger.
For example, this is the command column in the grid I just tested:
<asp:TemplateField>
<ItemStyle Width="40px" HorizontalAlign="Center" />
<ItemTemplate>
<asp:ImageButton runat="server" ID="EditButton" CommandName="Edit" ImageUrl="~/Images/edit-wide.png" CausesValidation="false" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex")%>' OnCommand="EquipmentList_RowEditing" />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton runat="server" ID="UpdateButton" CommandName="Update" ImageUrl="~/Images/ok.gif" CausesValidation="false" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex")%>' OnCommand="EquipmentList_RowUpdating" />
<asp:ImageButton runat="server" ID="CancelButton" CommandName="Cancel" ImageUrl="~/Images/cancel.gif" CausesValidation="false" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex")%>' OnCommand="EquipmentList_CancelEditing" />
</EditItemTemplate>
</asp:TemplateField>
Thanks. It was useful
No comments:
Post a Comment