Solved

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

Posted on 2012-04-08
3
433 Views
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);
                SaveNew.Controls.Add(btnSave);

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!
0
Comment
Question by:uconnfb13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 7

Expert Comment

by:dhawalseth
ID: 37822476
Hi,
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?
0
 
LVL 21

Expert Comment

by:tovvenki
ID: 37822535
Hi,
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,
Venki
0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 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
     <div>
        <asp:Panel runat="server" ID="panel1"></asp:Panel>
        <asp:Literal runat="server" ID="litDisplay"></asp:Literal>
    </div>

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);

        panel1.Controls.Add(btn);

    }

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

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

691 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