开发者

ASP.NET Custom Web control

I'm experiencing some problems and right now I don't know how to solve it. The web control simply updates a clock represented by a label every second. My issue is that the web control exposes a property called 'Formato' where the user can select to display in format 12 or 24 hours. This is done with an enum type where in spanish Doce means 12 and Veinticuatro means 24. This is the code for the server control:

namespace Ejercicio2RelojControl
{
public enum _FormatoHora
{
    Doce,
    Veinticuatro
}

[DefaultProperty("FormatoHora")]
[ToolboxData("<{0}:Ejercicio2RelojControl runat=server></{0}:Ejercicio2RelojControl>")]
[ToolboxBitmap(typeof(Ejercicio2RelojControl), "Ejercicio2RelojControl.Ejercicio2RelojControl.ico")]
//[Designer("Ejercicio2RelojControl.Ejercicio2RelojControlDesigner, Ejercicio2RelojControl")]
public class Ejercicio2RelojControl : WebControl
{
    public Ejercicio2RelojControl()
    {                      
    }


    [
    //Bindable(true),
    Category("Appearance"),
    //DefaultValue(_FormatoHora.Doce),
    Description(""),
    ]

    public virtual _FormatoHora FormatoHora        
    {
        get
        {                
            //object t = ViewState["FormatoHora"];                
            //return (t == null) ? _FormatoHora.Doce : (_FormatoHora)t;
            object obj2 = this.ViewState["_FormatoHora"];
            if (obj2 != null)
            {
                return (_Format开发者_Python百科oHora)obj2;
            }
            return _FormatoHora.Doce;
        }
        set
        {                
            ViewState["_FormatoHora"] = value;
        }
    }

    //Create one TimerControl   
    Timer timer = new Timer();


    private Label clockLabel = new Label();        


    // Declare one Updatepanel
    UpdatePanel updatePanel = new UpdatePanel();

    // Now override the Load event of Current Web Control
    protected override void OnLoad(EventArgs e)
    {                        
        //Text = "hh:mm:ss";
        // Create Ids for Control
        timer.ID = ID + "_tiker";            
        clockLabel.ID = ID + "_l";            
        // get the contentTemplate Control Instance
        Control controlContainer = updatePanel.ContentTemplateContainer;
        // add Label and timer control in Update Panel
        controlContainer.Controls.Add(clockLabel);            
        controlContainer.Controls.Add(timer);      


        // Add control Trigger in update panel on Tick Event
        updatePanel.Triggers.Add(new AsyncPostBackTrigger() { ControlID = timer.ID, EventName = "Tick" });
        updatePanel.ChildrenAsTriggers = true;
        // Set default clock time in label
        clockLabel.Text = DateTime.Now.ToString("h:mm:ss tt");
        //clockLabel.Text = DateTime.Now.ToString("H:mm:ss");            

        // Set Interval
        timer.Interval = 1000;
        // Add handler to timer
        timer.Tick += new EventHandler<EventArgs>(timer_Tick);

        updatePanel.RenderMode = UpdatePanelRenderMode.Block;
        //Add update panel to the base control collection.
        base.Controls.Add(updatePanel);
    }

    protected override void RenderContents(HtmlTextWriter output)
    {
        output.Write(FormatoHora);
    }

    void timer_Tick(object sender, EventArgs e)
    {
        // Set current date time in label to move current at each Tick Event
        clockLabel.Text = DateTime.Now.ToString("h:mm:ss tt");
        //clockLabel.Text = DateTime.Now.ToString("H:mm:ss");                   
    }

}  

}

Now it's time to test the custom control in an asp.net web application.

    <cc1:Ejercicio2RelojControl ID="Ejercicio2RelojControl1" runat="server" />      

Works great! BUT when I add the property "Formato" fails at compile time:

    <cc1:Ejercicio2RelojControl ID="Ejercicio2RelojControl1" runat="server" Formato="Doce" />

Compiler Error Message: CS0117: 'Ejercicio2RelojControl.Ejercicio2RelojControl' does not contain a definition for 'FormatoHora'

Why is the property Formato making the web app crash at compile time?

Thanks a lot.

EDIT:

namespace Ejercicio2RelojControl { public enum FormatoHora { Doce, Veinticuatro }

[DefaultProperty("FormatoHora")]
[ToolboxData("<{0}:Ejercicio2RelojControl runat=server></{0}:Ejercicio2RelojControl>")]

public class Ejercicio2RelojControl : WebControl, INamingContainer
{

    public FormatoHora FormatoHora
    {
        get
        {                
            object obj2 = this.ViewState["FormatoHora"];
            if (obj2 != null)
            {
                return (FormatoHora)obj2;
            }
            return FormatoHora.Doce;
        }
        set
        {
            ViewState["FormatoHora"] = value;
        }            
    }

As you can see I've changed the public property. Now the error has changed. Is the following:

Compiler Error Message: CS0120: An object reference is required for the non-static field, method, or property 'Ejercicio2RelojControl.Ejercicio2RelojControl.FormatoHora.get'

Any help appreciated. Thanks

EDIT 2:

I've discovered that the problem is on the set {}. If I comment it, all is working fine but then I cannot change FormatoHora between 12 and 24 because of is read only due to only get{} is implemented. Any help with the implementation of set{} ?


I am here for giving you the solution: You are using the same name for the namespace and for the webcontrol (Ejercicio2RelojControl) . Simply change that and your code will work fine.

Hope it helps, despite the fact some years have passed :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