PC Review


Reply
Thread Tools Rate Thread

button events vb vs c#

 
 
jhcorey@yahoo.com
Guest
Posts: n/a
 
      9th Aug 2005
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

 
Reply With Quote
 
 
 
 
Karl Seguin
Guest
Posts: n/a
 
      9th Aug 2005
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!)
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
jhcorey@yahoo.com
Guest
Posts: n/a
 
      9th Aug 2005
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

 
Reply With Quote
 
Karl Seguin
Guest
Posts: n/a
 
      9th Aug 2005
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!)
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Marina
Guest
Posts: n/a
 
      9th Aug 2005
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.

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      9th Aug 2005
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.

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
jhcorey@yahoo.com
Guest
Posts: n/a
 
      9th Aug 2005
Thanks,

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

Jim

 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      10th Aug 2005
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.

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Thanks,
>
> I got there thanks to your suggestions.
> This is what worked:
> PlaceHolder1.Controls.Add(LoadControl("VendorsCtl.ascx"));
>
> Jim
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Button with two events Dreamer Microsoft Access 6 15th Aug 2006 08:48 AM
Button events =?Utf-8?B?SmltbXkgSmF6eg==?= Microsoft ASP .NET 7 20th Dec 2005 07:22 PM
Two events for one button Veli Izzet Microsoft Access 7 15th Aug 2005 04:56 PM
Button events Rock Microsoft ASP .NET 2 20th Jan 2005 09:54 AM
button events =?Utf-8?B?QXZuaQ==?= Microsoft Dot NET 5 26th Jan 2004 01:59 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:34 PM.