Click even on custom server control

Last post 10-17-2006 9:13 AM by GoranPersson. 4 replies.

Sort Posts:

  • Click even on custom server control

    10-10-2006, 12:57 PM
    • Member
      283 point Member
    • GoranPersson
    • Member since 09-12-2003, 12:57 AM
    • Posts 115

    I want to create a server control that contains an image and a text and implement a click event.

    My first idea was to put an image and an text control in a panel and set a click event on the panel. But the panel doesn't have a click event.

     What can I do? I tried making a server side call through javascript, but I never got it right.

  • Re: Click even on custom server control

    10-10-2006, 1:58 PM
    • All-Star
      37,391 point All-Star
    • Haissam
    • Member since 10-05-2006, 6:25 AM
    • Beirut - Lebanon
    • Posts 5,632

    you can use an imagebutton server control where the onclick event exists..

    Happy coding

    Haissam Abdul Malak
    MCAD.NET
    | Blog |
  • Re: Click even on custom server control

    10-11-2006, 2:23 AM
    • Member
      283 point Member
    • GoranPersson
    • Member since 09-12-2003, 12:57 AM
    • Posts 115

    Thank you for your quick reply. 

     I thought of using an image button. But I would rather want a control wich you can give an path to the image and a text. Like:

    <myComp:MyImageButton Id="imbtn" runat="server" ImageUrl="/pict.gif" Text="This is my button!" OnClick="MyBtn_Click"></myComp:MyImageButton>

    and I want the whole component to be clickable.

    Here i try implementing a click event to a label (the class derives from compositecontrol). The page does a post back, but I never got it to reach the Click function:

            private Label label = new Label();
            private string onClickFunction;
    
            protected override void CreateChildControls()
            {
                label.ID = onClickFunction;
                label.Attributes.Add("onclick", "javascript:__doPostBack('" + onClickFunction + "','');");
                label.Text = "Click here.";
                this.Controls.Add(label);
                this.Controls.Add(new LiteralControl("&lt;input type=\"hidden\" name=\"__EVENTTARGET\" id=\"__EVENTTARGET\" value=\"\" />"));
                this.Controls.Add(new LiteralControl("&lt;input type=\"hidden\" name=\"__EVENTARGUMENT\" id=\"__EVENTARGUMENT\" value=\"\" />"));
                this.Controls.Add(new LiteralControl("&lt;script>function __doPostBack(eventTarget, eventArgument) 
                { var theForm = document.forms[0];if (!theForm.onsubmit || (theForm.onsubmit() != false)) 
                { alert(eventTarget);theForm.__EVENTTARGET.value = eventTarget;theForm.__EVENTARGUMENT.value = eventArgument;
                theForm.submit();};}</script>"));
                
            }
  • Re: Click even on custom server control

    10-11-2006, 7:06 AM
    • Member
      115 point Member
    • tkfe
    • Member since 02-07-2006, 12:25 PM
    • France
    • Posts 23
    • TrustedFriends-MVPs

    Hello Goran

    You must implement IPostBackEventHandler interface. See this example :

      

    1        public class ControlViewState : CompositeControl, IPostBackEventHandler
    2        {
    3            private Label _label;
    4            private HtmlAnchor _bouton;
    5    
    6            protected override void CreateChildControls()
    7            {
    8                base.CreateChildControls();
    9                _bouton = new HtmlAnchor();
    10               _bouton.InnerText = "Add";
    11   
    12               _bouton.HRef = "#";
    13               _bouton.ID = "Button" + this.UniqueID;
    14               _bouton.Attributes["OnClick"] =
    15               Page.ClientScript.GetPostBackEventReference(this, "Click");
    16               _label = new Label();
    17               _label.Text = _compteur.ToString();
    18           }
    19   
    20           private int _compteur
    21           {
    22               get
    23               {
    24                   return (ViewState["Compteur"] != null) ?
    25                 Int32.Parse(ViewState["Compteur"].ToString()) : 0;
    26               }
    27               set
    28               {
    29                   ViewState["Compteur"] = value;
    30               }
    31           }
    32   
    33           #region IPostBackEventHandler Members
    34           void IPostBackEventHandler.RaisePostBackEvent(string
    35           eventArgument)
    36           {
    37               if (eventArgument == "Click")
    38               {
    39                   _compteur++;
    40               }
    41           }
    42           #endregion
    43           protected override void Render(HtmlTextWriter output)
    44           {
    45               _bouton.RenderControl(output);
    46               _label.RenderControl(output);
    47           }
    48       }
    
     

    Frederic Melantois

  • Re: Click even on custom server control

    10-17-2006, 9:13 AM
    • Member
      283 point Member
    • GoranPersson
    • Member since 09-12-2003, 12:57 AM
    • Posts 115

    Thank you for your reply. Now I get how I invoke a button click.

    But I want to execute the function given in the tag (created by the person using the component): <my:control id="control" OnClick="Execute_Click" />.

    How do I do this?

    I would also like to have "OnClientClick" for javascript control over the click...?

Page 1 of 1 (5 items)