页面开发技巧 asp.net控件事件 以维持滚动位置 客户端回调
2010-01-06 16:03
357 查看
1、提交后的回发时能自动进入上次的当前位置
例如,如果数据项导致大型页回发,则最终用户需要将页滚动到此前正在编辑它们的位置,才能继续。页开发人员通过以下方法可以简单地标记窗体,以维持滚动位置:在
2、Button 控件的 OnClientClick 属性允许您在此按钮被单击时以编程方式运行客户端脚本。该按钮呈现客户端
3、用于控件的一个令人惊喜的新功能是“客户端回调”,该功能允许控件向服务器执行带外请求以获取附加数据,而不发送整页。此功能依赖于用于回调处理(通常通过 XMLHTTP)的浏览器支持,该支持由
其实一般的无刷新页面就可以使用客户端回调来实现,只有复杂的才需要使用AJAX等
<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeFile="CallBackEventHandler_cs.aspx.cs" Inherits="CallBackEventHandler_cs" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Client CallBack</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>Cascading DropDownLists Using ICallBackEventHandler</h3>
<asp:DropDownList ID="ParentDropDown"
onchange="GetChildren(this.options[this.selectedIndex].value, 'ddl');"
Runat="server">
<asp:ListItem Text="Item 1" />
<asp:ListItem Text="Item 2" />
<asp:ListItem Text="Item 3" />
</asp:DropDownList>
<asp:DropDownList ID="ChildDropDown" AutoPostBack="true" style="visibility:hidden" Runat="Server">
<asp:ListItem Text="Child Item" />
</asp:DropDownList>
<br /><br />
<asp:Label ID="Label1" runat="server"/>
<script type="text/javascript">
function ClientCallback(result, context)
{
var childDropDown = document.forms[0].elements['<%=ChildDropDown.UniqueID%>'];
if (!childDropDown)
{
return;
}
childDropDown.length = 0;
if (!result)
{
return;
}
var rows = result.split('|');
for (var i = 0; i < rows.length; ++i)
{
var option = document.createElement("OPTION");
option.value = rows[i];
option.innerHTML = rows[i];
childDropDown.appendChild(option);
}
childDropDown.style.visibility = "visible";
}
function ClientCallbackError(result, context)
{
alert(result);
}
</script>
</form>
</body>
</html>
source /article/4679688.html
例如,如果数据项导致大型页回发,则最终用户需要将页滚动到此前正在编辑它们的位置,才能继续。页开发人员通过以下方法可以简单地标记窗体,以维持滚动位置:在
@Page指令中将 MaintainScrollPositionOnPostBack 属性设置为 true,或在 Web.config 中进行此设置,以应用于应用程序中的所有页。
2、Button 控件的 OnClientClick 属性允许您在此按钮被单击时以编程方式运行客户端脚本。该按钮呈现客户端
onclick属性以及按钮自身的 Javascript。
3、用于控件的一个令人惊喜的新功能是“客户端回调”,该功能允许控件向服务器执行带外请求以获取附加数据,而不发送整页。此功能依赖于用于回调处理(通常通过 XMLHTTP)的浏览器支持,该支持由
SupportsClientCallbacks在浏览器功能中指定。
其实一般的无刷新页面就可以使用客户端回调来实现,只有复杂的才需要使用AJAX等
<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeFile="CallBackEventHandler_cs.aspx.cs" Inherits="CallBackEventHandler_cs" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Client CallBack</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>Cascading DropDownLists Using ICallBackEventHandler</h3>
<asp:DropDownList ID="ParentDropDown"
onchange="GetChildren(this.options[this.selectedIndex].value, 'ddl');"
Runat="server">
<asp:ListItem Text="Item 1" />
<asp:ListItem Text="Item 2" />
<asp:ListItem Text="Item 3" />
</asp:DropDownList>
<asp:DropDownList ID="ChildDropDown" AutoPostBack="true" style="visibility:hidden" Runat="Server">
<asp:ListItem Text="Child Item" />
</asp:DropDownList>
<br /><br />
<asp:Label ID="Label1" runat="server"/>
<script type="text/javascript">
function ClientCallback(result, context)
{
var childDropDown = document.forms[0].elements['<%=ChildDropDown.UniqueID%>'];
if (!childDropDown)
{
return;
}
childDropDown.length = 0;
if (!result)
{
return;
}
var rows = result.split('|');
for (var i = 0; i < rows.length; ++i)
{
var option = document.createElement("OPTION");
option.value = rows[i];
option.innerHTML = rows[i];
childDropDown.appendChild(option);
}
childDropDown.style.visibility = "visible";
}
function ClientCallbackError(result, context)
{
alert(result);
}
</script>
</form>
</body>
</html>
//----------------------------------------------------------------------- // This file is part of the Microsoft .NET SDK Code Samples. // // Copyright (C) Microsoft Corporation. All rights reserved. // //This source code is intended only as a supplement to Microsoft //Development Tools and/or on-line documentation. See these other //materials for detailed information regarding Microsoft code samples. // //THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY //KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE //IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A //PARTICULAR PURPOSE. //----------------------------------------------------------------------- using System; using System.Web.UI; public partial class CallBackEventHandler_cs : System.Web.UI.Page, ICallbackEventHandler { private string _callbackResult; private void Page_Load(object source, EventArgs e) { String callBack = Page.ClientScript.GetCallbackEventReference(this, "arg", "ClientCallback", "context", "ClientCallbackError", false); String clientFunction = "function GetChildren(arg, context){ " + callBack + "; }"; Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "GetChildren", clientFunction, true); if (Page.IsPostBack && !Page.IsCallback) { Label1.Text = "You Selected: " + Request.Form["ParentDropDown"] + ": " + Request.Form["ChildDropDown"]; } } public string GetCallbackResult() { return _callbackResult; } public void RaiseCallbackEvent(string eventArgument) { switch (eventArgument) { case "Item 1": _callbackResult = "One|Two|Three"; break; case "Item 2": _callbackResult = "Four|Five|Six"; break; case "Item 3": _callbackResult = "Seven|Eight|Nine"; break; default: _callbackResult = ""; break; } } }
source /article/4679688.html
相关文章推荐
- ASP.NET 3.5控件和组件开发技术之客户端回发/回调揭密
- Asp.net开发心得点滴[动态加载的用户控件使用事件委托,交给页面处理的事件无效问题]
- ASP.NET 3.5控件和组件开发技术之客户端回发/回调揭密
- ASP.NET 3.5控件和组件开发技术之客户端回发/回调揭密
- ASP.NET 2.0 正式版中无刷新页面(客户端回调)的开发
- ASP.NET自定义控件组件开发 第三章 为控件添加事件
- ASP.NET自定义控件组件开发 第四章 组合控件开发CompositeControl 后篇 --事件冒泡
- ASP.NET 服务器端控件响应客户端事件
- Asp.net 2.0 自定义控件开发专题[详细探讨页面状态(视图状态和控件状态)机制及其使用场景](示例代码下载)
- Asp.net 2.0 自定义控件开发专题[详细探讨页面状态(视图状态和控件状态)机制及其使用场景](示例代码下载)
- ASP.NET3.5控件和组件开发:高效事件的处理
- ASP.NET用户控件操作ASPX页面(在ASPX页面捕捉用户控件的事件)
- ASP.NET小技巧——回传后保持页面的滚动位置[转]
- ASP.NET 入手页面控件及事件触发
- Asp.net 2.0 自定义控件开发专题[详细探讨页面状态(视图状态和控件状态)机制及其使用场景](示例代码下载)
- asp.net为服务器控件客户端事件
- ASP.Net刷新页面后自动滚动到原来位置方法汇总
- asp.net页面中弹出对话框 选择“确定”后实现事件回调
- ASP.Net页面刷新后自动滚动到原来位置
- ASP.NET自定义控件组件开发 第三章 为控件添加事件 前篇