Maintain scroll position when update panel is in master page - asp.net

I have script manager and update panel placed in master page.
<div id="nav">
<asp:ScriptManager ID="ScriptManager1" runat="server"/>
</div>
<div id="mainchild">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:ContentPlaceHolder ID="HdrContentPlaceholderBody" runat="server">
</asp:ContentPlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
</div>
so this div is in master page and my child page has a grid view placed inside a panel.
<asp:Content ID="pagecontent" ContentPlaceHolderID="HdrContentPlaceholderBody" runat="server">
<asp:Panel ID="pnlAssignRole" runat="server" CssClass="popuppnl" Visible="false">
<div class="close-image">
<asp:ImageButton ID="ImageButton2" ToolTip="Close" runat="server" ImageUrl="~/App_Themes/Images/Close1.png" OnClick="btnAsgnCancel_Click" />
</div>
<table width="100%">
<tr>
<td>
<div style="height: 600px; overflow: auto;">
<asp:GridView ID="grdEmpAssigned">
gridview content
</GridView>
</div>
</td>
</tr>
</table>
</asp:Content>
in gridview on rowcommand it causes partial postback , i tried putting this piece of javascript below the updatepanel and also below scriptmanager in master page and in content page also, it did not help.. i think i am not getting where to place this javascript
<script type="text/javascript">
var xPos, yPos;
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_beginRequest(BeginRequestHandler);
prm.add_endRequest(EndRequestHandler);
function BeginRequestHandler(sender, args) {
try {
yPos = $get('ctl00_MainContent_scroll').scrollTop;
}
catch (err) { }
}
function EndRequestHandler(sender, args) {
try {
$get('ctl00_MainContent_scroll').scrollTop = yPos;
}
catch (err) { }
}
</script>
please guide me in this.

Please go thorugh this link. That might help you.
http://dotnetcrunch.wordpress.com/2011/08/05/maintain-scroll-position-for-the-whole-page-after-asynchronous-postback/
Here is the code from above link. Put it in your header section of site.
<script type=”text/javascript”>
var xPos, yPos;
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(EndRequestHandler);
function BeginRequestHandler(sender, args) {
xPos = document.body.scrollLeft;
yPos = documnet.body.scrollTop;
}
function EndRequestHandler(sender, args) {
document.body.scrollLeft = xPos;
document.body.scrollTop = yPos;
}
</script>

After several tryouts/errors and solutions which works only for first timer update event.
Scroll position was maintained but if you scroll after that, scroll position was always restored to first one!
Only working solution for me is:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<script type="text/javascript">
var yPos;
var ignoreNextScrollEvent = false;
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_beginRequest(BeginRequestHandler);
prm.add_endRequest(EndRequestHandler);
window.addEventListener('scroll', (event) => {
if (ignoreNextScrollEvent) {
ignoreNextScrollEvent = false; // Ignore this event because it was done by AsyncPostBackTrigger
return;
}
yPos = document.documentElement.scrollTop;
//console.log('scroll: ' + yPos); // debug only
});
function BeginRequestHandler(sender, args) {
ignoreNextScrollEvent = true;
//console.log('auto begin: ' + yPos); // debug only
}
function EndRequestHandler(sender, args) {
ignoreNextScrollEvent = true;
document.documentElement.scrollTop = document.body.scrollTop = yPos;
//console.log('auto end: ' + yPos); // debug only
}
</script>
<asp:UpdatePanel ID="pnl_update" runat="server">
<ContentTemplate>
// auto updated content ...
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="tmr_update" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Timer ID="tmr_update" runat="server" Interval="5000" OnTick="tmr_update_Tick"></asp:Timer>
Hope this helps someone ...

Related

Jquery not called while using updatepanel in asp.net

