Showing posts with label button. Show all posts
Showing posts with label button. Show all posts

Wednesday, March 28, 2012

ASP.Net AJAX ModalPopupExtender: How to Trigger Function from the Page CodeBehind

I have a function named"btnSave_Click()" located in the codebehind of a page. The function is associated with the
click event of my button named"btnSave". It saves the values of the controls in the page to the database.

When i have added a ModalPopupExtender and associated the value"btnSave"to theOkControlID property
of the ModalPopupExtender, the popup control was displayed as i have configured it but my routines inside
thebtnSave_Click()were not anymore executed.

How can i resolve this? How can i execute a function in the codebehind of my page after clicking a button
associated with the ModalPopupExtender? I am familiar with theOnOkScriptproperty of the popup extender
but as I understand, it can only execute a script within the same page and not my codes in the codebehind.

Thank you very much.

Hi,

When a button is set as the OKControl for a ModalPopupExtender, the Extender will intercept its PostBack behavior, and just close itself when the Button is clicked.

As a result, the server side function isn't able to run.

So, please don't specify btnSave as OKControl.

Hope this helps.

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

asp.net 2.0 | urgent | "Cancel" response from client side

hi, I have a web site where user provide some search criteria and click on button "Search". I want to provide a "Cancel" button which user can click. In case search takes long, user clicks on Cancel and he remains on the same page without any result. basically i want to provide the option of cancelling a request already fired or not wait for result of already fired request. I want to do this on client side.

I have tried sample provided on Microsoft site by using PageRequestManager.getinstance(), but it is very slow. http://asp.net/AJAX/Documentation/Live/tutorials/CancelAsyncPostback.aspx

Even in this sample, clicking on cancel does not immediately cancel the query.There is time lag which is almost the same time as when normal response comes back. If I click Cancel, I would expect that search to cancel and same page to get reloaded faster.

Please help me, this is urgent.

Thanks

Seehttp://mattberseth.com/blog/2007/06/aspnet_ajax_canceling_an_async_1.html

-Damien


Thanks Damien for providing linkSmile

It's really helpful


Hi,

I did some research, but I couldn't find anyone complaining about this issue.

As far as I know, the simplest way to cancle a AyncPostback is to use PageRequestManager.getinstance()(http://asp.net/AJAX/Documentation/Live/tutorials/CancelAsyncPostback.aspx), I nerver heard anyone complain that it is too slow.

I don't think there is a better way by now.

So please double check your code.

Best Regards,


Hi Jin,

Thanks for providing the link. I have used Sys.WebForms.PageRequestManager.getInstance().abortPostBack() in my code to cancel the asyn postback. I think it gets cancelled but the problem is that since request is still executing at server, anything that i now do on my application becomes very slow like loading another page or something. and after a few seconds everything returns to normal. this is probably because requests are getting queued at server...


Tirath.g@.hotmail.com:

I think it gets cancelled but the problem is that since request is still executing at server, anything that i now do on my application becomes very slow like loading another page or something. and after a few seconds everything returns to normal

I believe you are correct.

You can try to use the UpdateProgress control to "blank" out the screen while the process is occuring which may stay while the cancel is in progress. You can make the UpdateProgress "modal" so that it blanks out the page; there is an example here:http://blogs.visoftinc.com/archive/2007/09/10/modalupateprogress.aspx

-Damien