Custom Control does not allow access to Attributes

Discussion in 'ASP .Net Web Controls' started by Sanjay Pais, Aug 11, 2005.

  1. Sanjay Pais

    Sanjay Pais Guest

    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(intLastPeriodIndexPosition,
    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.TextBox")]

    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, Aug 11, 2005
    #1
    1. Advertising

  2. Sanjay Pais

    Brock Allen Guest

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

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > 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(intLastPeriodIndexPosition,
    > 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.TextBox")]
    >
    > 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, Aug 11, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sanjay Pais
    Replies:
    1
    Views:
    444
    Brock Allen
    Aug 11, 2005
  2. Blaine
    Replies:
    7
    Views:
    422
    Bryce
    Apr 26, 2005
  3. Jeff
    Replies:
    2
    Views:
    926
    clintonG
    Sep 19, 2006
  4. Sanjay Pais

    Custom Control does not allow access to Attributes

    Sanjay Pais, Aug 11, 2005, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    114
    Brock Allen
    Aug 11, 2005
  5. Ryan Taylor
    Replies:
    1
    Views:
    669
    Ryan Taylor
    Sep 9, 2004
Loading...

Share This Page