I have web application under which I am using update panel of some part of website. Update panel working properly but problem is there is jquery which is not working when I use update panel. First my jquery was inside update panel. It didn't worked so I tried to put it outside updatepanel But that too didn't worked. Is there additional things required to make it work if updatepanel used. Following is my code
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<input id="decrement" type ="button" value="-" class="add-sub-button" />
<asp:TextBox ID="quantity" ClientIDMode="Static" Text="1" CssClass="quantity" runat="server"></asp:TextBox>
<input id="increment" type ="button" value="+" class="add-sub-button" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="medicinesList" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
<script type="text/javascript">
$(document).ready(function () {
$("#increment").click(function () {
if ($('#quantity').val() != "90") {
var $n = $("#quantity");
$n.val(Number($n.val()) + 1);
}
});
$("#decrement").click(function () {
if ($('#quantity').val() != "1") {
var $n = $("#quantity");
$n.val(Number($n.val()) - 1);
}
});
});
</script>
use your java script like
<script type="text/javascript">
$(document).ready(function () {
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
Sys.WebForms.PageRequestManager.getInstance().beginAsyncPostBack();
function EndRequestHandler(sender, args) {
$("#increment").click(function () {
if ($('#quantity').val() != "90") {
var $n = $("#quantity");
$n.val(Number($n.val()) + 1);
}
});
$("#decrement").click(function () {
if ($('#quantity').val() != "1") {
var $n = $("#quantity");
$n.val(Number($n.val()) - 1);
}
});
}
});
</script>
Any events directly bound to DOM elements inside the UpdatePanel are lost on post backs. You will therefore need to use Delegated Events to ensure the desired behaviour is preserved.
This is done by binding the events to a container outside of the UpdatePanel and specifying a selector paremeter (representing the actual target) using the jQuery on() method instead of click().
selector - A selector string to filter the descendants of the selected elements that will call the handler. If the selector is null or omitted, the handler is always called when it reaches the selected element.
<div id="myPanel">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<input id="decrement" type="button" value="-" class="add-sub-button" />
<asp:TextBox ID="quantity" ClientIDMode="Static" Text="1" CssClass="quantity" runat="server"></asp:TextBox>
<input id="increment" type="button" value="+" class="add-sub-button" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="medicinesList" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
<script type="text/javascript">
$(document).ready(function() {
$("#myPanel").on("click", "#increment", function() {
if ($("#quantity").val() != "90") {
var $n = $("#quantity");
$n.val(Number($n.val()) + 1);
}
});
$("#myPanel").on("click", "#decrement", function() {
if ($("#quantity").val() != "1") {
var $n = $("#quantity");
$n.val(Number($n.val()) - 1);
}
});
});
</script>

How to update a status label while processing something on the server?

I have a requirement to process (server side) a lot of data (files) when the user clicks a button. I'd like to show a running summary of each file name as it's being processed. I've been trying to do it with the UpdatePanel control but only the very last update happens. Here's some simple code I created to simulate the issue (it should count up from 1 to 10 but instead waits the 5 seconds and outputs 10):
<%# Page Language="C#" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager1.RegisterAsyncPostBackControl(Button1);
}
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 1; i <= 10; i++)
{
Label1.Text = i.ToString();
System.Threading.Thread.Sleep(500);
UpdatePanel1.Update();
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
Is there a way to make this work? Or maybe a better way to do it?
Thanks in advance,
Jason
You'll need to use an ajax call to the server. I have copied this code from one of my previous projects it's a bit long code. try it and let me know if it works.
page1.aspx
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<script type="text/javascript">
function BeginProcess() {
// Create an iframe.
var iframe = document.createElement("iframe");
// Point the iframe to the location of
// the long running process.
iframe.src = "Process.aspx";
// Make the iframe invisible.
iframe.style.display = "none";
// Add the iframe to the DOM. The process
// will begin execution at this point.
document.body.appendChild(iframe);
// Disable the button and blur it.
document.getElementById('trigger').blur();
}
function UpdateProgress(PercentComplete, Message) {
document.getElementById('ContentPlaceHolder2_lbDownload').setAttribute("disabled", "true");
document.getElementById('trigger').value = PercentComplete + '%: ' + Message;
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<input type="submit" value="Start BackUp Process!"
id="trigger" onclick="BeginProcess(); return false;"
style="width: 250px;" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1"
AssociatedUpdatePanelID="UpdatePanel1" runat="server">
<ProgressTemplate>
</ProgressTemplate>
</asp:UpdateProgress>
</asp:Content>
Process.aspx.cs
public partial class Process : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
StringBuilder SB = new StringBuilder();
// Padding to circumvent IE's buffer.
Response.Write(new string('*', 256));
Response.Flush();
// Initialization
UpdateProgress(0, "Initializing task.");
try
{
foreach (yourloophere)
{
UpdateProgress(increment, db.Name + " Backup Started....");
//your process code
UpdateProgress(increment, db.Name + " Backup Completed!");
//your process code
SB.Append(db.Name + "BackUp Complted!");
//your process code
SB.Append("<br/>");
}
// All finished!
UpdateProgress(100, "All Database BackUp Completed!");
}
catch (Exception ex)
{
UpdateProgress(0, "Exception: " + ex.Message);
SB.Append("Back Up Failed!");
SB.Append("<br/>");
SB.Append("Failed DataBase: " + DBName);
SB.Append("<br/>");
SB.Append("Exception: " + ex.Message);
}
}
protected void UpdateProgress(double PercentComplete, string Message)
{
// Write out the parent script callback.
Response.Write(String.Format("<script type=\"text/javascript\">parent.UpdateProgress({0}, '{1}');</script>", PercentComplete, Message));
// To be sure the response isn't buffered on the server.
Response.Flush();
}
}

