ScriptManager issue in asp.net on Submit - asp.net

I have following code:
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSave" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:Button ID="btnSave" runat="server" Text="Submit"/>
<asp:Panel ID="pnlMyView" runat="server">
<asp:GridView Control here...
</Panel>
<asp:Panel ID="pnlInfo" runat="server" Visible="False">
<div>
Some information...
</div>
</Panel>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
<ProgressTemplate>
Update in progress...
</ProgressTemplate>
</asp:UpdateProgress>
<script language="javascript" type="text/javascript">
$(window).load(function() {
$("input:submit[id$='btnSave']").click(function() {
//jQuery code to validate gridview enteries...
})
});
</script>
Steps:
1.Refresh the page Or load page first time and clicked on Submit button to validate the entries in GridView
2.Validation done properly and save changes to database.
3.Made some changes in gridview entries and clicked on Submit button
4.Validation didn't take place and save the entries(i.e. wrong entries) in database.
5.Refresh the page i.e. pressing F5 Now everything working fine.
How to resolve issue at step 4.
-----------------PS----------------
Actually I have two panel pnlMyView and pnlInfo.So on page load I only make visible to pnlMyView and other is invisible.
Once if i move to pnlInfo and come to back on pnlMyView and submit Nothing validation take place.
Thanks

The problem is that your HTML gets reloaded and thus the event handler on the button gets lost.
You could try the following code which assures you that the handler stays in place:
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div id="root">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSave" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:Button ID="btnSave" runat="server" Text="Submit"/>
<asp:Panel ID="pnlMyView" runat="server">
<asp:GridView Control here...
</Panel>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
<ProgressTemplate>
Update in progress...
</ProgressTemplate>
</asp:UpdateProgress>
</div>
<script language="javascript" type="text/javascript">
$(window).load(function() {
$("input:submit[id$='btnSave']").on("click", ".root", function() {
//jQuery code to validate gridview enteries...
})
});
</script>
What this does is register the handler differently. You need to wrap the UpdatePanel inside a div with class root. Then you register the handler like this:
$("input:submit[id$='btnSave']").on("click", ".root", function() {});
This tells jQuery to registere a handler on an element called root, but only execute it when the input:submit... is executed.
Since the div is not removed, it will still detect the handler even after you have replaced the HTML.
These types of events are called delegated events. You can read a more thorough explanation in the jQuery documentation: http://api.jquery.com/on/

Better use OnClientClick property of Button control:
<asp:Button ID="btnSave" runat="server" Text="Submit" OnClientClick="return validate()" />
function validate(){
//jQuery code to validate gridview enteries...
//if validation isn't succeeded return false, otherwise return true
}

