How to fire the button click event in web part?

If I created a button control programmatically in a web form, I would use  

public Button btnSearch = new Button();

protected override void CreateChildControls()


Button1.Click += new System.EventHandler(this.Button1_Click); this.Controls.Add(Button1); }

protected void Button1_Click(object sender, EventArgs e) { //code here. }

It doesn't work in a web part.  It bombed at this.Controls.Add(Button1); after I clicked the button.  How does the button click event get fire in web part?  Thanks.
4/15/2008 8:37:36 PM 3779 articles. 0 followers. Follow

7 Replies

Similar Articles

[PageSpeed] 21
Get it on Google Play
Get it on Apple App Store

Im a bit confused here.. can you elaborate more? What really is your requirements?


"Code, Beer and Music" that's my way of being a programmer!

How to get your Forum Question Answered | Blog | CodeASP.NET
4/16/2008 12:30:22 AM

I created a search textbox and a button in a Visual Studio WebPart project programmatically and added the web part to SharePoint.   The textbox and the button came up intially. When I clicked the button, the Button1_Click event supposed be fire, but it didn't. Please see the code below.  I received a unknown error from SharePoint.  Any idea?   Thanks 

public DataGrid dg = new DataGrid();
public TextBox txtSearch = new TextBox();
public Button btnSearch = new Button();

protected override void CreateChildControls()
    if (!Page.IsPostBack)
        dg = new DataGrid();
    btnSearch.Text = "Search";
    btnSearch.Click += new System.EventHandler(this.btnSearch_Click);

void btnSearch_Click(object sender, EventArgs e)
    // some code to create ds1
    dg = new DataGrid();
    dg.DataSource = ds1;

protected override void RenderContents(HtmlTextWriter output)


4/16/2008 3:25:42 PM

Try to move your codes in OnInit or Page_Init events.. see below 


"Code, Beer and Music" that's my way of being a programmer!

How to get your Forum Question Answered | Blog | CodeASP.NET
4/17/2008 12:01:33 AM

Did you manage to solve this. I'm getting the same issue. Button click event does not fire. Page is just reloaded

5/28/2008 3:09:11 PM

Hello! You are instantiating btnSearch in wrong place. Correct way to do it would be inside CreateChildControls() like this:

Button btnSearch; 

protected override void CreateChildControls() 
   btnSearch = new Button(); 

 Check this post for more details: <-- free SharePoint web parts
5/28/2008 7:36:36 PM

I figured it out and sorry I forgot to post it here.  Just remove the "protected override void RenderContents(HtmlTextWriter output)" entirely.  It does not need to be rendered when you use a button.  Hope it helps.


6/3/2008 9:10:09 PM

Thanks so much.  Ran into the same situation, and that fixed it.  To fix it and use RenderContents call base:


protected override void Render(System.Web.UI.HtmlTextWriter writer)
//do your stuff


2/14/2009 10:28:16 PM

Similar Artilces:

Web Parts & GridView button, link image click events does not fire on IE 7.0
 Hi,I have created a sample web app for testing in .NET Framework 3.5, used web parts added grid view into web part on the page to display the data. the button & link click events on these controls does not fire on IE 7.0 such as edit, select, delete,  buttons, paging & sorting in GridView and web parts restore button click events.I have executed the same application on Fire Fox browser, which is working fine.Please help me in fixing the problem how to run the same on IE 7.0 Shyam Sundar Hi shyam_eg, It should work . I strongly suspect you have disabled javascript...

button click event from another button click not firing
 Hi,  I want to trigger a button click event from another button click .It is not firing.  Please help.  protected void btnReview_Click(object sender, EventArgs e) {       if (MandatoryValidation())        {         this.Button1.Click += new System.EventHandler(this.Button1_Click);       } }protected void Button1_Click(object sender, EventArgs e) { } protected void btnReview_Click(object sender, EventArgs e){   &...

How could add a javascript function to a button click event as a part of its click event handler?
Hi, The title might be confusing. What I want is simply two pages, Page1 and Page2, when I click on a linkbutton on page1, page2 pops up. I used to use a little bit of javascript to do this, and it worked just fine. I just added the following line of code to the Page_Load event of Page1:   protected void Page_Load(object sender, EventArgs e) { LinkButton1.Attributs.Add("onclick","'Page2.aspx',null,'height=500,width=650,status=no,resizable=yes,scrollbars=yes,toolbar=no,location=no,menubar=no')"); } But now, I have...

what is an example code of how you make a button fire its event in a web part
I have my web part working, but the only thing is that when I click the button (the search button in my webpart) it wont fire the btnSearch_click function I wrote for it. I created the button and it shows up in the overridechildcontrol method, and it even shows up on our test sharepoint site. But the button wont fire. How do I declare a button properly and assign it a .click function. Thanks, and I hope that made sense, Cedric Have you added the click event in your CreateChildControls method? If not, try to do that. protected override void CreateChildControls() { ...

(web control button) click event doesn't fire
Hi, I've problem with simple case ... On my web page I have 2 user controls (one is logging control which has button and its onClick event works fine ... next control has also button and its onClick event doesn't fire) all handler are initialize correct and I've no idea why the next buttons (it happens to every next button that I try to insert on web page) bevave like don't have assemblied handlers :/ Anyone had problem like that ? Lukas         paste the code where the handler is getting hooked in the code behind file. It is in the section named Visual Studio gen...

On enter
 I'm using .net 1.1. My content page is inside a template. There is a submit button in the template and in my page. When the user presses enter key, the button click event in the template page is firing. How do I force my button click event to fire on enter? I googled it and found a fix that if I add an input control it should fix it. But it didn't. Are there any other solutions? Here is my page's code. i don't have the template code.<form runat="server" id="FormDonor" method="post">          &nb...

Button click event fired only on clicking twice
Hi, My application is developed using VS 2005 - Web Application. In one of the pages, I added a button and a click event for the same. On clicking the button I perform some operation. But only if i click the button twice, the click event is fired. I tried every thing from rebuilding the solution to adding the button controls dynamically but of no avail. Any pointers? Thanks, Sandhya Hello Sandhya, Would you please post the entire page including the .aspx and .aspx.cs/vb? It would be helpful to track down the problem. Thank you, JessicaJessica CaoSincerely,Microsoft Online Communit...

Click Event not firing when submit button is clicked
I have a form with several textboxes, one dropdown list, and a checkbox.  If I type something into a textbox and click on the submit button, the click event for the submit button does not fire, though the form does postback.  If I click on it again, it will fire the click event.  On the other hand, if I click on the checkbox, or dropdownlist, rather than typing into the textbox, then click on submit, it fires as expected.  Does anyone have an explanation and solution?  Thanks in advance for any help! Elliott post your code - sounds like postback...

Button click firing another button event
I am using 2.0 with C#.  I am trying to achieve the following: I have two user controls, on Page1.ascx i have a submit button that submit values onto Page2.ascx.  Also, on Page2.ascx i have a hidden submit button that i want to be fired whenever i click Page1.ascx submit button.  How do i access one button from one user control to another?  As far as I know, you don't. You could have button1 set a form field that the page2 page_loads looks for and then it can fire button2 click...Please Mark As Answer posts that helped you."If we learn from our mistake...

Button click event not firing on the first click
I have three buttons on my form but when I click on the button, it does not fire the button click event. I looked at the call stack. However when I click on the second time it does. I have three buttons and they all act the same way. Here is a sample of my code private void btnSubmit_Click(object sender, System.EventArgs e) { this.dgResults.CurrentPageIndex = 0; FillGrid(); } private void btnExport_Click(object sender, System.EventArgs e) { this.dgResults.CurrentPageIndex = 0; this.dgResults.AlternatingItemStyle.BackColor =System.Drawing.Color.White; this.dgResults.AllowPaging =...

Web User Control - Button Click
I have a web user control which is has one Button control on it. It has a event for handling the click... if I put this user control ( button ) at the top  of the page, the click event does not get fired just page is refreshed...but where as if I move to the bottom of the page, ButtonClick is fired ???? I am completely clueless why is this change of behavior with respect to the position of the control?? Thanks. could you show us the code that you have problem with ...   Here is the code ( i have taken out the unwanted part of the page ) Please note that the bSave does ...

TextChanged event firing instead of button Click event
Hi, I am slightly confused as to whether it is possible to 'queue' the RaisePostBack events. I have a webform that has a listbox, a textbox and a button (as well as a few other things). If the user enters data into the text box, i want the listbox to clear any active selection, so i have tied code to the TextChanged event of the textbox. There is separate code tied to the button which takes the value of the textbox/listbox and processes it. The problem that i have is that if the textbox has focus, and the user enters data, and clicks immediately on the button, the TextChanged eve...

Link Button Event not Getting Fired when the button is clicked
Hi In the Inline code , i have any tables within a form. when i try to click the link button, the button event is not getting fired. Can any on slove this for me. Thanking you Solomon Hello, we would really want to help you, but we need to know more information, can u post some code please. regards.Bilal Hadiar, MCP, MCTS, MCPD, MCTMicrosoft MVP - Telerik MVP i think the handles part is not there for the for that event. Search for handles in codebehind. if not found add it....

An OnClick event doesn't fire up when a button is clicked in a Tab Panel in a Tab Container in an AJAX Web Form
Hello Post: I would like to enclose the following code so you can analize what is going on: "> " style="vertical-align:top;"> "> "> ...

Web resources about - How to fire the button click event in web part? -

Button - Wikipedia, the free encyclopedia
In modern clothing and fashion design , a button is a small fastener , now most commonly made of plastic , but also frequently of metal, wood ...

50,000 Websites Add Facebook's Like Button And Social Plugins In First Week
50,000 Websites Add Facebook's Like Button And Social Plugins In First Week

'Buy' Buttons Ease Slowly Into the Holiday Fray
Even as batches of smaller, startup retailers are quick to embrace "buy" buttons, don't expect the technology to move the needle too far into ...

Subscribe Button Added to Live for Facebook Mentions
Facebook added a subscribe button to its Live for Facebook Mentions video-streaming feature for celebrities, journalists and public figures. ...

"Stoner Jesus Bible Study is the creation of Deb Button, a 40-something mother of two who had never considered ...
"... when Colorado became the first state to legalize recreational marijuana for adults over 21. 'I was majorly conservative,' said Button, who ...

Immigration a hot-button issue during GOP debate - Videos - CBS News
The Republican presidential candidates have contrasting views on how to handle illegal immigration, which led to clashes during Tuesday night's ...

How to master the Apple TV remote’s new buttons
Like all well-designed products from the Apple mothership, the new Apple TV's remote, also known as the Siri remote, looks simple but contains ...

Shopping with Amazon Dash buttons made me less forgetful
If you're like me, you've made a trip to the grocery store with a list full of items only to return frustrated. You forgot toilet paper. Or soap. ...

More schools roll out 'panic button' smartphone apps for emergencies
More schools across the country are rolling out smartphone applications that act as panic buttons for shootings or other emergencies.

Protesters Toting Shotguns and "Big Daddy Infidel" Buttons Gather at Texas Mosque To "Show Force"
Gun and flag-toting protesters gathered at a mosque in Irving, Texas on Saturday to protests Islam and helping Syrian refugees, reports The Dallas ...

Resources last updated: 11/23/2015 7:38:32 AM