Professional Web Applications Themes

Button in custom webcontrol - event handler not working - ASP.NET Building Controls

Hello This is my 1st project where I have to create a Webcontrol. I have created a simple custom control with a button and 2 labels added to a panel. My problem is that the event handler that I have assigned to the button [private void btnSubmit_Click()], is not getting invoked. Could anyone please go thru the code and let me know what I am missing here? Thanks in advance My custom control is as follows using System using System.Web using System.Web.UI using System.Web.UI.WebControls using System.ComponentModel using System.Drawing namespace WebControlLibrary /// <summary /// This WebControl simulates the rolling of a ...

  1. #1

    Default Button in custom webcontrol - event handler not working

    Hello

    This is my 1st project where I have to create a Webcontrol. I have created a simple custom control with a button and 2 labels added to a panel. My problem is that the event handler that I have assigned to the button [private void btnSubmit_Click()], is not getting invoked. Could anyone please go thru the code and let me know what I am missing here? Thanks in advance

    My custom control is as follows

    using System
    using System.Web
    using System.Web.UI
    using System.Web.UI.WebControls
    using System.ComponentModel
    using System.Drawing

    namespace WebControlLibrary

    /// <summary
    /// This WebControl simulates the rolling of a die. It consists of a label and a submit butto
    /// encapsulated in a Panel. When the button is clicked, the event handler calls a metho
    /// of the control - RollDice() to set the label with a random number between 1 and 6
    /// When the Page is initialized, RollDice() is called in order to initialize the label wit
    /// a value
    /// </summary

    public class WebCustomControl1 : System.Web.UI.WebControls.WebContro

    int dice_num
    Label lblNum = new Label()
    Button btnSubmit = new Button()
    Label lblComments = new Label()
    Panel MainPanel = new Panel()

    [Bindable(true)
    Category("Appearance")
    DefaultValue("")
    public int DiceNu

    ge

    return dice_num

    se

    dice_num = value
    if(dice_num > 0 && dice_num < 7
    lblNum.Text = dice_num.ToString()


    private void btnSubmit_Click(object sender, CommandEventArgs e

    RollDice()
    lblComments.Text += "In btnSubmit_Click()"

    protected void RollDice(

    Random rng = new Random(System.DateTime.Now.Millisecond)
    dice_num = rng.Next(1,7)
    lblNum.Text = dice_num.ToString()


    protected override void OnInit(EventArgs e

    base.OnInit (e)
    btnSubmit.Text = "Submit"
    lblNum.BackColor = Color.LightBlue
    lblNum.Width = 30
    lblComments.Text = "In OnInit()"


    protected override void CreateChildControls(

    MainPanel.Controls.Add(lblNum)
    MainPanel.Controls.Add(btnSubmit)
    MainPanel.Controls.Add(lblComments)
    Controls.Add(MainPanel)


    protected override void RenderContents(HtmlTextWriter output

    AddAttributesToRender(output)
    MainPanel.RenderControl(output)




    The aspx page that I am using to test this control is just a simple one
    using System
    using System.Collections
    using System.ComponentModel
    using System.Data
    using System.Drawing
    using System.Web
    using System.Web.SessionState
    using System.Web.UI
    using System.Web.UI.WebControls
    using System.Web.UI.HtmlControls

    namespace WebCustomControlTes

    /// <summary
    /// Summary description for WebForm1
    /// </summary
    public class WebForm1 : System.Web.UI.Pag

    protected WebControlLibrary1.WebCustomControl1 WebCustomControl11

    private void Page_Load(object sender, System.EventArgs e

    // Put user code to initialize the page her


    #region Web Form Designer generated cod
    override protected void OnInit(EventArgs e

    /
    // CODEGEN: This call is required by the ASP.NET Web Form Designer
    /
    InitializeComponent()
    base.OnInit(e)


    /// <summary
    /// Required method for Designer support - do not modif
    /// the contents of this method with the code editor
    /// </summary
    private void InitializeComponent(
    {
    this.Load += new System.EventHandler(this.Page_Load)


    #endregio

    }
    Divya Guest

  2. #2

    Default IGNORE PREVIOUS CODE - Please look at this one

    Sorry! I pasted the wrong code in the earlier post! Please refer to this pos

    Hello

    This is my 1st project where I have to create a Webcontrol. I have created a simple custom control with a button and 2 labels added to a panel. My problem is that the event handler that I have assigned to the button [private void btnSubmit_Click()], is not getting invoked. Could anyone please go thru the code and let me know what I am missing here? Thanks in advance
    ************************************************** ***********************
    My custom control is as follows

    using System
    using System.Web
    using System.Web.UI
    using System.Web.UI.WebControls
    using System.ComponentModel
    using System.Drawing

    namespace WebControlLibrary

    /// <summary
    /// This WebControl simulates the rolling of a die. It consists of a label and a submit butto
    /// encapsulated in a Panel. When the button is clicked, the event handler calls a metho
    /// of the control - RollDice() to set the label with a random number between 1 and 6
    /// When the Page is initialized, RollDice() is called in order to initialize the label wit
    /// a value
    /// </summary

    public class WebCustomControl1 : System.Web.UI.WebControls.WebContro

    int dice_num
    Label lblNum = new Label()
    Button btnSubmit = new Button()
    Label lblComments = new Label()
    Panel MainPanel = new Panel()

    [Bindable(true)
    Category("Appearance")
    DefaultValue("")
    public int DiceNu

    ge

    return dice_num

    se

    dice_num = value
    if(dice_num > 0 && dice_num < 7
    lblNum.Text = dice_num.ToString()



    protected void RollDice(

    Random rng = new Random(System.DateTime.Now.Millisecond)
    dice_num = rng.Next(1,7)
    lblNum.Text = dice_num.ToString()


    protected override void OnInit(EventArgs e

    base.OnInit (e)
    // Subcribe to the submit button's Click even
    btnSubmit.Text = "Submit"
    lblNum.BackColor = Color.LightBlue
    lblNum.Width = 30
    lblComments.Text = "In OnInit()"
    btnSubmit.Click +=new EventHandler(btnSubmit_Click)


    protected override void CreateChildControls(

    MainPanel.Controls.Add(lblNum)
    MainPanel.Controls.Add(btnSubmit)
    MainPanel.Controls.Add(lblComments)
    Controls.Add(MainPanel)


    /// <summary
    /// Render this control to the output parameter specified
    /// </summary
    /// <param name="output"> The HTML writer to write out to </param
    protected override void RenderContents(HtmlTextWriter output

    AddAttributesToRender(output)
    MainPanel.RenderControl(output)


    private void btnSubmit_Click(object sender, EventArgs e

    RollDice()
    lblComments.Text += "In btnSubmit_Click()"



    ************************************************** ***********************
    The aspx page that I am using to test this control is just a simple one
    using System
    using System.Collections
    using System.ComponentModel
    using System.Data
    using System.Drawing
    using System.Web
    using System.Web.SessionState
    using System.Web.UI
    using System.Web.UI.WebControls
    using System.Web.UI.HtmlControls

    namespace WebCustomControlTes

    /// <summary
    /// Summary description for WebForm1
    /// </summary
    public class WebForm1 : System.Web.UI.Pag

    protected WebControlLibrary1.WebCustomControl1 WebCustomControl11

    private void Page_Load(object sender, System.EventArgs e

    // Put user code to initialize the page her


    #region Web Form Designer generated cod
    override protected void OnInit(EventArgs e

    /
    // CODEGEN: This call is required by the ASP.NET Web Form Designer
    /
    InitializeComponent()
    base.OnInit(e)


    /// <summary
    /// Required method for Designer support - do not modif
    /// the contents of this method with the code editor
    /// </summary
    private void InitializeComponent(
    {
    this.Load += new System.EventHandler(this.Page_Load)


    #endregio

    }
    Divya Guest

  3. #3

    Default Re: IGNORE PREVIOUS CODE - Please look at this one

    Hi Divya,

    By quickly browsing at your code I can see that you want to create a
    composite custom control. There are a few things that you need to do when
    coding a composite control:

    a) Make your control implement the INamingContainer marker interface. This
    is required in order for your child controls to get proper formed IDs (i.e.:
    parentid:childid). Note this is a marker interface (no methods to implement)
    so you should only modify your control declaration this way:
    [C#]
    public class WebCustomControl1 : System.Web.UI.WebControls.WebControl,
    System.Web.UI.INamingContainer

    b) Creation of the child controls should go in the CreateChildControls
    method and NOT at member definition like you're doing today. You should keep
    the member definition if you want but actual instantation should go in
    CreateChildControls. So your code should be modified this way:
    >>> Label lblNum = new Label();
    by:
    Label lblNum;
    and in CreateChildControls you then instantiate the Label.

    Make those changes and let me know the results, your events should be firing
    ok then ;-)

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    [url]http://obies.com/vga/blog.aspx[/url]
    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup

    "Divya" <divyasnewmediagateway.com> wrote in message
    news:92F53F14-8BB3-4C0B-9BFB-E9488C3411FAmicrosoft.com...
    > Sorry! I pasted the wrong code in the earlier post! Please refer to this
    post
    >
    > Hello,
    >
    > This is my 1st project where I have to create a Webcontrol. I have created
    a simple custom control with a button and 2 labels added to a panel. My
    problem is that the event handler that I have assigned to the button
    [private void btnSubmit_Click()], is not getting invoked. Could anyone
    please go thru the code and let me know what I am missing here? Thanks in
    advance!
    > ************************************************** ************************
    > My custom control is as follows -
    >
    > using System;
    > using System.Web;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.ComponentModel;
    > using System.Drawing;
    >
    > namespace WebControlLibrary1
    > {
    > /// <summary>
    > /// This WebControl simulates the rolling of a die. It consists of a label
    and a submit button
    > /// encapsulated in a Panel. When the button is clicked, the event handler
    calls a method
    > /// of the control - RollDice() to set the label with a random number
    between 1 and 6.
    > /// When the Page is initialized, RollDice() is called in order to
    initialize the label with
    > /// a value.
    > /// </summary>
    >
    > public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
    > {
    > int dice_num;
    > Label lblNum = new Label();
    > Button btnSubmit = new Button();
    > Label lblComments = new Label();
    > Panel MainPanel = new Panel();
    >
    > [Bindable(true),
    > Category("Appearance"),
    > DefaultValue("")]
    > public int DiceNum
    > {
    > get
    > {
    > return dice_num;
    > }
    > set
    > {
    > dice_num = value;
    > if(dice_num > 0 && dice_num < 7)
    > lblNum.Text = dice_num.ToString();
    > }
    > }
    >
    > protected void RollDice()
    > {
    > Random rng = new Random(System.DateTime.Now.Millisecond);
    > dice_num = rng.Next(1,7);
    > lblNum.Text = dice_num.ToString();
    > }
    >
    > protected override void OnInit(EventArgs e)
    > {
    > base.OnInit (e);
    > // Subcribe to the submit button's Click event
    > btnSubmit.Text = "Submit";
    > lblNum.BackColor = Color.LightBlue;
    > lblNum.Width = 30;
    > lblComments.Text = "In OnInit()";
    > btnSubmit.Click +=new EventHandler(btnSubmit_Click);
    > }
    >
    > protected override void CreateChildControls()
    > {
    > MainPanel.Controls.Add(lblNum);
    > MainPanel.Controls.Add(btnSubmit);
    > MainPanel.Controls.Add(lblComments);
    > Controls.Add(MainPanel);
    > }
    >
    > /// <summary>
    > /// Render this control to the output parameter specified.
    > /// </summary>
    > /// <param name="output"> The HTML writer to write out to </param>
    > protected override void RenderContents(HtmlTextWriter output)
    > {
    > AddAttributesToRender(output);
    > MainPanel.RenderControl(output);
    > }
    >
    > private void btnSubmit_Click(object sender, EventArgs e)
    > {
    > RollDice();
    > lblComments.Text += "In btnSubmit_Click()";
    > }
    > }
    > }
    > ************************************************** ************************
    > The aspx page that I am using to test this control is just a simple one -
    > using System;
    > using System.Collections;
    > using System.ComponentModel;
    > using System.Data;
    > using System.Drawing;
    > using System.Web;
    > using System.Web.SessionState;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.Web.UI.HtmlControls;
    >
    > namespace WebCustomControlTest
    > {
    > /// <summary>
    > /// Summary description for WebForm1.
    > /// </summary>
    > public class WebForm1 : System.Web.UI.Page
    > {
    > protected WebControlLibrary1.WebCustomControl1 WebCustomControl11;
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > // Put user code to initialize the page here
    > }
    >
    > #region Web Form Designer generated code
    > override protected void OnInit(EventArgs e)
    > {
    > //
    > // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    > //
    > InitializeComponent();
    > base.OnInit(e);
    > }
    >
    > /// <summary>
    > /// Required method for Designer support - do not modify
    > /// the contents of this method with the code editor.
    > /// </summary>
    > private void InitializeComponent()
    > {
    > this.Load += new System.EventHandler(this.Page_Load);
    >
    > }
    > #endregion
    > }
    > }

    Victor Garcia Aprea [MVP] Guest

Similar Threads

  1. Button Click Event Not Working
    By JeffHung in forum Macromedia Flex General Discussion
    Replies: 1
    Last Post: May 11th, 09:11 AM
  2. Custom ListControl OnFocus Event not working
    By Scott Blood in forum ASP.NET Building Controls
    Replies: 0
    Last Post: July 31st, 10:04 AM
  3. Replies: 1
    Last Post: August 26th, 12:29 PM
  4. DataGrid's UpdateCommand event handler and CancelCommand handler problem
    By David Mans in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: July 29th, 05:52 AM
  5. Event Handler not Working in Composite Control
    By João P. Bragança in forum ASP.NET Building Controls
    Replies: 2
    Last Post: July 2nd, 10:37 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139