473,465 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

button events vb vs c#

I've typically been working with VB.Net. When I want to create code
that runs when a button is clicked, I'll go to the code-behind, find
the button in the dropdown in the top left, then select the click event
in the dropdown in the top right. The ide then creates an empty
function and I type in my code.
Now I'm working with C#. I found that I could create the skeleton of
the function by clicking on the button in the design view of the page.
But now it seems it's messing things up.

For one thing, sometimes InitializeComponent gets cleared out.

Otherwise, I have some buttons working, but right now I'm trying to put
a button on a user control and load the control into a placeholder.

Here is code from the user control:

protected System.Web.UI.WebControls.Button btnRetrieve;
.....
private void InitializeComponent()
{
this.btnRetrieve.Click += new
System.EventHandler(this.btnRetrieve_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
private void btnRetrieve_Click(object sender, System.EventArgs e)
{
}

When I try to run this, I get "Object reference not set to an instance
of an object" in the first line in InitializeComponent.

Any explanation would be appreciated.
TIA,
Jim

Nov 19 '05 #1
7 1180
do you have an <asp:button id="btnRetrieve" runat="server" /> in your aspx?

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
<jh*****@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I've typically been working with VB.Net. When I want to create code
that runs when a button is clicked, I'll go to the code-behind, find
the button in the dropdown in the top left, then select the click event
in the dropdown in the top right. The ide then creates an empty
function and I type in my code.
Now I'm working with C#. I found that I could create the skeleton of
the function by clicking on the button in the design view of the page.
But now it seems it's messing things up.

For one thing, sometimes InitializeComponent gets cleared out.

Otherwise, I have some buttons working, but right now I'm trying to put
a button on a user control and load the control into a placeholder.

Here is code from the user control:

protected System.Web.UI.WebControls.Button btnRetrieve;
....
private void InitializeComponent()
{
this.btnRetrieve.Click += new
System.EventHandler(this.btnRetrieve_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
private void btnRetrieve_Click(object sender, System.EventArgs e)
{
}

When I try to run this, I get "Object reference not set to an instance
of an object" in the first line in InitializeComponent.

Any explanation would be appreciated.
TIA,
Jim

Nov 19 '05 #2
Most certainly.
Except that it's in my ascx. All of this is in a user control
(VendorsCtl.ascx).
In my page I have:
VendorsCtl myVendors = new VendorsCtl();
PlaceHolder1.Controls.Add(myVendors);

Jim

Nov 19 '05 #3
you don't create user controls via new...you create them via
Page.LoadControl("MyControl.ascx");

I imagine that's the issue...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
<jh*****@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Most certainly.
Except that it's in my ascx. All of this is in a user control
(VendorsCtl.ascx).
In my page I have:
VendorsCtl myVendors = new VendorsCtl();
PlaceHolder1.Controls.Add(myVendors);

Jim

Nov 19 '05 #4
You should call LoadControl to load your .ascx:

VendorCtl myVendors = LoadControl("VendorsCtl.ascx")

A .ascx isn't just a class - it has a visual portion that is defined in the
..ascx. Just instantiating the class is not the same as having asp.net run
through the .ascx file itself, and create all the objects in the
corresponding class, etc.

<jh*****@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Most certainly.
Except that it's in my ascx. All of this is in a user control
(VendorsCtl.ascx).
In my page I have:
VendorsCtl myVendors = new VendorsCtl();
PlaceHolder1.Controls.Add(myVendors);

Jim

Nov 19 '05 #5
I don't see any code in your InitializeComponent() method that creates an
instance of the button and assigns it to the btnRetrieve field.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Everybody picks their nose,
But some people are better at hiding it.

<jh*****@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I've typically been working with VB.Net. When I want to create code
that runs when a button is clicked, I'll go to the code-behind, find
the button in the dropdown in the top left, then select the click event
in the dropdown in the top right. The ide then creates an empty
function and I type in my code.
Now I'm working with C#. I found that I could create the skeleton of
the function by clicking on the button in the design view of the page.
But now it seems it's messing things up.

For one thing, sometimes InitializeComponent gets cleared out.

Otherwise, I have some buttons working, but right now I'm trying to put
a button on a user control and load the control into a placeholder.

Here is code from the user control:

protected System.Web.UI.WebControls.Button btnRetrieve;
....
private void InitializeComponent()
{
this.btnRetrieve.Click += new
System.EventHandler(this.btnRetrieve_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
private void btnRetrieve_Click(object sender, System.EventArgs e)
{
}

When I try to run this, I get "Object reference not set to an instance
of an object" in the first line in InitializeComponent.

Any explanation would be appreciated.
TIA,
Jim

Nov 19 '05 #6
Thanks,

I got there thanks to your suggestions.
This is what worked:
PlaceHolder1.Controls.Add(LoadControl("VendorsCtl. ascx"));

Jim

Nov 19 '05 #7
Yeah, I caught the reference ot the User Control in the replies and related
correspondence. Good show!

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Everybody picks their nose,
But some people are better at hiding it.

<jh*****@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Thanks,

I got there thanks to your suggestions.
This is what worked:
PlaceHolder1.Controls.Add(LoadControl("VendorsCtl. ascx"));

Jim

Nov 19 '05 #8

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

Similar topics

3
by: Aymer | last post by:
i created a new button object. how do i attach an event on it? thanx for your help. Dim cc As ColorConverter = new ColorConverter() Dim b = new button() b.ID = "SelectedDate" b.BorderStyle...
14
by: tshad | last post by:
I posted this on the asp.net group, also. I wasn't sure whether this was an asp.net problem or a javascript problem. I have a page that was originally created from a program I found on the net...
4
by: joe.osowski | last post by:
I've been staring at this a while, and I haven't had much luck with it. So I figure I'll try Usenet. No matter what I try, it seems the event.button property is always "0". Here is my test...
5
by: RAJ | last post by:
hi plz tell me how to know "how window is going to close"... i have to right code for X button of forms... plz telll me thanks bye
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...
18
by: Colin McGuire | last post by:
Hi - this was posted last weekend and unfortunately not resolved. The solutions that were posted almost worked but after another 5 days of working on the code everynight, I am not further ahead....
7
by: MgGuigg | last post by:
Hello all, This is my first time posting a question to this forum, so here is hoping I am following protocol. I am scraping the rust off my old Basic programming skills, and have just recently...
14
by: Kevin | last post by:
A couple of easy questions here hopefully. I've been working on two different database projects which make use of multiple forms. 1. Where's the best/recommended placement for command buttons...
3
by: =?Utf-8?B?Rmxhc2hwcm8=?= | last post by:
i have googled this question but cannot find an answer. i'm running windows vista and i'm using Visual Basic Express 2008. i know the build event button SHOULD be in under the compile tag but i...
5
by: Tony | last post by:
I am continuing to develop an Access 2007 application which was originally converted from Access 2003. In Access 2003 I was able to disable the Access Close button in the top righthand corner of...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.