Custom Control does not allow access to Attributes

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default Custom Control does not allow access to Attributes

    I built a custom control for all the basic web.ui.controls like textbox,
    label, checkbox etc etc. I added my custom attribute called ApplySecurity to
    the html in the page.

    However, when I cycle through the controls on the page using this code, I
    cant seem to be able to access the Attribute collection. However, if I were
    to add the tag to a regular TextBox, the Attribute is available.

    My recursive function looks like this:
    ************************************************** *****
    private SortedList PageFieldDetector(Control ctlPage, ref SortedList
    r_slPageControlList)

    {

    string strPageID = ctlPage.ClientID;

    string strObjectID = "";

    string strObjectType = "";

    string strTempObjectType = "";

    string strApplySecurity = "";

    int intLastPeriodIndexPosition = 0;

    foreach (Control ctrl in ctlPage.Controls)

    {

    strObjectID = ctrl.ClientID.ToString();

    //check if securable

    try

    {

    strApplySecurity =
    ((WebControl)(ctrl)).Attributes["applysecurity"].ToString();

    }

    catch

    {

    strApplySecurity = "False";

    }

    if (strApplySecurity == "True")

    {

    strTempObjectType = ctrl.GetType().ToString();

    intLastPeriodIndexPosition = strTempObjectType.LastIndexOf(".") + 1;

    strObjectType = strTempObjectType.Substring(intLastPeriodIndexPosi tion,
    strTempObjectType.Length - intLastPeriodIndexPosition);

    r_slPageControlList.Add(strObjectID, strObjectType);

    }

    else

    {

    if (ctrl.Controls.Count > 0)

    {

    PageFieldDetector(ctrl, ref r_slPageControlList);

    }

    }

    }

    return r_slPageControlList;

    }

    ************************************************** *****
    A regular html control like this has the attribute collection:
    <asp:Label ID="lblHeader" applysecurity="False" runat="server" Text="Page
    Header No security is to be applied to this object"></asp:Label>



    However, My custom textbox does not diaplay any attributes at all

    <aepc:aeptextbox id="AEPTextBox1" runat="server"
    applysecurity="True"></aepc:aeptextbox>

    This is my code for the custom textbox:

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.ComponentModel;

    using System.Drawing;

    using System.Diagnostics;

    using System.Design;

    [assembly: TagPrefix("AEPortal", "AEPC")]

    namespace AEPortal

    {

    [ToolboxData("<{0}:AEPTextBox runat=server
    applysecurity=False></{0}:AEPTextBox>")]

    [ToolboxBitmap(typeof(TextBox))]

    [DesignerAttribute("System.Web.UI.WebControls.TextB ox")]

    public class AEPTextBox : System.Web.UI.WebControls.TextBox

    {

    [Bindable(true),

    Description("Accruent Enterprise Portal Custom TextBox"),

    Category("Misc"),

    DefaultValue("False")]

    private bool blnApplySecurity = false;

    public bool ApplySecurity

    {

    get

    {

    return blnApplySecurity;

    }

    set

    {

    blnApplySecurity = value;

    }

    }

    protected override void Render(HtmlTextWriter w)

    {

    w.AddAttribute("applysecurity", blnApplySecurity.ToString());

    base.Render(w);

    }

    }

    }


    Sanjay Pais Guest

  2. Similar Questions and Discussions

    1. <% =expression%> not evaluated in custom control attributes
      Let's say my ASPX page codebehind has a method S() which returns a string value (of let's say a constant "string"). If I do this (hope all these...
    2. Designer/attributes for custom DataSource Control?
      Hi, I've developed my own datasource control. When I make my datasource control be the datasource of a formview control, I would like the...
    3. How to access an event of a custom control??? Thank you!
      Could you please post your reply? Thank you I'm trying to resolve why my custom image button control with a custom eventargs class will not fire...
    4. Custom TextBox with custom attributes and properties question
      I have an webform that has a datarepeater on it. In that repeater I'm binding some data and I have put a textbox control in there. On the...
    5. Access WebControl in custom control
      I cannot access a server control that is nested in a template control...like so: <dtag:PageTemplate runat="server" id="myPageTemplate">...
  3. #2

    Default Re: Custom Control does not allow access to Attributes

    Arbitrary attributes (ones where there is no property) are implemented by
    the custom control implementing the IAttributeAccessor interface.

    -Brock
    DevelopMentor
    [url]http://staff.develop.com/ballen[/url]


    > I built a custom control for all the basic web.ui.controls like
    > textbox, label, checkbox etc etc. I added my custom attribute called
    > ApplySecurity to the html in the page.
    >
    > However, when I cycle through the controls on the page using this
    > code, I cant seem to be able to access the Attribute collection.
    > However, if I were to add the tag to a regular TextBox, the Attribute
    > is available.
    >
    > My recursive function looks like this:
    > ************************************************** *****
    > private SortedList PageFieldDetector(Control ctlPage, ref SortedList
    > r_slPageControlList)
    > {
    >
    > string strPageID = ctlPage.ClientID;
    >
    > string strObjectID = "";
    >
    > string strObjectType = "";
    >
    > string strTempObjectType = "";
    >
    > string strApplySecurity = "";
    >
    > int intLastPeriodIndexPosition = 0;
    >
    > foreach (Control ctrl in ctlPage.Controls)
    >
    > {
    >
    > strObjectID = ctrl.ClientID.ToString();
    >
    > //check if securable
    >
    > try
    >
    > {
    >
    > strApplySecurity =
    > ((WebControl)(ctrl)).Attributes["applysecurity"].ToString();
    >
    > }
    >
    > catch
    >
    > {
    >
    > strApplySecurity = "False";
    >
    > }
    >
    > if (strApplySecurity == "True")
    >
    > {
    >
    > strTempObjectType = ctrl.GetType().ToString();
    >
    > intLastPeriodIndexPosition = strTempObjectType.LastIndexOf(".") + 1;
    >
    > strObjectType =
    > strTempObjectType.Substring(intLastPeriodIndexPosi tion,
    > strTempObjectType.Length - intLastPeriodIndexPosition);
    >
    > r_slPageControlList.Add(strObjectID, strObjectType);
    >
    > }
    >
    > else
    >
    > {
    >
    > if (ctrl.Controls.Count > 0)
    >
    > {
    >
    > PageFieldDetector(ctrl, ref r_slPageControlList);
    >
    > }
    >
    > }
    >
    > }
    >
    > return r_slPageControlList;
    >
    > }
    >
    > ************************************************** *****
    > A regular html control like this has the attribute collection:
    > <asp:Label ID="lblHeader" applysecurity="False" runat="server"
    > Text="Page
    > Header No security is to be applied to this object"></asp:Label>
    > However, My custom textbox does not diaplay any attributes at all
    >
    > <aepc:aeptextbox id="AEPTextBox1" runat="server"
    > applysecurity="True"></aepc:aeptextbox>
    >
    > This is my code for the custom textbox:
    >
    > using System;
    >
    > using System.Collections.Generic;
    >
    > using System.Text;
    >
    > using System.Web.UI;
    >
    > using System.Web.UI.WebControls;
    >
    > using System.ComponentModel;
    >
    > using System.Drawing;
    >
    > using System.Diagnostics;
    >
    > using System.Design;
    >
    > [assembly: TagPrefix("AEPortal", "AEPC")]
    >
    > namespace AEPortal
    >
    > {
    >
    > [ToolboxData("<{0}:AEPTextBox runat=server
    > applysecurity=False></{0}:AEPTextBox>")]
    >
    > [ToolboxBitmap(typeof(TextBox))]
    >
    > [DesignerAttribute("System.Web.UI.WebControls.TextB ox")]
    >
    > public class AEPTextBox : System.Web.UI.WebControls.TextBox
    >
    > {
    >
    > [Bindable(true),
    >
    > Description("Accruent Enterprise Portal Custom TextBox"),
    >
    > Category("Misc"),
    >
    > DefaultValue("False")]
    >
    > private bool blnApplySecurity = false;
    >
    > public bool ApplySecurity
    >
    > {
    >
    > get
    >
    > {
    >
    > return blnApplySecurity;
    >
    > }
    >
    > set
    >
    > {
    >
    > blnApplySecurity = value;
    >
    > }
    >
    > }
    >
    > protected override void Render(HtmlTextWriter w)
    >
    > {
    >
    > w.AddAttribute("applysecurity", blnApplySecurity.ToString());
    >
    > base.Render(w);
    >
    > }
    >
    > }
    >
    > }
    >


    Brock Allen Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139