How to scroll to selected in a DataGrid?

I'm displaying a Datagrid like this and inviting the user to make a selection...
<div id="gradesDiv" style="overflow: auto; width: 380px; height: 300px">
<asp:DataGrid id="gradesGrid"
BorderWidth="1"
CellPadding="3"
AutoGenerateColumns="true"
runat="server">
<Columns>
<asp:ButtonColumn HeaderText="Select Item"
ButtonType="LinkButton"
Text="Select"
CommandName="Select">
</asp:ButtonColumn>
</Columns>
</asp:DataGrid>
</div>
(three other columns are added in the code-behind). But when the user makes a selection, a postback is performed and the scroll position lost. I'd like to be able to reset the div to display the selected item. Does anyone know how to do this?
I've tried adding
MaintainScrollPositionOnPostback="true"
to the asp, but it doesn't help. I attempted to maintain the scroll position in the codebehind but gradesDiv does not appear to be available to the code-behind.
There's quite a few creative approaches on the web on how this can be done.Just search for - maintaining div scroll position on postback.This is just one of such examples:
<script type="text/javascript">
$(document).ready(function () {
var xPos, yPos;
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_beginRequest(BeginRequestHandler);
prm.add_endRequest(EndRequestHandler);
var divId = 'gradesDiv';
function BeginRequestHandler(sender, args) {
xPos = $get(divId).scrollLeft;
yPos = $get(divId).scrollTop;
}
function EndRequestHandler(sender, args) {
$get(divId).scrollLeft = xPos;
$get(divId).scrollTop = yPos;
}
});
</script>

Maintain Panel Scroll Position On Partial Postback ASP.NET

I have a gridview that putted in ASP.NET Panel.
both of panel and Gridview are in an UpdatePanel.
there is a column in gridview that Causes Partial PostBacks.
i want to Maintain Panel Scroll position on those postbacks.
Is there any way?
regards.
There is no built-in facility to resolve it in asp.net
However, there is a workaround for this problem; You need to handle it with javascript.
Solution is mentioned here: Maintain Scrollbar Position Inside UpdatePanel After Partial PostBack
Edited 20-May-2012; after seeing the comments
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" ScriptMode="Release" />
<script type="text/javascript">
// It is important to place this JavaScript code after ScriptManager1
var xPos, yPos;
var prm = Sys.WebForms.PageRequestManager.getInstance();
function BeginRequestHandler(sender, args) {
if ($get('<%=Panel1.ClientID%>') != null) {
// Get X and Y positions of scrollbar before the partial postback
xPos = $get('<%=Panel1.ClientID%>').scrollLeft;
yPos = $get('<%=Panel1.ClientID%>').scrollTop;
}
}
function EndRequestHandler(sender, args) {
if ($get('<%=Panel1.ClientID%>') != null) {
// Set X and Y positions back to the scrollbar
// after partial postback
$get('<%=Panel1.ClientID%>').scrollLeft = xPos;
$get('<%=Panel1.ClientID%>').scrollTop = yPos;
}
}
prm.add_beginRequest(BeginRequestHandler);
prm.add_endRequest(EndRequestHandler);
</script>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server" Height="300">
<%-- Some stuff which would cause a partial postback goes here --%>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</form>
Below is the code snapshot:-
Add MaintainScrollPositionOnPostback="true" to your page directive.
I was looking for an answer to this problem for several days, using the typical alternative of MaintainScrollPositionOnPostback and the JavaScript solutions using BeginRequestHandler and EndRequestHandler where in my case I use MasterPage.
Nothing worked, however I came up with a fairly simple solution using jQuery with BeginRequestHandler and EndRequestHandler using the same #waqas-raja algorithm:
<script type="text/javascript">
var scrollPosition = 0;
$(document).ready(function () {
$(window).scroll(function (event) {
scrollPosition = $(window).scrollTop();
});
});
</script>
<script type="text/javascript">
// It is important to place this JavaScript code after ScriptManager1
var xPos, yPos;
var prm = Sys.WebForms.PageRequestManager.getInstance();
function BeginRequestHandler(sender, args) {
console.log('BeginRequest');
}
function EndRequestHandler(sender, args) {
$(window).scrollTop(scrollPosition);
}
prm.add_beginRequest(BeginRequestHandler);
prm.add_endRequest(EndRequestHandler);
</script>
The idea is to capture the position of the Scroll in a global variable each time the user moves the Scroll, in this way it is known which was the last position and when making the postback the EndRequestHandler event is entered and updated with the last position what the user marked
This worked for me in Firefox and Google Chrome :)
This Solution Helped me.Paste this code below ScriptManager inside the form tag
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<script type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_beginRequest(beginRequest);
function beginRequest() {
prm._scrollPosition = null;
}
</script>
Refer to this original Post and answer
ASP.NET: Timer and scroll position

