在母版页中查找控件 [英] Finding a control inside a masterpage

查看:86
本文介绍了在母版页中查找控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

我需要这一方面的帮助:

我正在创建一个具有触发器的服务器控件.
(这是一个上下文菜单,每个触发器都会使上下文菜单弹出)

触发器由用户作为字符串传递(类似于Extender的属性-"TargetControlID"或updatepanel的触发器")
如:

Hello!

I need some help on this one:

I''m creating a server control which has triggers.
(it''s a context menu, and each trigger makes the context menu pop)

the triggers are passed by the user as a string (similar to the Extender''s property - ''TargetControlID'', or the updatepanel''s "triggers")
such as:

<Triggers>
   <atm:Trigger ControlID ="Button1" />
</Triggers>


然后传递给javascript类(这是ajax控件)

但是当使用母版页时,我实际上需要传递控件的ClientID. "FindControl(trigger.ID).ClientID"不起作用,因为我需要知道该控件位于哪个contentPlaceHolder中...

我不知道extenderControls是如何解决这个问题的...

有什么建议?


and then passed to the javascript class (this is an ajax control)

But when a masterpage is used I actually need to pass the ClientID of the control. "FindControl(trigger.ID).ClientID" doesn''t work because I need to know which contentPlaceHolder the control is in...

I couldn''t find out how the extenderControls got pass that problem...

Any suggestions?

thanks in advanced!

推荐答案

在C#ASP.NET中有效-不确定您使用的是什么.

因为母版页项目(如按钮等)都以母版页引用为前缀,所以ID"butGo"不可见,即使使用document.getElementbyId("butGo")也无法在Javascript中访问.
要解决此问题,请将以下内容添加到您的.CS文件中:

This works in C# ASP.NET - not sure what you are using.

Because master page items (like buttons, etc) are prefixed with master page references, the ID "butGo" is not visible, and cannot be accessed in Javascript, even with document.getElementbyId("butGo").
To cure this, add the following to your .CS file:

/// <summary>
/// Create a javascript array of client IDs for access via master pages.
/// In addition, creates and appends the code required to access the array and return the fully qualified name.
/// </summary>
/// <param name="wc">Controls to access via Javascript</param>
public void CreateJSArrayWithClientIds(params Control[] ca)
    {
    if (ca.Length > 0)
        {
        StringBuilder arrClientIDValue = new StringBuilder();
        StringBuilder arrServerIDValue = new StringBuilder();

        // Get a ClientScriptManager reference from the Page class.
        ClientScriptManager cs = Page.ClientScript;

        // Now loop through the controls and build the client and server id''s
        foreach (Control c in ca)
            {
            arrClientIDValue.Append("\"" +
                             c.ClientID + "\",");
            arrServerIDValue.Append("\"" +
                             c.ID + "\",");
            }

        // Register the array of client and server id to the client
        cs.RegisterArrayDeclaration("MyClientID",
           arrClientIDValue.ToString().Remove(arrClientIDValue.ToString().Length - 1, 1));
        cs.RegisterArrayDeclaration("MyServerID",
           arrServerIDValue.ToString().Remove(arrServerIDValue.ToString().Length - 1, 1));

        // Now register the method GetClientId, used to get the client id of tthe control
        cs.RegisterStartupScript(this.Page.GetType(), "key",
           "\n" +
           "function GetClientId(serverId)\n" +
           "   {\n" +
           "   for(i=0; i<MyServerID.length; i++)\n" +
           "      {\n" +
           "      if ( MyServerID[i] == serverId )\n" +
           "         {\n" +
           "         return MyClientID[i];\n" +
           "         break;\n" +
           "         }\n" +
           "      }\n" +
           "   }\n", true);
        }
    }

将以下内容添加到您的Page_Load事件:

Add the following to your Page_Load event:

CreateJSArrayWithClientIds(...);


其中...是您希望在javascript中访问的控件的列表.
例如CreateJSArrayWithClientIds(butGo);

然后在您的JavaScript中,使用:


where ... is the list of controls you wish to access in javascript.
e.g. CreateJSArrayWithClientIds(butGo);

Then in your javascript, use:

document.getElementById(GetClientId("id of control"));


例如document.getElementById(GetClientId("butGo"));


e.g. document.getElementById(GetClientId("butGo"));


FindControl仅查找控件的直接子级.如果控件在控件内部,则需要遍历每个控件的子级.
FindControl only looks for immediate children of a control. If the control is inside a control you will need to recurse through each control''s children.


在您的项目中使用该类.它包含一个扩展方法,该方法将迭代并递归地找到控件.

Use the class in your project. It contains an extension method which will iterate and find the control recursively.

public static class Extensions
    {
        public static Control FindControlR(this Control root, string id)
        {
            Control controlFound;
            if (root != null)
            {
                controlFound = root.FindControl(id);
                if (controlFound != null)
                {
                    return controlFound;
                }

                foreach (Control c in root.Controls)
                {
                    controlFound = c.FindControlR(id);
                    if (controlFound != null)
                    {
                        return controlFound;
                    }
                }
            }
            return null;
        }
    }


您可以在母版页上找到并使用该控件.
例如


You can find and use the control from masterpage.
e.g.

this.FindControlR("Button1") 


这篇关于在母版页中查找控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