网乐原科技

直播中

news center新闻中心
首页 > 资讯中心 > 软件技术

通过自定义类来达到向aspx页面加入脚本代码的例子

发布时间:2008-07-08     阅读数: 次       来源:网乐原科技
Set the InitialFocus for an ASP.NET WebForm
The PageUtil class has a static method SetInitialFocus(control) which can be used to generate a JavaScript for an ASP.NET page (WebForm), which sets the focus on a (given) control.

private void Page_Load(object sender, System.EventArgs e)
{
// Set the InitialFocus on TextBox1
PageUtil.SetInitialFocus(TextBox1);

using System;
using System.Web.UI;

namespace InitialFocusDemo
{
/// <summary>
/// Utility class for a ASP.NET page
/// </summary>
public class PageUtil
{

/// <summary>
/// Set the IntialFocus to the given control. Only works when JavaScript is supported.
/// </summary>
/// <param name="control">Control to set the InitialFocus on.</param>
public static void SetInitialFocus(Control control) {
if (control.Page == null) {
throw new ArgumentException("The Control must be added to a Page before you can set the IntialFocus to it.");
}
if (control.Page.Request.Browser.JavaScript == true) {
control.Page.RegisterClientScriptBlock("InitialFocus",
"<SCRIPT FOR='window' EVENT='onload' LANGUAGE='JScript'>document.all."
+ control.UniqueID + ".focus();</SCRIPT>");
}
}

}
}

网乐原科技

客服热线:0771-5761507

QQ:53290011

QQ邮箱:53290011@qq.com

工作时间:周一到周五 9:00-18:00

地址:广西南宁市江南万达写字楼C16栋1309室

物联网开发

关注我们

微信小商店 腾讯QQ客服 微信客服