473,382 Members | 1,386 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,382 software developers and data experts.

Add an eventhandler on a button_click

Hi,
I am having this problem:
I have developed a composite component which has 2 components: a textbox and a button.
I need to add an eventhandler to a button click. I have added the eventhandler, in the CreatechildControls override, but it doesn't work.
this is the code:

namespace Space1
{
public class LookUp: System.Web.UI.WebControls.WebControl , INamingContainer
{
protected System.Web.UI.WebControls.TextBox txtRisultato = new TextBox();
protected System.Web.UI.WebControls.Button BtnLookUp = new Button();

public LookUp()
{
EnsureChildControls() ;
}
private void BtnLookUp_Click(Object sender, EventArgs e)
{
txtRisultato.Text = "Hi!!!";
}
protected override void CreateChildControls ()
{
Controls.Add(BtnLookUp);
Controls.Add(txtRisultato);
BtnLookUp.Text = "...";
Controls.Add (new LiteralControl ("<TABLE>"));
Controls.Add(new LiteralControl("<TR>"));
Controls.Add(new LiteralControl("<TD>"));
Controls.Add(txtRisultato);
Controls.Add(new LiteralControl("</TD>"));
Controls.Add(new LiteralControl("<TD>"));
Controls.Add(BtnLookUp);
Controls.Add(new LiteralControl("</TD>"));
Controls.Add(new LiteralControl("</TR>"));
Controls.Add (new LiteralControl("</TABLE>"));
BtnLookUp.Click+=new EventHandler(BtnLookUp_Click);
}
}
}

How can I fire the button click event?

Thank you
Alessandro Rossi
Nov 18 '05 #1
1 1556
The problem is this: you are instantiating this control when you create an
instance of the class, which is every time that you respond to a request.
After you instantiate this control, you then add an event handler to it. Of
course, there is no event to respond to on this newly instantiated control -
the event happened on the control that it instantiated on the first page
request.

What you need to do, in the OnInit method, is make a call to
Page.RegisterRequiresPostback(this). To ensure that you actually have a
hosting page, you should check for Page != null first. Then, you can
implement IPostBackDataHandler to handle the post back data. You have two
methods to implement: LoadPostData and RaisePostDataChangedEvent.

--
Chris Jackson
Software Engineer
Microsoft MVP - Windows Client
Windows XP Associate Expert
--
More people read the newsgroups than read my email.
Reply to the newsgroup for a faster response.
(Control-G using Outlook Express)
--

"Alessandro Rossi" <al**************@areaitsrl.com> wrote in message
news:10**********************************@microsof t.com...
Hi,
I am having this problem:
I have developed a composite component which has 2 components: a textbox
and a button.
I need to add an eventhandler to a button click. I have added the
eventhandler, in the CreatechildControls override, but it doesn't work.
this is the code:

namespace Space1
{
public class LookUp: System.Web.UI.WebControls.WebControl ,
INamingContainer
{
protected System.Web.UI.WebControls.TextBox txtRisultato = new TextBox();
protected System.Web.UI.WebControls.Button BtnLookUp = new Button();

public LookUp()
{
EnsureChildControls() ;
}
private void BtnLookUp_Click(Object sender, EventArgs e)
{
txtRisultato.Text = "Hi!!!";
}
protected override void CreateChildControls ()
{
Controls.Add(BtnLookUp);
Controls.Add(txtRisultato);
BtnLookUp.Text = "...";
Controls.Add (new LiteralControl ("<TABLE>"));
Controls.Add(new LiteralControl("<TR>"));
Controls.Add(new LiteralControl("<TD>"));
Controls.Add(txtRisultato);
Controls.Add(new LiteralControl("</TD>"));
Controls.Add(new LiteralControl("<TD>"));
Controls.Add(BtnLookUp);
Controls.Add(new LiteralControl("</TD>"));
Controls.Add(new LiteralControl("</TR>"));
Controls.Add (new LiteralControl("</TABLE>"));
BtnLookUp.Click+=new EventHandler(BtnLookUp_Click);
}
}
}

How can I fire the button click event?

Thank you
Alessandro Rossi

Nov 18 '05 #2

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

Similar topics

3
by: Remco | last post by:
Hello, Serverside I'm generating a html page. There are different controls for which I want to create an eventhandler manually. Like: document.forms.TextBox1.onchange = EventHandler;...
12
by: aplaxas | last post by:
Hi! "CDemo.Call" eventHandler is added to "button3.click" Event when both button1 and button2 are clicked. However, I want to add "cd.Call" only one time even though I clicked both button1 and...
1
by: Paul Aspinall | last post by:
Hi I have a form with a button on it. It seems that when I click the button, it causes Form_load to fire, before the Button_Click procedure Does anyone know why?? Thanks
3
by: Tim Thomas | last post by:
Hi, I am very new to .NET and am in the process of building my first web application. I will briefly describe what i am trying to achieve: I have a system where suppliers register their...
2
by: Frank Schumacher | last post by:
Hi Folks, I have a problem with the order of events fired by ASP.NET. I found many articles which explaining the lifecycle of a site, but I found none which took the event from a Control on the...
9
by: gaddoznntp | last post by:
Hi all, I've a server button handling a button_click procedure. If the client user clicks the button, the page causes the postback and the button_click procedure is fired. Then, if the user...
13
by: Matt | last post by:
I am trying to call a recursive subroutine within a Button_Click event. How can I do this? I tried to create a seperate Function() but it will not allow me to use any variables that I created in...
3
by: charles | last post by:
Hi: In my page I need to re-start a thread. I put the code into a Button_Click(sender, e) handler. When I click the button, it works. Then I call this handler inside a timer's handler...
0
by: John | last post by:
I am loading my controls dynamically into my asp.net page. When I click on a button on one of those user controls then page reloads and I need to reload the page based on what has happened in the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.