html5中文学习网

您的位置: 首页 > 网络编程 > ASP.NET » 正文

通过自定义类来达到向aspx页面加入脚本代码的例子_.NET教程_编程技术

[ ] 已经帮助:人解决问题
脚本|页面
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>");
            }
        }

    }
}
 
RbsHTML5中文学习网 - HTML5先行者学习网
RbsHTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助