1

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 (_FormatoHora)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{} ?

8
  • 1
    Something's out of sync. You say your web control exposes a property called Formato, but your property in the code sample is FormatoHora, and you are setting Formato in your custom control sample, and the error messages says FormatoHora doesn't exist. Which is it? Commented Jun 22, 2011 at 23:27
  • I've edited slightly the custom control. Commented Jun 23, 2011 at 8:38
  • 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{} ? Commented Jun 23, 2011 at 12:03
  • @user354427: I copied your code exactly into a new Visual Studio project, ran it, and after I added the required ScriptManager on the page, it ran with no errors. Can you check and make sure what you've posted above is EXACTLY what you are actually trying to execute? Commented Jun 23, 2011 at 13:21
  • The error is introduced when it's added FormatoHora inside the markup.<cc1:Ejercicio2RelojControl ID="Ejercicio2RelojControl1" runat="server" FormatoHora="Doce" /> Commented Jun 23, 2011 at 13:53

1 Answer 1

1

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 :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.