473,386 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Button in custom webcontrol - event handler not called

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

}
Nov 18 '05 #1
1 2708
Hi Divya,

Please see my reply to your post @ the buildingcontrols ng

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

"Divya" <di****@newmediagateway.com> wrote in message
news:1A**********************************@microsof t.com...
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
}
}

Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Novice | last post by:
I can't seem to get my Custom WebControl to output a button whose click event is associated with a particular method. Here is the code I have right now that contains a panel and there is a...
0
by: Novice | last post by:
Hey all, I've already posted this question and two posters offered suggestions and they worked - but now I would like to know why - and if possible the answer to a second question. Here is my...
3
by: Chris Newby | last post by:
I have a very simple custom control that derives from WebControls.Panel and implements INamingContainer. It appear that controls created as children of my custom control are having ViewState...
0
by: Chris Newby | last post by:
I have a custom control that is essentially implemented as follows: ================================================ public class TestPanel : WebControl, INamingContainer { protected override...
1
by: Martin | last post by:
Hi, I have produced a custom server control that simple outputs a row of 26 buttons, one button for each letter of the english alphabet. now what I would like to do is catch the button click...
6
by: murl | last post by:
I have built a Web Custom Control using c#, and it's pretty simple. A dropdownlist, and a button that once clicked will render different html. I tried attaching an event handler to the Click event...
11
by: bill | last post by:
I dynamically create buttons and associate them with an event using AddHandler. I want all the button events to fire at one time, when the page is posted, instead of when each button is clicked....
1
by: tranky | last post by:
Hi boys.. ....i've a question for you! I've a custom control with a button. Well... I need to create that: <mytagprefix:mytag runat="server" onButtonClick="Button_Click"> And in the code...
1
by: shapper | last post by:
Hello, I am creating a custom control where I have a button. Somehow my button click event is not being raised. I tried to place different codes inside MyButton_Click and nothing runs. Then I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.