Hey friends I got the solution.I used live event and now everything is working fine.
$("input:submit[id$='btnSave']").live("click", function(event) {
Thanks a all for your response

Related

How can I prevent all update panels reloading when I click a button in one of them?

I've got a webform with two update panels:
<asp:ScriptManager ID="ScriptManager1" runat="server" ScriptMode="Release">
</asp:ScriptManager>
<p>
<asp:UpdatePanel ID="upl1" runat="server">
<ContentTemplate>
<asp:Button runat="server" ID="btn1" Text="Button1" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="upl2" runat="server">
<ContentTemplate>
<asp:Button runat="server" id="btn2" Text="Button2" />
</ContentTemplate>
</asp:UpdatePanel>
</p>
If I click btn1, then the load event of upl2 is triggered, as well as upl1.
I would expect only upl1 to be loaded, since this is the update panel that contains the button.
Why isn't this happening, and how can I make it happen?
One pratice I always use when I work with Update Panels, is to set the properties: ChildrenAsTriggers="false" UpdateMode="Conditional"
And refresh the Update Panel only when I want:
protected void btn1_Click(object sender, EventArgs e)
{
// some logic
// ....
upl1.Update();
}
Try applying these properties and see if it solves your problem.
I had a similar problem with a button triggering an UpdatePanel, and using ChildrenAsTriggers="false" UpdateMode="Conditional" didn't help.
I ended up using the following to force the button to do a regular post-back:
ScriptManager1.RegisterPostBackControl(btn1);

How I can change label, when ProgressTemplate is running?

How I can change label(lblSaved), when UpdateProgress is running?My label update after UpdateProgress. I need, when UpdateProgress is starting run to do label(lblSaved) visible false.
<asp:UpdateProgress ID="uprogAutoSave" AssociatedUpdatePanelID="upnlAutoSave" runat="server">
<ProgressTemplate>
<asp:Literal runat="server" Text="<%$ Resources:AutoSave %>" /></ProgressTemplate>
</asp:UpdateProgress>
...
<asp:UpdatePanel ID="UpdatePanel1" RenderMode="Inline" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="TimerAutoSave" />
</Triggers>
<ContentTemplate>
<asp:Label ID="lblSaved" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
You have to handel Ajax event from client-side using JavaScript or jQuery.
You have to handle two Ajax event at client side.
OnRequestStart
OnResponseEnd
The first method call at same time your Ajax request start and the second one called when your ajax request become in complete state.
In this methods you have to handle your label, and this all is JavaScript/jQuery so I assume that you will not get any problem to manage the label.

How to Show the progressbar to Adding PostBackTriggers and AsyncPostBackTriggers to UpdatePanel in asp.net?

I have a page with a ToolkitScriptManager and three update panels each update panel have triggers based on controls , now my problem is fire any server side events show the progressbar like percentages Example, How to show like that in PostBackTriggers and AsyncPostBackTriggers controls , my code is
<asp:UpdatePanel ID="up1" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="TxtZip" EventName="textchanged" />
</Triggers>
<ContentTemplate>
<asp:TextBox ID="TxtZip" runat="server" AutoPostBack="true" ClientIDMode="Static"
MaxLength="6" onkeypress="return isNumberKey(event)" CssClass="txtstyle" OnTextChanged="TxtZip_TextChanged"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:PostBackTrigger ControlID="IBtnSave" />
</Triggers>
<ContentTemplate>
<span class="art-button-wrapper"><span class="l"></span><span class="r"></span>
<asp:Button ID="IBtnSave" runat="server" Text="Submit/Save" CssClass="art-button"
ClientIDMode="Static" OnClientClick="return postbackButtonClick()" OnClick="IBtnSave_Click" />
</span>
</ContentTemplate>
</asp:UpdatePanel>
my progress bar code
<asp:UpdateProgress ID="UpdateProgress1" runat="server" DynamicLayout="true">
<ProgressTemplate>
<div id="Progressbar" class="Progressbar" align="center" valign="middle" runat="server">
<asp:Image ID="Image1" Width="75" Height="95" runat="server" ImageUrl="~/images/animation_processing.gif" />
</div>
</ProgressTemplate>
</asp:UpdateProgress>
and java script code is
<script type="text/javascript">
//update panel and postback trigger with fileupload control upadteprogressbar code
var updateProgress = null;
function postbackButtonClick() {
updateProgress = $find("<%= UpdateProgress1.ClientID %>");
window.setTimeout("updateProgress.set_visible(true)", updateProgress.get_displayAfter());
return true;
}
</script>
This code working only postbacktrigger reason is when button click before fire onclick event , that event to call java script function , but Async Postback trigger does not call that function, how to handle that code and display the progressbar with percentages process, give me any suggestion
Just add the bellow code in aspx content page it working fine
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
Sys.Application.add_init(appl_init);
function hideProgressBar() {
// var updateProgress = $find("<%= UpdateProgress1.ClientID %>");
// updateProgress.style.visibility = "hidden";
$("#MainContent_UpdateProgress1").hide()
}
function appl_init() {
var pgRegMgr = Sys.WebForms.PageRequestManager.getInstance();
pgRegMgr.add_beginRequest(postbackButtonClick);
pgRegMgr.add_endRequest(hideProgressBar)
}
</script>

AsyncPostBack FileUpload on a ModalPopup

I am using a UpdatePanel, and my submit button is one of the triggers along with the clear button. But the problem is I have a FileUpload Control in the div. This is a modal popup, so it displays a form for a user to upload a little note. When I try to upload a file with AsyncPostBackTrigger it does nothing (which I have read about). My question is how do I not use the PostBackTrigger because I want to use the asyncpostbacktrigger because if an error occurs, then the Modal popup closes and the user doesnt know if the file was uploaded or not. What can I do?
Code:
<asp:Panel ID="addnotepanel" runat="server" style="/*display:none;*/" CssClass="addnotepanel">
<asp:UpdatePanel ID="UpdatePanel1" runat="server"><ContentTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ClrBtn" />
<asp:PostBackTrigger ControlID="SubmitBtn" />
</Triggers>
</asp:UpdatePanel>
File:
<br />
<asp:Label ID="ErrorLabel" runat="server" Visible="False"></asp:Label>
<br />
<asp:Button ID="Submitbtn" runat="server" Text="Submit"
onclick="Submitbtn_Click" />
<asp:Button ID="CnlBtn" runat="server" Text="Cancel" onclick="CnlBtn_Click" />
<asp:Button ID="ClrBtn" runat="server" onclick="ClrBtn_Click"
Text="Clear" />
</div></asp:Panel>
keep SubmitBtn as a PostBackTrigger but dont set it as the "OkControlID" of the modalpopupextender.
in the Submitbtn_Click server side sub, call yourModalpopupextenderID.hide() if the upload is completed so the Modal pop up closes only if there is no error.
You could use a AsyncFileUpload from the AjaxControlToolkit
Here, as an exemple some code to show how to use it :
<AjaxControlToolkit:AsyncFileUpload ID="AttachementsFileUpload"
runat="server"
OnUploadedComplete="AttachementsFileUpload_UploadedComplete"
OnClientUploadComplete="uploadComplete" />
<script type="text/javascript">
var UpdateAttachementsGridViewButton = '<%= UpdateAttachementsGridViewButton.ClientID %>';
function uploadComplete(sender, args) {
$get(UpdateAttachementsGridViewButton).click();
}
</script>
As you see, when the upload is complete, I use Javascript to trigger an click on an hidden button. Meanwhile I retrieve the file in the AttachementsFileUpload_UploadedComplete using something like this:
Dim AttachementsFileUpload As AjaxControlToolkit.AsyncFileUpload = AnnouncementFormView.FindControl("AttachementsFileUpload")
Attachements.add(e.filename, AttachementsFileUpload.FileBytes)
It's how I used it in my situation, but you will find plenty of examples on how it work

UpdateProgress with an UpdatePanel

I want to show the UpdateProgress on page A when a user clicks on the "Next" button to go to next page. The next page is Page B, which has heavy data loading.
When the button is clicked, it doesn't show the UpdateProgress.
What's missing from this code, and how can it be made to show?
<asp:UpdateProgress ID="UpdateProgress1" runat="Server" AssociatedUpdatePanelID="UpdatePanel1">
<ProgressTemplate >
Please wait ...
</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnNext" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:Button ID="btnCancel" runat="server" TabIndex="1" Text="Cancel"onclick="btnCancel_Click" />
<asp:Button ID="btnNext" runat="server" TabIndex="2" Text="Next" onclick="btnNext_Click" />
</ContentTemplate>
</asp:UpdatePanel>
Try setting DisplayAfter to a very small value to make the progress indicator appear immediately, e.g.:
<asp:UpdateProgress ID="UpdateProgress1" runat="Server" AssociatedUpdatePanelID="UpdatePanel1" DisplayAfter="1">
Couple of things to try:
1) Move the UpdateProgress control inside the UpdatePanel
2) Remove the AssociatedUpdatePanelID attribute from the UpdateProgress tag
I'm banking on Option 1 doing the trick.
EDIT
Here is a non-ProgressTemplate way, using client-side event handlers:
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function BeginRequestHandler(sender, args)
{
// some code to show image, e.g:
document.getElementById('somedivwhichasimage').className = 'show';
}
function EndRequestHandler(sender, args)
{
// some code to hide image, e.g:
document.getElementById('somedivwhichasimage').className = 'hidden';
}
</script>
Add this code on the code behind of this page.
protected void btnNext_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(3000);
}
Hope this helps!!
Edit:
Follow this link:
http://msdn.microsoft.com/en-us/library/bb386421.aspx
Adding the code from aspx page that I tried and is working,
<asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager>
<asp:UpdateProgress ID="UpdateProgress1" runat="Server" AssociatedUpdatePanelID="UpdatePanel1">
<ProgressTemplate >
<asp:Label ID="lblwait" runat="server" Text="Please wait.."></asp:Label>
</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:Button ID="btnCancel" runat="server" TabIndex="1" Text="Cancel" />
<asp:Button ID="Button1" runat="server" TabIndex="2" Text="Next" onclick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
try putting UpdateProgress control inside UpdatePanel. and that should work for you.
hope that helps!

Resources