Using C# to create a button and add an onclick event

Posted on 2012-04-08
Medium Priority
Last Modified: 2012-04-10
Hello All,

I am attempting to create a button on page load (given specific criteria is met), and add an on click server side event.  The button is added to an asp place holder "SaveNew"

My code for creating the button is as follows:

 Button btnSave = new Button();
                btnSave.ID = "btnSave";
                btnSave.Text = "Save and New";
                btnSave.BorderColor = System.Drawing.Color.Navy;
                btnSave.Height = new Unit("21px");
                btnSave.ForeColor = System.Drawing.Color.Navy;
                btnSave.Font.Bold = true;
                btnSave.Font.Name = "Verdana";
                btnSave.Click += new System.EventHandler(save_new);

Open in new window

The button creates the way I would expect, but I do not believe that the event aspect of things is working correctly.  When I click on it, the function "save_new" does not fire.  It goes through the Page Loading Event but never reaches the save_new event.  Does anybody know why this might be or a better way to add a server side onclick event?

Thanks in advance!
Question by:uconnfb13

Expert Comment

ID: 37822476
You need to provide some background.
Where are you using this code, is it for a gridview or repeater? On which event of page life cycle or control you have written the above code?
LVL 21

Expert Comment

ID: 37822535
Have you created a event handler like this
protected void save_new(object sender, EventArgs e)

in which event handler are you creating the button??

Thanks and regards,
LVL 20

Accepted Solution

BuggyCoder earned 2000 total points
ID: 37822553
Given below is the code that i have used and tested and it worked fine for me:-

Here is the markup:-
Add Div in the form tag with panel in it, we will add control to this panel, this will make the control part of form tag and has its runat=server
        <asp:Panel runat="server" ID="panel1"></asp:Panel>
        <asp:Literal runat="server" ID="litDisplay"></asp:Literal>

Open in new window

Here is the code behind:-
using System;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        var btn = new Button();
        btn.ID = "btnTest";

        btn.Click += new EventHandler(btn_Click);



    void btn_Click(object sender, EventArgs e)
        litDisplay.Text = "Clicked!!!";

Open in new window


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question