locked
Getting started with server control overriding RRS feed

  • Question

  • User1457566480 posted

     I am looking for good documentation/books about enhancing existing controls, practically a DropDownList.

    I want to add more features to meet my specific needs.

     

    Saturday, May 16, 2009 7:50 AM

Answers

  • User-364289655 posted
    http://www.eggheadcafe.com/articles/pfc/extendservercontrol.asp

    This article shows how to extend a text box - in your new class if you do inherits dropdownlist, then at that point your control (without adding any other code) will work exactly like a dropdown list. From there you can add new methods, override other ones, etc.

     The article will give you a good idea

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 16, 2009 2:33 PM
  • User-364289655 posted

    Good question, I'm not entirely sure, but you could recreate it pretty easily - you know the output of a dropdownlist (you can view the html on your page). It's just a matter of putting in the right indenting and properties and what not.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 16, 2009 5:21 PM
  • User-2106054853 posted

    Hi,

    If you want to use <optgroup> to do so please try this:

    <cc1:MyDDL ID="MyDDL1" runat="server">

    <asp:ListItem>0</asp:ListItem>

    <asp:ListItem Enabled="false">1</asp:ListItem>

    <asp:ListItem>2</asp:ListItem>

    <asp:ListItem>3</asp:ListItem>

    <asp:ListItem>4</asp:ListItem>

    </cc1:MyDDL>

     

    public class MyDDL:DropDownList

    {

    protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)

    {

    ListItemCollection items = this.Items;

    int count = items.Count;

    if (count > 0)

    {

    bool flag = false;for (int i = 0; i < count; i++)

    {

    ListItem item = items[i];

    if (item.Enabled)

    {

    writer.WriteBeginTag(
    "option");if (item.Selected)

    {

    if (flag)

    {

    this.VerifyMultiSelect();

    }

    flag = true;

    writer.WriteAttribute("selected", "selected");

    }

    writer.WriteAttribute(
    "value", item.Value, true);

    //item.HasAttributes is internal so we cannot use it. This will result in lose of attributes

    //if (item.HasAttributes)

    //{

    // item.Attributes.Render(writer);

    //}

    if (this.Page != null)

    {

    this.Page.ClientScript.RegisterForEventValidation(this.UniqueID, item.Value);

    }

    writer.Write(
    '>');

    HttpUtility.HtmlEncode(item.Text, writer);

    writer.WriteEndTag("option");

    writer.WriteLine();

    }

    else

    {

    writer.WriteBeginTag(
    "OPTGROUP");

    writer.WriteAttribute("label", item.Value, true);

    writer.Write('>');

    HttpUtility.HtmlEncode(item.Text, writer);

    writer.WriteEndTag("OPTGROUP");

    writer.WriteLine();

    }

    }

     

    }

    }

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 19, 2009 3:48 AM

