栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在回发时,如何检查Page_Init事件中哪个控件导致回发

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在回发时,如何检查Page_Init事件中哪个控件导致回发

我看到已经有一些很好的建议和方法建议如何获得回发控制。但是,我发现了另一个网页(Mahesh博客),该网页具有一种检索回发控件ID的方法。

我将在此处进行一些修改,包括使其成为扩展类。希望它以这种方式更有用。

/// <summary>/// Gets the ID of the post back control./// /// See: http://geekswithblogs.net/mahesh/archive/2006/06/27/83264.aspx/// </summary>/// <param name = "page">The page.</param>/// <returns></returns>public static string GetPostBackControlId(this Page page){    if (!page.IsPostBack)        return string.Empty;    Control control = null;    // first we will check the "__EVENTTARGET" because if post back made by the controls    // which used "_doPostBack" function also available in Request.Form collection.    string controlName = page.Request.Params["__EVENTTARGET"];    if (!String.IsNullOrEmpty(controlName))    {        control = page.FindControl(controlName);    }    else    {        // if __EVENTTARGET is null, the control is a button type and we need to        // iterate over the form collection to find it        // ReSharper disable TooWideLocalVariableScope        string controlId;        Control foundControl;        // ReSharper restore TooWideLocalVariableScope        foreach (string ctl in page.Request.Form)        { // handle ImageButton they having an additional "quasi-property"  // in their Id which identifies mouse x and y coordinates if (ctl.EndsWith(".x") || ctl.EndsWith(".y")) {     controlId = ctl.Substring(0, ctl.Length - 2);     foundControl = page.FindControl(controlId); } else {     foundControl = page.FindControl(ctl); } if (!(foundControl is IButtonControl)) continue; control = foundControl; break;        }    }    return control == null ? String.Empty : control.ID;}

更新(2016-07-22): 键入check for

Button
ImageButton
更改为寻找
IButtonControl
以识别来自第三方控件的回发。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/411902.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号