在javascript中访问自定义服务器控件属性 [英] Access custom server control property in javascript

查看:90
本文介绍了在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

推荐答案

是的,这是可能的.

当您通过文档的对象(divObj.AccessControlID)访问它时,因此期望您的自定义控件呈现如下:

Yes this is possible.

when you acsess it via an document''s object (divObj.AccessControlID) therefore it is expected that your custom control is rendered like this:

<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.


嘿,阿林达姆·图瓦里
感谢您的答复.
我已经尝试过了,但是不确定.

请看下面的代码.

Hey Arindam Tewary
Thanks for your reply.
I have tried it and I am getting undefine.

Please take a look at following code.

[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


很高兴知道您的想法了. :)并且我能够为您提供帮助.

目前,我没有选择在FIreFox中对此进行测试,因为我的系统中未安装该选项.但是,如果我对其进行了测试,那么我肯定会在该线程中发布我的输入. :)

祝你有美好的一天!!!
Glad to know that you got that going. :) and I was able to help you.

Currently I dont have an option to test this in FIreFox as I dont have that installed in my system. But if I get it tested I would surely post my input in this thread. :)

Have a nice day !!!


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

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