本文介绍了在javascript中访问自定义服务器控件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们.

我有以下要求.

抱歉,我正在开发在"DIV"标签中呈现的客户服务器控件.

我在此自定义控件类中有一个名为"AccessControlID"的属性.我在此类中也有一个Javascrit,我想在其中访问此属性.

例如.应该以这种方式检索AccessControlID.

var divObj = document.getElementById(''MyCustomControlIDHere'');
var accessControlID = divObj.AccessControlID;

有可能吗?

谢谢

Hi friends.

I have below requirement.

Supponse, I am developing custome server control which is rendering in ''DIV'' tag.

I have one property named ''AccessControlID'' in this custom control class. I have also one javascrit in this class where I want to access this proeprty.

for example. AccessControlID should be retrive this way.

var divObj = document.getElementById(''MyCustomControlIDHere'');
var accessControlID = divObj.AccessControlID;

Does it possible?

Thanks

推荐答案

<span AccessControlID="Something" id="SomeID"></span>





我在上面的行中使用了span标签只是为了解释目的.您也可以使用div标签.没问题:)

如果要这样做,则必须在自定义控件类中重写以下方法(这是我尝试过的示例代码块)







I have used span tag in above line for explaining purpose only here. You can assume your div tag also. Not a problem. :)

If that you want to do that, you have to override the following method in your custom control class( Here is sample code block that I tried )



[DefaultProperty("Text")]
    [ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
    public class WebCustomControl1 : WebControl
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            output.Write(Text);
        }
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            writer.AddAttribute("AccessControlID", "This is custom attribute value");
            base.AddAttributesToRender(writer);

        }
    }






上面的控件在aspx页面中使用时,呈现效果如下






The above control when used in aspx page and renders like this

<span AccessControlID="This is custom attribute value" id="WebCustomControl1_1"></span>



因此,您可以使用javascript访问该属性或执行以下操作



hence you can access that attribute in javascript or do like this

<script language="javascript" type="text/javascript">
    function Search()
    {
       alert(document.getElementById("WebCustomControl1_1").AccessControlID);
    }
    </script>



希望对您有帮助.请让我知道这是否是您要寻找的东西.



Hope it helps you. Please let me know whether this is what you were looking for.


[DefaultProperty("AccessControlID")]
    [ToolboxData("<{0}:RequiredControl runat=server></{0}:RequiredControl>")]
    public class RequiredControl : WebControl
    {
        public RequiredControl(): base(HtmlTextWriterTag.Div)
        {
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string AccessControlID
        {
            get
            {
                String s = (String)ViewState["AccessControlID"];
                return ((s == null) ? "[" + this.ID + "]" : s);
            }
            set
            {
                ViewState["AccessControlID"] = value;
            }
        }
        protected override void RenderContents(HtmlTextWriter output)
        {
        }
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            writer.AddAttribute("AccessControlID", this.AccessControlID);
            base.AddAttributesToRender(writer);
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (Page.ClientScript.IsClientScriptBlockRegistered("ASPCodeJSLog") == false)
                Page.ClientScript.RegisterClientScriptInclude("ASPCodeJSLog",
                             Page.ClientScript.GetWebResourceUrl(this.GetType(),
                                                         "RequireControl.RequiredControl.js"))
        }
    }





注意:AccessControlID在div元素中呈现为"accesscontrolid".

因此,我尝试了以下两种方法.

我在Page
中的功能





note : AccessControlID is rendered as ''accesscontrolid'' in div element.

So I have tried as both following ways.

My function in Page

function checkFunction()
{
    alert(document.getElementById('RequiredControl1').AccessControlID);
    alert(document.getElementById('RequiredControl1').accesscontrolid);
}



谢谢



Thanks



这篇关于在javascript中访问自定义服务器控件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 00:43