how add list of options to a property in custom server control in asp.net

416 Views Asked by At

i want to add some options to a property like this in custom server control

   [
        Category("Appearance"),
        DefaultValue(""),
        Description("The text to display on the button.")
    ]
            public string SomeProperty
            {
                get
                {
                    EnsureChildControls();
                    return somevalue;
                }
                set
                {
                    EnsureChildControls();
                    value;
                }
            }

so i can set custom control property using list of options for example

when you set a control AutoPostBack property you can find two options in dropdown list to choose "true" or "False" ..

i want to do some thing like that in my custom server control..

1

There are 1 best solutions below

1
On

Try this

This is simple Demo..If you want to Option than try to use enum type property

aspx page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Testing.WebForm2" %>

<%@ Register Src="~/test.ascx" TagName="test" TagPrefix="uc" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <uc:test ID="ucArraerEmployee" runat="server" SelectedOption="Option3" />
    </div>
    </form>
</body>
</html>

User Control

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="test.ascx.cs" Inherits="Testing.test" %>
Display: <asp:Label Text="text" runat="server" ID="lbl"/>

User Control Code behind

    public enum Options
    {
        Option1 = 1,
        Option2 = 2,
        Option3 = 3,
    }
    public virtual Options SelectedOption { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        lbl.Text = SelectedOption.ToString();
    }