Para quienes no sepan que es SmartNavigation, es una funcionalidad que nos permite mantener la posicion de la barra de scroll luego de cada PostBack. Esto nos es muy util cuando tenemos Abms muy largos o solo queremos decir que somos re capos y mantenemos el scroll luego de cada postback. tagPrefix:SmartNavigation
Código:
using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace Petrobras.CDS.Framework.Controls.Web
{
[ToolboxData("<{0}:SmartNavigation runat=server></{0}:SmartNavigation>")]
public class SmartNavigation: ViewstatedControl
{
public SmartNavigation()
{
//
// TODO: Add constructor logic here
//
}
private const string XPosId = "XPos";
private const string YPosId = "YPos";
private void CreateJavaScript()
{
string NL = Environment.NewLine;
StringBuilder script = new StringBuilder();
script.Append("<script type=\"text/javascript\">" + NL);
script.Append("function SetScrollerPosition()" + NL);
script.Append("{" + NL);
script.Append("\tif (document.getElementById('" + XPosId + "').value != '')" + NL);
script.Append("\t{" + NL);
script.Append("\t\tvar x = document.getElementById('" + XPosId + "').value;" + NL);
script.Append("\t\tvar y = document.getElementById('" + YPosId + "').value;" + NL);
script.Append("\t\twindow.scrollTo(x, y);" + NL);
script.Append("\t}" + NL);
script.Append("\tdocument.getElementById('" + XPosId + "').value = '0';" + NL);
script.Append("\tdocument.getElementById('" + YPosId + "').value = '0';" + NL);
script.Append("}" + NL);
script.Append("function GetScrollerPosition()" + NL);
script.Append("{" + NL);
script.Append("\tvar scrollX, scrollY;" + NL);
script.Append("\tif (document.all)" + NL);
script.Append("\t{" + NL);
script.Append("\tif (!document.documentElement.scrollLeft)" + NL);
script.Append("\t\tscrollX = document.body.scrollLeft;" + NL);
script.Append("\telse" + NL);
script.Append("\t\tscrollX = document.documentElement.scrollLeft;" + NL);
script.Append("\tif (!document.documentElement.scrollTop)" + NL);
script.Append("\t\tscrollY = document.body.scrollTop;" + NL);
script.Append("\telse" + NL);
script.Append("\t\tscrollY = document.documentElement.scrollTop;" + NL);
script.Append("\t}" + NL);
script.Append("\telse" + NL);
script.Append("\t{" + NL);
script.Append("\t\tscrollX = window.pageXOffset;" + NL);
script.Append("\t\tscrollY = window.pageYOffset;" + NL);
script.Append("\t}" + NL);
script.Append("\tdocument.getElementById('" + XPosId + "').value = scrollX;" + NL);
script.Append("\tdocument.getElementById('" + YPosId + "').value = scrollY;" + NL);
script.Append("}" + NL);
script.Append("if(window.addEventListener)" + NL);
script.Append("\twindow.addEventListener('onscroll',GetScrollerPosition,false);" + NL);
script.Append("else if(window.attachEvent)" + NL);
script.Append("\twindow.attachEvent('onscroll',GetScrollerPosition);" + NL);
script.Append("else" + NL);
script.Append("\twindow.onscroll = GetScrollerPosition;" + NL);
script.Append("SetScrollerPosition();" + NL);
script.Append("</script>" + NL);
if(!Page.IsStartupScriptRegistered("SmartNavigation"))
Page.RegisterStartupScript("SmartNavigation",script.ToString());
}
protected override void OnPreRender(EventArgs e)
{
CreateJavaScript();
base.OnPreRender (e);
}
public int XValue
{
get
{
if(!Page.IsPostBack)
return 0;
try
{
if(Page.Request.RequestType == "GET")
return int.Parse(Page.Request.QueryString[XPosId]);
else if(Page.Request.RequestType == "POST")
return int.Parse(Page.Request.Form[XPosId]);
else
return 0;
}
catch(System.Exception)
{
return 0;
}
}
}
public int YValue
{
get
{
if(!Page.IsPostBack)
return 0;
try
{
if(Page.Request.RequestType == "GET")
return int.Parse(Page.Request.QueryString[YPosId]);
else if(Page.Request.RequestType == "POST")
return int.Parse(Page.Request.Form[YPosId]);
else
return 0;
}
catch(System.Exception)
{
return 0;
}
}
}
protected override void Render(HtmlTextWriter writer)
{
Page.VerifyRenderingInServerForm(this);
writer.WriteLine("<input type=\"hidden\" name=\"" + XPosId +
"\" id=\"" + XPosId + "\" value=\"" + XValue + "\" />");
writer.WriteLine("<input type=\"hidden\" name=\"" + YPosId +
"\" id=\"" + YPosId + "\" value=\"" + YValue + "\" />");
}
}
}
Su forma de uso es simple:
<tagPrefix:SmartNavigation id="smartNav" runat="server"></tagPrefix:SmartNavigation>
1 comentario:
Pero que codigo pilasss
Publicar un comentario