Home All Groups Group Topic Archive Search About

how to override DropDownList.SelectedValue property

Author
10 Nov 2005 5:52 PM
Abraham Andres Luna
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

Author
10 Nov 2005 6:46 PM
Phillip Williams
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)

Show quoteHide quote
"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
>
>
>
Author
10 Nov 2005 6:57 PM
Abraham Andres Luna
yeah i was setting it to a non-existent value to make sure the trycatch
works
thank you for your answer


Show quoteHide quote
"Phillip Williams" <Phillip.Willi***@webswapp.com> wrote in message
news:0C1896C5-9F3F-4313-B71C-EFDE33635FC5@microsoft.com...
> 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
>>
>>
>>