• 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;
    }

  • 相关阅读:
    BZOJ 2599: [IOI2011]Race [点分治]
    BZOJ 2152: 聪聪可可 [点分治]
    POJ1741Tree [点分治]【学习笔记】
    论避免手写堆的各种姿势(1)
    BZOJ 1835: [ZJOI2010]base 基站选址 [序列DP 线段树]
    Jmeter参数化
    Manjaro Linux执行某些命令缺少libtinfo.so.5问题
    Nmon的安装及使用
    JMeter性能测试-服务器资源监控插件详解
    linux 服务器性能监控(一)
  • 原文地址:https://www.cnblogs.com/jintan/p/1273723.html
Copyright © 2020-2023  润新知