zoukankan      html  css  js  c++  java
  • Set Focus to Control

    I was trying to set focus using the code

    this.ScriptManager1.SetFocus(myControlName.ClientID);

    and I wrote this code in my SelectedIndexChanged Event Function. That time this code didn't work.

    Now I changed this code to Page Load Function i.e.
    protected void Page_Load(object sender, EventArgs e)
    {
        
    if (!IsPostBack)
        {
            
    this.ScriptManager1.SetFocus(myControlName.ClientID);
        }
        
    else
        {
            
    this.ScriptManager1.SetFocus(GetPostBackControl(this.Page).ClientID);
        }



    Here I am using a function GetPostBackControl() to identify the current postbacking control and I am setting focus to the same control. If you want to move focus to any other control, just use a Switch Statment.
    public static System.Web.UI.Control GetPostBackControl(System.Web.UI.Page page)
    {
        Control control 
    = null;
        
    string ctrlname = page.Request.Params["__EVENTTARGET"];
        
    if (ctrlname != null && ctrlname != String.Empty)
        {
            control 
    = page.FindControl(ctrlname);
        }
        
    // if __EVENTTARGET is null, the control is a button type and we need to 
        
    // iterate over the form collection to find it 
        else
        {
            
    string ctrlStr = String.Empty;
            Control c 
    = null;
            
    foreach (string ctl in page.Request.Form)
            {
                
    // handle ImageButton controls  
                if (ctl.EndsWith(".x"|| ctl.EndsWith(".y"))
                {
                    ctrlStr 
    = ctl.Substring(0, ctl.Length - 2);
                    c 
    = page.FindControl(ctrlStr);
                }
                
    else
                {
                    c 
    = page.FindControl(ctl);
                }
                
    if (c is System.Web.UI.WebControls.Button ||
                c 
    is System.Web.UI.WebControls.ImageButton)
                {
                    control 
    = c;
                    
    break;
                }
            }
        }
        
    return control;
    }

  • 相关阅读:
    通用工业协议(CIP)形式化的安全分析(前期概念的梳理)
    WEB安全工程师整理资料
    AWVS (Acunetix Web Vulnerability Scanner )
    Java Decompiler反编译Jar文件
    信息安全面试题整理
    工业网络安全 智能电网,SCADA和其他工业控制系统等关键基础设施的网络安全(总结)
    PLC编程的基础知识的总结
    协议安全分析方法的综述
    The Essential Burp Suite
    kali linux 虚拟机克隆之后版本回退问题
  • 原文地址:https://www.cnblogs.com/jintan/p/1273723.html
Copyright © 2011-2022 走看看