All replies

  • User-364289655 posted
    http://www.eggheadcafe.com/articles/pfc/extendservercontrol.asp

    This article shows how to extend a text box - in your new class if you do inherits dropdownlist, then at that point your control (without adding any other code) will work exactly like a dropdown list. From there you can add new methods, override other ones, etc.

     The article will give you a good idea

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 16, 2009 2:33 PM
  • User1457566480 posted

     Where can I get the full source code of DropDowList Control? is it available?

     For instance "override void Render(System.Web.UI.HtmlTextWriter writer)"

    What is the original code of Render function?

     

    Saturday, May 16, 2009 4:03 PM
  • User-364289655 posted

    Good question, I'm not entirely sure, but you could recreate it pretty easily - you know the output of a dropdownlist (you can view the html on your page). It's just a matter of putting in the right indenting and properties and what not.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 16, 2009 5:21 PM
  • User1457566480 posted

     I guess. But still, it's hard to know where should I do the changing (I am not sure that the Render is the only function that I need to modify).

    As I understand there is no open sourcecode of DropDownList.

    Saturday, May 16, 2009 6:25 PM
  • User-364289655 posted

    What are you trying to modify, maybe I can help?

    ~P

    Sunday, May 17, 2009 3:59 AM
  • User1457566480 posted

     I am trying to add a feature to disable an item in the dropdownlist.

    Make it locked, without an option to select this item by clicking on it.

    Meanwhile I'am trying make a temporary workaround with HTML <select> and optgroup.

     

    P.S.

    But as I'am writing this post, I've found this  article: http://www.norimek.com/blog/post/2008/06/Implementing-OptGroup-In-A-DropDownList-Control.aspx

    I hope this is what I need...

    Sunday, May 17, 2009 4:57 AM
  • User1457566480 posted

    Using the example that i found,

    the only option to make in item "locked"  is by rendering "optgroup" tag?

    Maybe there is another method to solve this, that doesn't require modifying/overriding render methods,

    maybe there is a method that is responsible for selecting an item by mouse/keyboard clicking on it?

    (This I can't know without dropdownlist sourcecode)

     

    If <optgroup>  is my only option, then I don't actually need any sub groups of items (as described in this article).

    The one problem I think about is it's style (Bold/italic), I want it to be regular font and not sure that new style can be applied.

    Sunday, May 17, 2009 5:30 AM
  • User-2106054853 posted

    Hi,

     I am trying to add a feature to disable an item in the dropdownlist.

    Make it locked, without an option to select this item by clicking on it.

    If you want to disable all items you can try:

    <asp:DropDownList Enabled="false" ID="DropDownList1" runat="server">

    <asp:ListItem>0</asp:ListItem>

    <asp:ListItem>1</asp:ListItem>

    </asp:DropDownList>

     If you want to disable one item, try

    CustomValidator

    Code:

     

    <
    html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">

    <title></title>

    <script type="text/javascript">

    function Validate(obj,e)

    {

    if (e.Value%2==1)

    {

    e.IsValid =
    false;

    }

    }

    </script>

    </head>

    <body>

    <form id="form1" runat="server">

    <div>

    <asp:Button ID="Button1" runat="server" Text="Select" />

    <asp:DropDownList ID="DropDownList1" runat="server">

    <asp:ListItem>0</asp:ListItem>

    <asp:ListItem>1</asp:ListItem>

    <asp:ListItem>2</asp:ListItem>

    <asp:ListItem>3</asp:ListItem>

    </asp:DropDownList>

    <asp:CustomValidator ClientValidationFunction="Validate" ControlToValidate="DropDownList1" ID="CustomValidator1" runat="server" ErrorMessage="Cannot select odd integer"></asp:CustomValidator>

    </div>

    </form>

    </body>

    </html>

     

    Please note the above code only do client side validation. A more secure  option is to add server side validation as well.

    If you'd like to create a custom control you can simply combine DrowDownList and CustomValidator together. 

    Monday, May 18, 2009 1:48 AM
  • User1457566480 posted

    Allen,

     I  want to disable one or more items but not all of them!

    Validator is no good becuase the item would be still clicked with mouse/keyboard, and I need to prevent it.

    <optgroup> is some how good solution.

    I am going to add <optgroup>  by overriding RenderContents in DropDownList.

    What I need now, and hope any one can help me:

    I want to "flag" listitems that needed to be updated with <optgroup> tags, for that I would like to add a new property(IsLocked) to the nested ListItem class. The problem is that this class is SEALED!

     

     What Can I Do?

    Monday, May 18, 2009 2:29 AM
  • User-2106054853 posted

    Hi,

    If you want to use <optgroup> to do so please try this:

    <cc1:MyDDL ID="MyDDL1" runat="server">

    <asp:ListItem>0</asp:ListItem>

    <asp:ListItem Enabled="false">1</asp:ListItem>

    <asp:ListItem>2</asp:ListItem>

    <asp:ListItem>3</asp:ListItem>

    <asp:ListItem>4</asp:ListItem>

    </cc1:MyDDL>

     

    public class MyDDL:DropDownList

    {

    protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)

    {

    ListItemCollection items = this.Items;

    int count = items.Count;

    if (count > 0)

    {

    bool flag = false;for (int i = 0; i < count; i++)

    {

    ListItem item = items[i];

    if (item.Enabled)

    {

    writer.WriteBeginTag(
    "option");if (item.Selected)

    {

    if (flag)

    {

    this.VerifyMultiSelect();

    }

    flag = true;

    writer.WriteAttribute("selected", "selected");

    }

    writer.WriteAttribute(
    "value", item.Value, true);

    //item.HasAttributes is internal so we cannot use it. This will result in lose of attributes

    //if (item.HasAttributes)

    //{

    // item.Attributes.Render(writer);

    //}

    if (this.Page != null)

    {

    this.Page.ClientScript.RegisterForEventValidation(this.UniqueID, item.Value);

    }

    writer.Write(
    '>');

    HttpUtility.HtmlEncode(item.Text, writer);

    writer.WriteEndTag("option");

    writer.WriteLine();

    }

    else

    {

    writer.WriteBeginTag(
    "OPTGROUP");

    writer.WriteAttribute("label", item.Value, true);

    writer.Write('>');

    HttpUtility.HtmlEncode(item.Text, writer);

    writer.WriteEndTag("OPTGROUP");

    writer.WriteLine();

    }

    }

     

    }

    }

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 19, 2009 3:48 AM
  • User1155910849 posted

     I know you're already found your answer but I wanted to mention this. You can use Red Gate's .NET Reflector to look at the source code of .NET objects if you ever want to know what they look at. It's a great way to figure out how to set up your own stuff. I use it a lot, being a bit of a .NET noob.

     http://www.red-gate.com/products/reflector/

    Monday, June 1, 2009 10:14 AM