获取引发回发事件的服务器控件ID

获取引发回发事件的服务器控件ID

         /// <summary>

        /// 获取回发的控件ID by wyf
        /// </summary>
        /// <param name="Page"></param>
        /// <returns></returns>
        protected string getPostBackControlName(Page Page)
        {
            Control control = null;
            string ctrlname = Page.Request.Params["__EVENTTARGET"];
            if (ctrlname != null && ctrlname != String.Empty)
            {
                control = Page.FindControl(ctrlname);
            }
            else
            {
                Control c;
                foreach (string ctl in Page.Request.Form)
                {
                    if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
                    {
                        c = Page.FindControl(ctl.Substring(0, ctl.Length - 2));
                    }
                    else
                    {
                        c = Page.FindControl(ctl);
                    }
                    if (c is System.Web.UI.WebControls.Button ||
                             c is System.Web.UI.WebControls.ImageButton || c is System.Web.UI.WebControls.LinkButton)
                    {
                        control = c;
                        break;
                    }
                }
            }
            if (control != null)
                return control.ID;
            else
                return string.Empty;
        }

posted @ 2012-10-10 09:31  西岭  阅读(60)  评论(0)    收藏  举报