How to display value in the SimpleModal's dialog?

my question is really simple. I have a asp.net button. I can use it to call the simpleModal and have a dialog displayed. Now, I added a label control in the dialog, and would like this label to display some value. What should I do?
Here is my codes
$('#<%= btnOpen.ClientID %>').click(function(e) {
e.preventDefault();
$('#content').modal({
onOpen: function(dialog) {
dialog.overlay.fadeIn('slow', function() {
dialog.data.hide();
dialog.container.fadeIn('slow', function() {
dialog.data.slideDown('slow');
});
});
},
onClose: function(dialog) {
dialog.data.fadeOut('slow', function() {
dialog.container.slideUp('slow', function() {
dialog.overlay.fadeOut('slow', function() {
$.modal.close(); // must call this!
});
});
});
}
});
e.preventDefault();
// return false;
});
<asp:Button ID="btnOpen" runat="server" Text="ASP.NET Open"/>
<div id="content" style="display: none;">
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
I assume since you said that your question is simple that you just have an unfamiliarity with jQuery. You can put this in your click function, or in the $(document).ready function, depending on your full requirements:
var yourValue = ; // put your function or value here
$('#Label1').text(yourValue);
Note: You'll need to use .html instead of .text if you have a string with tags, but .text is faster.
Lol, I am answering my own question again, but I will give credit to mNVhr tho.
I finally get the whole thing work. The trick for asp.net button to fire a postback, along with javascript's postback, is to put the asp.net button into an update panel. Here is the code I have
For the javascript part:
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="js/jquery.simplemodal-1.3.5.js" type="text/javascript"></script>
<script type="text/javascript">
function myOpen() {
$('#content').modal({
onOpen: function(dialog) {
dialog.overlay.fadeIn('slow', function() {
dialog.data.hide();
dialog.container.fadeIn('slow', function() {
dialog.data.slideDown('slow');
});
});
},
onClose: function(dialog) {
dialog.data.fadeOut('slow', function() {
dialog.container.slideUp('slow', function() {
dialog.overlay.fadeOut('slow', function() {
$.modal.close();
});
});
});
}
});
}
function myClose() {
$.modal.close();
}
</script>
For the HTML markup
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="btnOpen" runat="server" Text="Open" OnClick="btnOpen_Click" OnClientClick="myOpen();" />
</ContentTemplate>
</asp:UpdatePanel>
<div id='content' style="display: none">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
<input id="Button2" type="button" value="Close" onclick="myClose();" />
<asp:Label ID="Label2" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
For the code behind:
protected void Page_Load(object sender, EventArgs e)
{
}
private void CloseDialog()
{
string script = string.Format(#"myClose()");
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), UniqueID, script, true);
}
protected void btnSave_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "1")
CloseDialog();
else
Label2.Text = TextBox1.Text;
}
protected void btnOpen_Click(object sender, EventArgs e)
{
TextBox1.Text = DateTime.Now.ToString();
UpdatePanel1.Update();
}
I hope this tiny code can help those asp.net developer who want to use the nice jQuery in their projects.
As you can see, from the above codes.
When I click on the btnOpen button, two postbacks fired. One is from the asp.net code behind, which assign current datetime to the textbox control inside the modal dialog. The second postback is from the javascript, which open the modal dialog. The asp.net button has to be inside the update panel. Otherwise, the modal dialog will only stay for about 0.5 second.
When I click on the btnSave inside the modal dialog. Postback also occurred. I have a little logic here. When the textbox's value is 1, I call the closeDialog() function. When the value is other numbers, the modal dialog stay opening, and the label control inside the dialog will display the number from the text box.
jQuery is nice, but as a .Net developer, it is just new, and sometimes difficult for me to understand it, especially for the conflict of postbacks between javascript and .net.
I hope this answer is helpful.

Resources