I have an ASP.net WebForms page that has a lot of content on the top of the screen. It has a link button that will post back to the page and show another section of the page. When the page refreshes, I would like to set focus and scroll down to this section of the page.
I tried doing
txtField.Focus()
in my code behind and it will set focus and try to scroll there, but then scrolls right back to the top. The focus is still on my text box but the position of the screen is at the very top. The Link is at the top of the screen which is causing the postback. I want to scroll to the very bottom of the screen. It does this briefly and then scrolls right back to the top.
I have tried setting
Page.MaintainScrollPositionOnPostback = false;
but that doesn't seem to help either.
Is there some way I can force it to go to a specific position?
Is it possible to add an anchor tag to the URL when I postback using a button or link button?
Page.MaintainScrollPositionOnPostBack = true; should take you back to the same position on the screen, but you could use AJAX, or you could use SetFocus() to focus on a specific control after the postback:
http://msdn.microsoft.com/en-us/library/ms178232.aspx
You can use the code below if you have an anchor for the location:
Page.ClientScript.RegisterStartupScript(this.GetType(), "hash", "location.hash = '#MOVEHERE';", true);
In your case I suggest you to keep the default value of Page.MaintainScrollPositionOnPostBack, and use the pure javascript scrolling function
function scrollToDiv()
{
document.getElementById('yourDiv').scrollIntoView();
}
And call it at the page startup with a little delay of 1ms (pure javascript again)
setTimeout(scrollToDiv, 1);
And finally call it from the C# code behind, with the RegisterStartupScript (js executed after all the page has been loaded) :
ScriptManager.RegisterStartupScript(Page, typeof(Page), "ScrollToADiv", "setTimeout(scrollToDiv, 1);", true);
Like this, it will bypass any asp automatic scrolling
try this
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack) {
string targetId = Page.Request.Params.Get("__EVENTTARGET");
Page.ClientScript.RegisterStartupScript(this.GetType(), "focusthis", "document.getElementById('" + targetId + "').focus()", true);
}
}
Page.MaintainScrollPositionOnPostback = true seems to work just fine.
I've tried Matthieu Charbonnier answer, but it didn't work unless I've added
" window.scrollTo = function () { };"
as it was suggested in http://gnidesign.blogspot.com.au/2011/06/how-to-maintain-page-scroll-on-postback.html
I've created a helper method, that's working in Chrome,FireFox and IE
public static void ScrollToControl( Page page, string clientId, bool alignToTop)
{
//NOTE: if there are more than one call on the page, first one will take preference
//If we want that last will take preference, change key from MethodBase.GetCurrentMethod().Name to anchorName
//recommended in http://gnidesign.blogspot.com.au/2011/06/how-to-maintain-page-scroll-on-postback.html
String script = " window.scrollTo = function () { };" + Environment.NewLine;
script += String.Format("document.getElementById('{0}').scrollIntoView({1});" , clientId, alignToTop.JSToString());
page.ClientScript.RegisterStartupScript(TypeForClientScript(), MethodBase.GetCurrentMethod().Name, script, true );
//return script;
}
public static string JSToString(this bool bValue)
{
return bValue.ToString().ToLower();
}
Use getElementById('{0}').scrollIntoView is simpler than location.hash , because you don't need to add extra anchor element.
Parameter alignToTop is very convenient to specify do you want to show control at the top or bottom of the screen.
I have
<asp:MultiView ID="mvAriza" runat="server">
<asp:View ID="View14" runat="server">
............ .......
</asp:View>
</asp:MultiView>
on *.aspx page. And on the *.aspx.cs page on a button click.
Page.SetFocus(mvAriza.ClientID);
It works great.
This scroll automatically to desired Div in asp.net Control
This is Function
call it from Where you Want
and also Download Java script file
OnClientClick="return scrollGrid()"
function scrollGrid1() {
$('html,body').animate
(
{
scrollTop: $('#Div1').offset().top
},
'slow'
)
}
While not elegant for your situation, it is also possible to use dummy Custom Validators, set the one you want to scroll to as invalid then do
DummyCustomValidator.SetFocusOnError = true;
In my case, I am actually using page Validators with async postbacks and multiple programmatically shown/hidden panels on a long vertical form. Since some fields are only required if MyLogicalParent.Visible = true and if specific answers are given in other controls, such as a RequiredFieldValidator on a TextBox when "Other" is selected in a CheckBoxList, I have a LOT of logic to process page validation. Setting scroll positions was painful in all of the normal methods.
I also use RegisterStartupScript to handle maintaining the current scroll position when async postbacks alter the page's vertical dimension.
<script type="text/javascript">
$(document).ready(function () {
var windowHeight = $(document)[0].documentElement.clientHeight; /*This is the height of the viewable browser area.*/
var scrolledPosition = $(window)[0].scrollY; /*This is the number of Pixels the Window is currently scrolled to.*/
var scroll = windowHeight + scrolledPosition; /*This should be the same as $(window).scrollTop() */
/*If the amount scrolled + the height of the window is Less than or equal to the total height of the page (past the viewable client window)*/
if ($(window).scrollTop() + getWindowSize()[1] <= getDocHeight()) {
/*Move the morescroll div to the bottom of the page... -34 is the height of the div plus a small bottom margin.*/
$("#morescroll").offset({ top: windowHeight - 34 });
}
})
/*This is the total height of the document including the area past the viewable client window.*/
function getDocHeight() {
var D = document;
/*The Largest of these six numbers is the total doc height. */
return Math.max(
D.body.scrollHeight, D.documentElement.scrollHeight,
D.body.offsetHeight, D.documentElement.offsetHeight,
D.body.clientHeight, D.documentElement.clientHeight
);
}
/*This is the width and height of the Viewable Browser area.*/
function getWindowSize() {
var myWidth = 0, myHeight = 0;
if (typeof (window.innerWidth) == 'number') {
//Non-IE
myWidth = window.innerWidth;
myHeight = window.innerHeight;
} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
//IE 6+ in 'standards compliant mode'
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
//IE 4 compatible
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}
return [myWidth, myHeight];
}
//This sets a transparent div <div id="morescroll" class="scrollMinder"> with the text "Scroll down for more." to the bottom of the viewable page.
$(window).scroll(
function () {
var windowHeight = $(document)[0].documentElement.clientHeight;
var scrolledPosition = $(window)[0].scrollY;
var scrll = windowHeight + scrolledPosition;
document.getElementById('<%= HF_LastScrolled.ClientID %>').value = scrolledPosition;
var docHeight = $(document)[0].documentElement.scrollHeight;
/*if we are scrolled to within 60 pixels from the bottom of the document, hide the indicator so it doesn't cover the footer.*/
if ($(window).scrollTop() + $(window).height() >= $(document).height() - 60) {
$("#morescroll").hide();
}
/*if we scroll back above 60 pixels from the bottom of the document, show the indicator and set the top of the div to -34 pixels.*/
else if ($(window).scrollTop() + getWindowSize()[1] <= getDocHeight()) {
$("#morescroll").show();
$("#morescroll").offset({ top: scrll - 34 });
}
});
</script>
<%-- This stores the Y scroll location.--%>
<asp:HiddenField ID="HF_LastScrolled" runat="server" />
<div id="morescroll" class="scrollMinder">
<span class="spanMinder">Scroll down for more.</span>
</div>
private string LastScrolled = "";
protected void Page_PreRender (object sender, EventArgs e)
{
if (string.IsNullOrEmpty(LastScrolled))
{
LastScrolled = "0";
}
if (string.IsNullOrEmpty(scrollPosition))
{
sb.Clear();
sb.AppendLine("Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);");
sb.AppendLine("function EndRequestHandler(sender, args) {");
sb.Append("scrollTo(0, ").Append(LastScrolled).Append(");");
sb.AppendLine("}");
sb.AppendLine("function load() {");
sb.AppendLine("Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);");
sb.AppendLine("}");
cs.RegisterStartupScript(GetType(), "ScrollToLastPosition", sb.ToString(), true);
scrollPosition = "ScrollToLastPosition";
}
if (!string.IsNullOrEmpty(scrollPosition))
{
ScriptManager.RegisterStartupScript(this, GetType(), scrollPosition, sb.ToString(), true);
}
}
protected void Page_Load (object sender, EventArgs e)
{
LastScrolled = HF_LastScrolled.Value;
Page.MaintainScrollPositionOnPostBack = false;
}
protected void SetScrollToLastPosition ()
{
sb.Clear();
sb.AppendLine("Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);");
sb.AppendLine("function EndRequestHandler(sender, args) {");
sb.Append("scrollTo(0, ").Append(LastScrolled).AppendLine(");");
sb.AppendLine("}");
sb.AppendLine("function load() {");
sb.AppendLine("Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);");
sb.AppendLine("}");
cs.RegisterStartupScript(GetType(), "ScrollToLastPosition", sb.ToString(), true);
scrollPosition = "ScrollToLastPosition";
string tempstring = sb.ToString();
ScriptManager.RegisterStartupScript(this, GetType(), scrollPosition, sb.ToString(), true);
}
protected void SetScrolltoPositionY (int y)
{
sb.Clear();
sb.AppendLine("Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);");
sb.AppendLine("function EndRequestHandler(sender, args) {");
sb.Append("scrollTo(0, ").Append(y).AppendLine(");");
sb.AppendLine("}");
sb.AppendLine("function load() {");
sb.AppendLine("Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);");
sb.AppendLine("}");
cs.RegisterStartupScript(GetType(), "ScrollTo-0-" + y.ToString(), sb.ToString(), true);
scrollPosition = "ScrollTo - 0-" + y.ToString();
string tempstring = sb.ToString();
ScriptManager.RegisterStartupScript(this, GetType(), scrollPosition, sb.ToString(), true);
}
Related
Is there a way to make an Iframe expandable as needed? I have a web page with an Iframe to a second web page on a same server. The second web page has an expandable table. What I would like is when someone views the second page throught the Iframe and clicks on the table to expand, the Iframe will also expand to fit the size.
<script type="text/javascript">
//<![CDATA[
window.onload = function() {
var f = document.getElementById("mainframe");
function resize() {
var h = "";
var w = "";
if (f.contentDocument) {
h = f.contentDocument.documentElement.offsetHeight + 20 + "px";
(f.contentDocument.documentElement,"").getProperty Value
("width");
} else if (f.contentWindow) {
h = f.contentWindow.document.body.scrollHeight + 5 + "px";
} else {
return;
}
f.setAttribute("height",h);
f.parentNode.setAttribute("height",h);
}
if (window.addEventListener) {
f.onload = resize;
} else if (f.attachEvent) {
f.attachEvent("onload", resize);
} else {
return;
}
resize();
}
//]]>
</script>
<iframe name="frm" id="mainframe" src="URL HERE" frameborder="0" allowtransparency="no" scrolling="no" target="_self" height="150"></iframe>
And here's the HTML code:
Name Of Page
If the page is in same domain in that case following solution should work.
Make iframe automatically adjust height according to the contents without using scrollbar?
RadWindow not open when i click over the button
My javascript code:
<script type="text/javascript">
function OpenURL(URL) {
var oWnd1 = radopen(URL, "Detalhamento do Contrato");
oWnd1.set_height("600px");
oWnd1.set_width("600px");
oWnd1.center();
oWnd1.set_modal(true);
}
</script>
My C# code:
protected void RadGrid_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName.Equals("Detalhes"))
{
String Parametros = "ContratoNumero=" + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CONT_NUMERO"];
Parametros += "&AditivoNumero=" + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CONT_NUMADITIVO"];
Parametros += "&ContratoTipo=" + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CONT_TIPO"];
Parametros += "&Exercicio=" + this.txtExercicio.Text;
string URL = "ContratoDetalhamento.aspx?" + Parametros;
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "relatorio", String.Format("OpenURL('{0}','{1}');", URL, DateTime.Now.Ticks), true);
}
}
Can someone help me ?
Thanks for now
You are missing:
oWnd1.SetActive();
1) look here on executing the script properly: http://www.telerik.com/help/aspnet-ajax/radwindow-troubleshooting-javascript-from-server-side.html.
2) look here on the proper radopen() syntax: http://www.telerik.com/help/aspnet-ajax/window-programming-opening.html.
Here is how I would do it:
function OpenURL(URL) {
var oWnd1 = radopen(URL, "RadWindowName");//note, no spaces
oWnd1.setSize(600, 600);//width, height
oWnd1.set_modal(true);//making it modal will center it by default
}
Now, make sure you execute the code late enough:
string script = "function(f){openURL('url'); Sys.Application.remove_load(f);}Sys.Application.add_load(f);";
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "someKey", script, true);
Hi I want to share with you my solution to create RadWindow dialog in Javascript code only.
We need to implement 2 methods: one for initializing RadWindow dialog, and the last one for recieving the arguments returned after closing the RadWindow. You can do what you want in this second step (e.x postback,...)
Here is my code:
Initializing RadWindow dialog:
function openMyDialog(url, args) {
var manageWindow = GetRadWindowManager();
if (manageWindow) {
var radWindow = manageWindow.open(url, "<your_dialog_name>");
if (radWindow) {
radWindow.set_initialBehaviors(Telerik.Web.UI.WindowBehaviors.None);
radWindow.set_behaviors(Telerik.Web.UI.WindowBehaviors.Move + Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Resize);
radWindow.setActive(true);
radWindow.SetModal(true);
radWindow.center();
radWindow.set_visibleStatusbar(false);
radWindow.set_keepInScreenBounds(true);
radWindow.set_minWidth(640);
radWindow.set_minHeight(480);
radWindow.setSize(640, 480);
radWindow.set_destroyOnClose(true);
radWindow.add_close(closeMyDialog);//after closing the RadWindow, closeMyDialog will be called
radWindow.argument = args;//you can pass the value from parent page to RadWindow dialog as this line
}
}
}
Closing the RadWindow dialog:
function closeMoveProjectDialog(sender, args) {
var objArgs = args.get_argument();
//objArgs variable stored the values returned from the RadWindow
//you can use it for your purpose
}
How to call this? You can put the open method into your expected method. In my side, I have a method as shown below and I will call the RadWindow as this way:
function ShowForeignKeyFrontEditSingle(param1, param2){
var url = "ForeignKeyFrontEditSingle.aspx";
var objArgs = new Array();
objArgs[0] = param1;
objArgs[1] = param2;
openMyDialog(url, objArgs);
return;
}
Of course, you have to declare a RadWindowManager control
function GetRadWindowManager() {
return $find("<%=your_radwindow_manager_control.ClientID%>");
}
I have a content page that contains the following...
UpdatePanel1 - containing Error Display Divs
contains update triggers for both buttons
UpdatePanel2 - containing process 1 with an asp:button
updatePanel3 - containing process 2 with an asp:button
JavaScript that presents the user with a Popup confirm Jquery Messagebox based on the process they are executing.
UpdatePanel 2 or 3 becomes visible based on the users selection from the menu options.
When I click a button the messagebox pops and the page is processed correctly using __doPostback from the messagebox response and the page does a full postback.
I would rather the page do a partial postback and the content it had and display the Error Display Divs if there was an error. Any assistance would be appreciated.
nothing special about the button
<asp:Button ID="ResetSomething" runat="server" Text="ResetSomething" Width="275px" />
here is the content page script block
<script type="text/javascript" language="javascript">
<!--
function pageLoad() {
setform();
};
function setform() {
var reset1_button = $('input[id*=ResetSomething]');
var reset2_button = $('input[id*=ResetSomethingElse]');
reset1_button.click(function() {
var element = $(this);
$.prompt('Message1', { show: 'slideDown', buttons: { Ok: true, Cancel: false },
submit: function(v, m, f) { submit_reset_callback(v, m, element); }
});
return (false);
});
var submit_reset_callback = function(result, messages, element) {
if (result) { __doPostBack("ResetSomething");}
return (false);
};
reset2_button.click(function() {
var element = $(this);
$.prompt('Message2', { show: 'slideDown', buttons: { Ok: true, Cancel: false },
submit: function(v, m, f) { submit_update_callback(v, m, element); }
});
return (false);
});
var submit_update_callback = function(result, messages, element) {
if (result) { __doPostBack("ResetSomethingElse"); }
return (false);
};
};
-->
</script>
this is the code behind for the OnInit:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.PreLoad += (sender, args) =>
{
this.ClientScript.GetPostBackEventReference(this, "arg");
if (!IsPostBack) { return; }
string __targetaction = this.Request["__EVENTTARGET"];
string __args = this.Request["__EVENTARGUMENT"];
if (string.IsNullOrEmpty(__args)) return;
if (__targetaction == "ResetSomething")
{
ResetSomething();
}
if (__targetaction == "ResetSomethingElse")
{
ResetSomethingElse();
}
this.upnlNotifications.Update();
};
}
Define the function below and replace your __doPostBack calls with doPostBackAsync(controlId, null).
function doPostBackAsync(eventName, eventArgs) {
var prm = Sys.WebForms.PageRequestManager.getInstance();
if (!Array.contains(prm._asyncPostBackControlIDs, eventName)) {
prm._asyncPostBackControlIDs.push(eventName);
}
if (!Array.contains(prm._asyncPostBackControlClientIDs, eventName)) {
prm._asyncPostBackControlClientIDs.push(eventName);
}
__doPostBack(eventName, eventArgs);
}
The controlId should be the id of the button to generate async postback else full postback occurs in the page.
For that you can use the clientidmode as static e.g
<asp:Button ID="button1" runat="server" ClientIDMode="Static"/>
//then you can use below code
_doPostBack('button1', '');//clientidmode forces buttonid = id given by us
Check its correct?
If so then you can get the async postback instead of a full page postback.
This helped me.
Thanks #Tim for his comment.
I have a JavaScript MessageBox that keeps displaying when I click events in a Repeater or refresh the current page.
Here is the function:
public myFunctions()
{
}
/** Display Successs/Error Message **/
public void DisplayUserMessage(string messageType, string messageString, Label ErrorMessageLabel)
{
ErrorMessageLabel.Visible = true;
ErrorMessageLabel.Text = "<b>" + messageType.Substring(0, 1).ToUpper() + messageType.Substring(1).ToLower() + ":</b> " + messageString;
ErrorMessageLabel.CssClass = messageType.ToLower() + "_message";
}
public void HideUserMessage(Label ErrorMessageLabel)
{
ErrorMessageLabel.Visible = false;
ErrorMessageLabel.Text = "";
ErrorMessageLabel.CssClass = "";
}
Here is the jquery to make it fade out:
$(document).ready(function () {
/** Success/Error Messages **/
$('.success_message').animate({ opacity: 1.0 }, 2000).fadeOut('slow');
$('.error_message').animate({ opacity: 1.0 }, 2000).fadeOut('slow');
});
Here it is on the MasterPage:
<!-- Message Box -->
<div id="msgBox" runat="server">
<asp:Label ID="ErrorMessageLabel" CssClass="" runat="server" Visible="false"></asp:Label>
</div>
Here is the script in the code-behind when a success occurs:
Label ErrorMessageLabel = (Label)Master.FindControl("ErrorMessageLabel");
new myFunctions().DisplayUserMessage("success", "Administrator Updated!", ErrorMessageLabel);
Anyone know how I can stop it from continually showing up after I click another button or refresh the page?
Use a hidden input and set the value when success function is called. All the other times reset the value. In your document ready function check the value of the hidden element and then call the animate function.
<input id="txtHidSuccess" type="hidden" runat="server" />
In page load
txtHidSuccess.Value = "0";
In the success/error function
txtHidSuccess.Value = "1";
jQuery
$(function(){
if ($("#txtHidSuccess").val() === "1") {
/** Success/Error Messages **/
$('.success_message').animate({ opacity: 1.0 }, 2000).fadeOut('slow');
$('.error_message').animate({ opacity: 1.0 }, 2000).fadeOut('slow');
}
});
I see where you're calling DisplayUserMessage(), but not HideUserMessage(). My guess is that when you set the ErrorMessageLabel properties, they're stored in the View State persisted across postbacks.
Rather than use an ASP.NET Label control, you could try just wrapping some text with a regular div and use CSS to hide it. When the success event occurs in your app, you can use ClientScriptManager.RegisterStartupScript() to write a script to the client that shows the div. Then, your animate functions can fade out the div if it's visible.
if(IsPostBack)
new myFunctions().HideUserMessage(ErrorMessageLabel);
This worked. Thanks #Harie
I am using a treeview control. I am buliding the tree dynamically. sometimes the tree becomes larger and the down scroll bar is need to see the entire tree.
user can select a node from the tree. if one node is selected ,i change the color of the node from server side.
my problem is that if a user selected a node which is bottom in the tree(means, the user used the down scrollbar to see that node), after postback it shows the top of the tree.to see the selected node the user need to use the down scroll bar.
I need to show the selected node after postback. How can I do this?
I am using c# and asp.net
With help of jquery we can send the selected node id to the query string and on document.ready we can read back and highlight that node.
Have a look on the code:
Code behind onclick code:
public void TreeView1_OnClick(Object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(
Page,
Page.GetType(),
"HighlightSelectedNode",
"HighlightSelectedNode();",
true
);
}
and the javascript:
<script type="text/javascript" language="javascript">
function HighlightSelectedNode() {
var selectedNodeID = $('#<%= TreeView1.ClientID %>_SelectedNode').val();
if (selectedNodeID != "") {
document.location.href = "http://" + window.location.host
+ window.location.pathname
+ "?Node=" + selectedNodeID;
return false;
} else {
// alert("Not found");
}
}
// Highlight active node on pageload.
$(document).ready(function () {
var querystring = location.search.replace('?', '').split('&');
var queryObj = {};
for (var i = 0; i < querystring.length; i++) {
var name = querystring[i].split('=')[0];
var value = querystring[i].split('=')[1];
queryObj[name] = value;
}
var nodeID = queryObj["Node"];
$('#' + nodeID).css({ 'background-color': '#888'});
});
</script>
You can use update panel to work around this issue.