Showing posts with label press. Show all posts
Showing posts with label press. Show all posts

Saturday, March 24, 2012

ASP.NET AJAX - Why IsPostBack=false?

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

Wednesday, March 21, 2012

ascx and ajax

HTML Source EditorWord wrap

Hi!

I am using an chat control (ascx) on my aspx sites. Any user can post a message and press the 'Absenden' button. After that the message will be send to database through code behind. The Gridview will be updated after this. Now I am trying to change the code (to ajax) so that there is 'no' postback on clicking on the 'Absenden' button. I have just watched the video on introducing ajax and sofar i am not quite sure what to do - placing a script manager ? placing an update panel? Thanks in advance for your help!!

ASCX Code

<%@dotnet.itags.org.ControlLanguage="C#"AutoEventWireup="true"CodeFile="ajaxLaberbox.ascx.cs"Inherits="Steuerelemente_laberbox" %>
<%@dotnet.itags.org.RegisterAssembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"Namespace="System.Web.UI"TagPrefix="asp" %>
<tableborder="0"width="150">
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<tdclass="ueberschrift3"align="center">Die LOW-Laberbox</td>
</tr>
<tr>
<tdheight="1"> </td>
</tr>
<tr>
<tdheight="1">
<asp:LabelID="lblNachricht"runat="server"Text="Deine Nachricht"CssClass="ueberschrift3"></asp:Label>
</td>
<tr>
<tdalign="center"height="1"><asp:TextBoxID="txtNachricht"runat="server"Height="87px"BorderColor="Transparent"ForeColor="Black"TextMode="MultiLine"MaxLength="400"Width="160px"></asp:TextBox>
</td>
</tr>
<tr>
<tdalign="center"height="1"><asp:ButtonID="btnAbsenden"runat="server"Text="Absenden"BackColor="#EDC112"BorderWidth="0px"Font-Names="Tahoma"Font-Size="8pt"OnClick="btnAbsenden_Click"/>
</td>
</tr>
<tr>
<tdnowrap>
<asp:GridViewID="gvNachrichten"runat="server"AutoGenerateColumns="False"DataSourceID="dbNachrichten"GridLines="None"Width="175px">
<Columns>
<asp:BoundFieldDataField="text"HtmlEncode="False"SortExpression="text">
<ControlStyleWidth="150px"/>
<ItemStyleWidth="150px"Wrap="True"/>
</asp:BoundField>
</Columns>
</asp:GridView>
<asp:SqlDataSourceID="dbNachrichten"runat="server"ConnectionString="<%$ ConnectionStrings:dbLOWConnectionString %>"
SelectCommand="SELECT top 10 '<span class="ueberschrift3">' + benutzer + ': </span><font size="-2" color="#EDC112">(' + DATENAME(d, datum) + '.' + convert(varchar(3), DATEPART(m, datum)) + '. ' + DATENAME(hh, datum) + ':' + DATENAME(mi, datum) + ')</font><span class="normal"> ' + nachricht + '</span>' AS text FROM tblNachrichten order by id desc">
</asp:SqlDataSource>
</td>
</tr>
</table>

The page that contains that control will need to have scriptmanager tag in it. The contents of your control (After the <%@. Register %>) need to be in an update panel of its own or you could place the control in an update panel inside of the Page.

Here is a link to some code to paste inside of the control that will dynamically add a scriptmanager and add all of the controls contents to an update panel.http://forums.asp.net/t/1090648.aspx?PageIndex=2


I found the problem! Unfortunatelly it was something completly different. My project was a simple ASP.Net Website. So in myweb.configmissed all necessarytags for ajax (see below)!! Thx to Joseh Humphrey - additionally i had to add the script manager in the page not in the control!

<config section>
<httpHandlers>
<system.webServer>
and so on

IT WORKS! :-)