how to override DropDownList.SelectedValue property

Discussion in 'ASP .Net Web Controls' started by Abraham Andres Luna, Nov 10, 2005.

  1. hey everyone,

    i have a control that inherits from dropdownlist:

    using System;
    using System.Web.UI.WebControls;

    namespace RDK.WebControls
    {
    public class SelectedValueDropDown : DropDownList
    {
    protected override void OnInit(EventArgs E)
    {
    this.Items.Add(new ListItem("NONE", "NONE"));
    }

    public override string SelectedValue
    {
    get
    {
    return this.SelectedValue;
    }
    set
    {
    try
    {
    this.SelectedValue = value;
    }
    catch
    {
    this.SelectedValue = "Error";
    }
    }
    }
    }
    }


    when i try to set the selectedvalue i get a server application unavailable
    error page

    <%@ Page %>
    <script runat="server">
    void Page_Load(Object Sender, EventArgs E)
    {
    ddlWhich.SelectedValue = "ME";
    }
    </script>
    <html>
    <head>
    <title>Drop Down</title>
    </head>
    <body>
    <form id="frm" runat="server">
    <RDK:SelectedValueDropDown id="ddlWhich" runat="server" />
    </form>
    </body>
    </html>


    how am i supposed to override/use the selectedvalue property
    Abraham Andres Luna, Nov 10, 2005
    #1
    1. Advertising

  2. You should use the keyword "base" instead of "this" in a derived class method
    to access the overridden method in the base class, e.g. base.SelectedValue =
    value; otherwise you caused an infinite loop.

    BTW, the logic you have in the SelectedValue does not make sense (the list
    does not have a value named "Error" in it yet you are trying to set it when
    trapping an error)

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "Abraham Andres Luna" wrote:

    > hey everyone,
    >
    > i have a control that inherits from dropdownlist:
    >
    > using System;
    > using System.Web.UI.WebControls;
    >
    > namespace RDK.WebControls
    > {
    > public class SelectedValueDropDown : DropDownList
    > {
    > protected override void OnInit(EventArgs E)
    > {
    > this.Items.Add(new ListItem("NONE", "NONE"));
    > }
    >
    > public override string SelectedValue
    > {
    > get
    > {
    > return this.SelectedValue;
    > }
    > set
    > {
    > try
    > {
    > this.SelectedValue = value;
    > }
    > catch
    > {
    > this.SelectedValue = "Error";
    > }
    > }
    > }
    > }
    > }
    >
    >
    > when i try to set the selectedvalue i get a server application unavailable
    > error page
    >
    > <%@ Page %>
    > <script runat="server">
    > void Page_Load(Object Sender, EventArgs E)
    > {
    > ddlWhich.SelectedValue = "ME";
    > }
    > </script>
    > <html>
    > <head>
    > <title>Drop Down</title>
    > </head>
    > <body>
    > <form id="frm" runat="server">
    > <RDK:SelectedValueDropDown id="ddlWhich" runat="server" />
    > </form>
    > </body>
    > </html>
    >
    >
    > how am i supposed to override/use the selectedvalue property
    >
    >
    >
    Phillip Williams, Nov 10, 2005
    #2
    1. Advertising

  3. yeah i was setting it to a non-existent value to make sure the trycatch
    works
    thank you for your answer


    "Phillip Williams" <> wrote in message
    news:...
    > You should use the keyword "base" instead of "this" in a derived class
    > method
    > to access the overridden method in the base class, e.g. base.SelectedValue
    > =
    > value; otherwise you caused an infinite loop.
    >
    > BTW, the logic you have in the SelectedValue does not make sense (the list
    > does not have a value named "Error" in it yet you are trying to set it
    > when
    > trapping an error)
    >
    > --
    > HTH,
    > Phillip Williams
    > http://www.societopia.net
    > http://www.webswapp.com
    >
    >
    > "Abraham Andres Luna" wrote:
    >
    >> hey everyone,
    >>
    >> i have a control that inherits from dropdownlist:
    >>
    >> using System;
    >> using System.Web.UI.WebControls;
    >>
    >> namespace RDK.WebControls
    >> {
    >> public class SelectedValueDropDown : DropDownList
    >> {
    >> protected override void OnInit(EventArgs E)
    >> {
    >> this.Items.Add(new ListItem("NONE", "NONE"));
    >> }
    >>
    >> public override string SelectedValue
    >> {
    >> get
    >> {
    >> return this.SelectedValue;
    >> }
    >> set
    >> {
    >> try
    >> {
    >> this.SelectedValue = value;
    >> }
    >> catch
    >> {
    >> this.SelectedValue = "Error";
    >> }
    >> }
    >> }
    >> }
    >> }
    >>
    >>
    >> when i try to set the selectedvalue i get a server application
    >> unavailable
    >> error page
    >>
    >> <%@ Page %>
    >> <script runat="server">
    >> void Page_Load(Object Sender, EventArgs E)
    >> {
    >> ddlWhich.SelectedValue = "ME";
    >> }
    >> </script>
    >> <html>
    >> <head>
    >> <title>Drop Down</title>
    >> </head>
    >> <body>
    >> <form id="frm" runat="server">
    >> <RDK:SelectedValueDropDown id="ddlWhich" runat="server" />
    >> </form>
    >> </body>
    >> </html>
    >>
    >>
    >> how am i supposed to override/use the selectedvalue property
    >>
    >>
    >>
    Abraham Andres Luna, Nov 10, 2005
    #3
    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. Jesse
    Replies:
    3
    Views:
    1,225
    DudeGuy
    Jul 24, 2003
  2. Tom Jorgenson
    Replies:
    3
    Views:
    7,015
  3. gu4y
    Replies:
    4
    Views:
    6,743
    mngates
    Jan 13, 2010
  4. Sudhir

    Dropdownlist.SelectedValue ISSUE

    Sudhir, Feb 26, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    455
    Scott Mitchell [MVP]
    Feb 26, 2004
  5. Slavan
    Replies:
    0
    Views:
    520
    Slavan
    Feb 5, 2009
Loading...

Share This Page