Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VS 2005 C#, OnClick issue with copied Button.

Status
Not open for further replies.

DavidKnight

Programmer
Mar 31, 2004
27
CA
I'm a VB.NET developer trying to wotk in the C# environment. I ran into a funny situation that I have not run into while working in a VB webform.

I copy and paste an existing Standard.Button "Button1" in Design view and endup with the follow HTML Source code:

<form id="form1" runat="server">
<div>

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Style="z-index: 100; left: 276px; position: absolute; top: 15px" Text="Button" />

<asp:Button ID="Button2" runat="server" OnClick="Button1_Click" Style="z-index: 100; left: 278px; position: absolute; top: 55px" Text="Button" />

</div>
</form>

Why does the Copied Button "Button2" have its OnClick event set to "Button1_Click"? What am I missing? Do I have to check each time I copy an object in Design View?

This seems to be a major difference from VB to C# in the handling of Button objects. The VB environment handles the OnClick in the code behind. (OnClick in VB isn't even available on the HTML side.) Where in C# it does the OnClick in the HTML and not in the code behind.

Thanks in advance for the help;
David
 
this is by design the only thing that much change is the id of the control so VS does this for you.

if you don't want to wire the events to the control in markup (personally i can't stand it) then you can wire them up in the Init event
Code:
Button1.Click += DoSomething;
Button2.Click += DoSomethingelse;
where you define the event handlers DoSomething and DoSomethingelse

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top