Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Gianluca 56 posts 79 karma points
    Dec 27, 2010 @ 10:55
    Gianluca
    0

    Button in Custom Server Control not firing Click Event

    Hello everybody,

    I realized a custom server control for an Umbraco-based website. 

    This control is a form and it contains some buttons in it. For some reason, their click events are not firing. 

    I put all the initialization in OnInit. It's here that I wrote something like: btnLogin.Click += new EventHandler(btnLogin_Click);

    I use IPostBackDataHandler.LoadPostData to retrieve data from the form controls after a post. Inside OnRenderContent I do the actual control rendering (output.Writeline or control.RenderControl(output), etc.).

    Can anyone point out what I am doing wrong as I really can't see it?

    Thanks in advance,

    Gianluca

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Dec 27, 2010 @ 11:00
    Jan Skovgaard
    0

    HI Gianluca

    It will probably be a bit easier to help you out with the code if we can see a snippet of it. :-)

    If you have written the onClick event by hand it's possible that there is a typo in it for instance...but I asume that you have used the design view en visual studio and double clicked the button to get the code written for you automatically. So maybe it's the code inside of it that's the problem?

    Post a snippet then it will be more easy to help you out.

    /Jan

  • Gianluca 56 posts 79 karma points
    Dec 27, 2010 @ 11:10
    Gianluca
    0

    Hello again!

    @Jan: here is the code of my custom server control... thanks for your help!

    Cheers, gianluca.

        public class MyPurchaseForm : WebControl, IPostBackDataHandler
    {
           public override ControlCollection Controls
           {
           get
                {
                    EnsureChildControls();
                    return base.Controls;
                }
            }
            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
    
                customerCode .ID = this.UniqueID + "_customercode";             customerCode .Attributes.Add("style", "margin: 5px 0 0 18px; width: 180px;");
                btnLogin .ID = this.UniqueID + "_login";             btnLogin.Text = "Accedi";             btnLogin.Attributes.Add("style", "margin:4px 0 0 10px; display:inline-block; color:#000");             btnLogin.Click += new EventHandler(btnLogin_Click);         }
            #region IPostBackDataHandler Members         public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)         {             customerCode .Text = postCollection[this.UniqueID + "_customercode"];                        return false;         }
            public void RaisePostDataChangedEvent()         {             throw new NotImplementedException();         }
            #endregion
            protected override void OnPreRender(EventArgs e)         {             if (Page != null)                 Page.RegisterRequiresPostBack(this);
                base.OnPreRender(e);         }
            protected override void RenderContents(HtmlTextWriter output)         {                       output.WriteLine("<label style=\"margin:20px 350px 0 18px; width:auto; text-align:left;\">My message</label>");                       customerCode.RenderControl(output);                     btnLogin.RenderControl(output);                     break;             }         }         void btnLogin_Click(object sender, EventArgs e)         {             DoSomething();         }
            private void DoSomething()         {             throw new NotImplementedException();         }     }
  • Tom Fulton 2030 posts 4998 karma points c-trib
    Dec 27, 2010 @ 14:08
    Tom Fulton
    0

    Hi,

    I think the macro needs to be enclosed in a <form ruant="server"> tag for it to work.  Or did you already try that?

    -Tom

  • Gianluca 56 posts 79 karma points
    Dec 27, 2010 @ 15:29
    Gianluca
    0

    Hi Tom,

    Yes, of course. I did not mention it, but of course I have created a Macro that invokes my Custom Server Control. The macro is used inside a Master Page, which provides the <form> tag, inside which it is rendered the custom control and it is loaded the Macro by using <umbraco:Macro alias="..." runat="server" />.

    Perhaps my question is more generic, perhaps my ideas regarding custom server controls are not that clear... 

    My objective is to create a custom server control that collects some info from the user, keeps them in mem until the last step is reached and finally persists the information (writes to a db).

    I look forward to receiving your opinions, 

    Cheers,

    Gianluca.

Please Sign in or register to post replies

Write your reply to:

Draft