Why does this work in VB.NET but not C#?

I'm using the Club Starter Kit and the issue is with the PageNumberer object that implements a WebControl. I've converted it to C# and have most of it working except for the PageNumberer object.

Here is the VB.NET code:

1            Public Event SelectedPageChanged As EventHandler
3            Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
4                Dim newPage As Integer
5                If Integer.TryParse(eventArgument, newPage) Then
7                    Me.SelectedPage = newPage
8                    OnSelectedPageChanged(EventArgs.Empty)
9                End If
10           End Sub
12           Protected Overridable Sub OnSelectedPageChanged(ByVal e As EventArgs)
13               RaiseEvent SelectedPageChanged(Me, e)
14           End Sub

Here is the C# code:

1            public event EventHandler SelectedPageChanged;
3            void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
4            {
5                int newPage;
7                if (int.TryParse(eventArgument, newPage))
8                {
9                    this.SelectedPage = newPage;
10                   OnSelectedPageChanged(EventArgs.Empty);
11               }
12           }
14           protected override void OnSelectedPageChanged(EventArgs eventArgs)
15           {
16               SelectedPageChanged(this, eventArgs);
17           }

The issue is with OnSelectedPageChanged(). In VB.NET, it always works. However, in C#, the SelectedPageChanged event handler (line 16 in the C# code above) is always null. What is VB.NET doing behind the scenes to ensure that it points correctly to an event handler that exists?

I've searched all over the place and can't seem to understand how to get this to work in C#.



9/10/2008 5:51:26 PM
asp.net.custom-server-controls 3498 articles. 0 followers. Follow

4 Replies

Similar Articles

[PageSpeed] 49

VB implicitely create the handler. In C#, you need to check for null before you raise it, so make sure to use the "invoke" snippet (type invoke and hit tab) to see how to raise the event. Of course, the event will have to be wired up, too (if its a custom control you're making, remember that C# doesn't have the "handles" keyword, so you need to either wire it up manually using the += syntax, or to set AutoEventWireup (though that only works for page events, not your controls.. I'm mentionning it anyway for completeness sake)

9/10/2008 6:24:31 PM

When you force fire an event in c# you should always check for null like so.

if (SelectedPageChanged != null) SelectedPageChanged(this, eventArgs);

I would be that the reason its null is because no one has "signed up" to listen for the event.

Make sure that who ever needs this event is listening for it.

public Ctor()
     MyPageClass.SelectedPageChanged += new EventHandler<EventArgs>(handlerMethod);

Honestly, it's been a while. So, I'm not sure what this syntax is, but you get the pic. Hope it helps!


"Never be so busy making a living that you forget to make a life."
9/10/2008 6:25:20 PM

I finally figured it out (and with some of the tips everyone gave). I had this all linked up to an ObjectDataSource object, but I forgot to also link in the PageNumberer to the ObjectDataSource parameters.

1                                <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OnSelecting="ObjectDataSource1_Selecting"
2                                    SelectMethod="List" TypeName="SBAA.Web.News">
3                                    <SelectParameters>
4                                        <asp:ControlParameter DefaultValue="1" Name="pagenum" ControlID="pn1" PropertyName="SelectedPage" Type="Int32" />
5                                        <asp:ControlParameter ControlID="ddlSports" DefaultValue="0" Name="sportId" PropertyName="SelectedValue"
6                                            Type="Int32" />
7                                    </SelectParameters>
8                                </asp:ObjectDataSource>

In the example above, at line 4, I didn't have it as asp:ControlParameter, but as asp:Parameter. Every time I would attempt to change the page, the ObjectDataSource1_Selecting event handler would never get called, so the page numbers would change, but the data view would display the same first n records.

Thanks again for all the help!


9/10/2008 7:04:28 PM

 Excellant - please mark this thread as answered.

Don't forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
9/13/2008 6:39:52 PM

Similar Artilces:

Sending SMS using vb.net or C#.net using vb.net or c#.net
Hi  My requirement is I hav one csv file with these fields id,mobilenum,messgae,status.intiallu staus is 0. once i read the all fileds and take that mobile number.using tat mobile number  i need to send sms .after sending sms i shuld change status as 1 How to send sms thru coding (please dont provide any links.if it is provide also please give working links becox i checked codeproject .i didnt get any nice link. and also provide the how to update the status field im csv file   Thank ssandhya   To send SMS, you need some third party SMS providers. if you consul...

Type.GetType does not work in VB.NET but works in C#. VB.NET gurus Please help
Friends,   I am an experienced C# programmer who is working on a VB.NET project now. I am writing different methods covering the following functionalities 1) Take a datareader as input and return an arraylist of class object2) Take an xmlnode (received from a webservice) as input and return an arraylist of class object. The methods are generic methods which take datareader/xmlnode as first parameter and classname (string) as the second parameter. This way it will work trivially. The schema of class object matches with the input (datareader or xmlnode)In C# I used to do th...

use VB.NET and C#.NET code in the same C#.NET project
All-- Here is a sample that is "off the beaten path", (at least for me). Is it possible, in an ASP.NET application, using the code-behind page building technique, to have both pages written in VB.NET and pages written C#.NET?At http://www.WebLogicArts.com/DemoList.aspx there is a sample that shows that, (contrary to popular belief), it IS possible to mix ASP.NET pages built with C#.NET with ASP.NET pages built with VB.NET in the same VS.NET 2003 project. Note that this is just a "fun" sample to see if it can be done and I do not recommend this practice as a "standard" way of develo...

converting to vb.net from c#.net authorize.net
authorize.net offered me some sample code when I signed up with them the only problem is the sample code is in c#.net but my page that they type all of their credit card into is vb.net <code><%@ Import Namespace="System.Net" %> <%@ Import Namespace="System.IO" %> <script language="C#" runat="server"> void Page_Load(Object Src, EventArgs E) { myPage.Text = readHtmlPage("https://certification.authorize.net/gateway/transact.dll"); } private String readHtmlPage(string url) { ...

Creating .Net custom server controls and .Net pages programatically
Thanks a lot for reading this and greatly appreciate your inputs I am writing a webinterface that lets users configure options that'll create an interface for them and generete the webpage. So I am having to programattically create custom server controls and .Net page with code behind to support the functionality. Are there APIs in the .NET SDK that let you do this. Any pointers would be greatly helpful Thanks a lot Sathya Controls on the fly? Or Create code on the fly? I think it is the latter so here we go - look at the following namespaces: * System.Reflection.Emit - gen...

vb.net vs c#.net vs j#.net
just out of curiousity which looks, acts most like java? i'm still learning trying to used to vb.net but it seems that it was easier to do some stuff with java. C# is Java++ in my opinion. If you have done C or Jave you will prefer C#.Mike Schellenberger - MCAD If you are looking for language similar to java go for J#. However most code samples you find on the net will be in VB.NET and C#....

VB.Net To C#.Net
Hello... Since Last from 1 year i had develope web application in using vb.net now i want to transfer from vb.net to c#.net so what should i do to transform to c# language any link or book related to me through i can easily understand and coding using c# in a short time... Thanks~ Mark As Answer If UseFull ~Kaushal. Hi, you can read the specifications of the language. You're already familiar with .NET and how to program in an OO manner thanks to your experience with vb.net. The only thing you need to learn is the new syntax of C#. You can also convert parts of your v...

vb .net or c# .net
I currently develop websites using php but have been requested to develop a site using asp.net. The question I have is for someone who knows php would it be better to transition to vb.net or c#.net? Thanks, Fr. Robert C# -> There is a converter that will transition about 60% or more of your PHP code to C#. Plus C# is closer to PHP than VB.NET.Jason N. GaylordASPInsider and Microsoft MVPhttp://jasongaylord.com...

what lang does everyone prefer? Thanks, Peter Iuvara, MCP It's a very famous debatable question in forums! I would say if you are from Java/C/C++, then C# would be easy to use for you. If you are from ASP/VB, then VB.NET would be easy for you! As long as if you follow CLS, then what ever the language you use, it's going to be the same for CLR!Sreedharhttp://www.w3coder.orgweblog http://weblogs.asp.net/skoganti I would agree with that ... just curious as to what user's on this forum predominantly code in?Thanks, Peter Iuvara, MCP I came from no real programming background, started i...

Help Me C# is working But VB.net Not working
Edited by SomeNewKid. Please post code between <code> and </code> tags. Pls, Help me, It is simple chat programe. My <IFRAME> src is calling the bellow event in C# it is working perfectly. private void Page_Load(object sender, System.EventArgs e) { string sDealer=""; string sWrite=""; if ( Session["Channel"] != null ) sDealer = Session["Channel"].ToString(); Response.Write( "<meta http-equiv=\"Refresh\"content=\"4\">" ); sWrite=MyModule.MyChat.GetAl...

DW control in VB. NET custom control
Hi Just attempting to investigate some DW.NET aspects. In regards to building a ancestor object with VB.NET, why is when I create a datawindowcontrol custom control that any reference to it is like (assuming that u_dw is the name of the DW control): custom_control.u_dw.DatawindowObject = "myDW" instead of simply: u_dw.DatawindowObject = "myDW" If I create a .NET custom control with a button on it, I can directly reference the button's methods and properties without having to qualify it with the custom control's name. When I was developing with po...

Converting from vb.net to c#.net
Hello I have recently purchased VBGolds RawDataPrinter which should allow users to print directly to label printers on our network directly from a c# web form.  Unfortunately they only supply vb.net examples and I have need of c#.  The component has been installed on our web server.  This is some code that they have supplied as examples in vb.net   "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" >"Head1" runat="server&qu...

Difference Between VB.Net And C#.Net
 hi friends,Please tell me the key difference between the Vb.Net And C#.Net..wheather vb.net supports operator overloading..Thanks & Regards,Pravin Kumar T.   Regards,Pravin Kumar T. pravin_thanusu:Please tell me the key difference between the Vb.Net And C#.Net.. Neither are ASP.NET so you probably want a different forum.  You'll also find your answers at Microsoft.com. JeffPlease: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.  >Please tell me the k...

vb.net and c#.net same app
Been asked before, but... If I build a c# web app and someone builds a vb.net desktop app, can they communicate? and can you develop against it...ie..in my c# web app, can I add the vb.net desktop app as a reference? or at least make calls to it? web apps or desktop apps can contain components created in either language. i have a web solution that contains 4 projects. One of the projects is C# the others are VB.Mike Banavige~~~~~~~~~~~~Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/ Thanks...that is what I was looking for ...

use of vb.net and c#.net
i make a small website using vb.net,  in it i make a 2 class in C#.net. that i want to use in that website, i dont know how to call that class. let your class is  - class clsMyClass    {        public void myFunction()        {            // do something        }    } now the first way - create objectclsMyClass myObj = new clsMyClass();myObj.myFunction(); // call function================...

Web resources about - Why does this work in VB.NET but not C#? - asp.net.custom-server-controls

The GOP Establishment Has Found The One Thing That Can Make Donald Trump Palatable: Ted Cruz
Huffington Post The GOP Establishment Has Found The One Thing That Can Make Donald Trump Palatable: Ted Cruz Huffington Post The Republican ...

Apple to Open iOS App Development Centers, Starting in Italy
Apple has announced that it will be opening its first iOS app development center in Europe at a partner institution in Naples, Italy. Apple expects ...

Inspector General: Hillary Clinton’s Emails Contained Highly Classified Intel
Hillary Clinton’s emails are like a ghost that just won’t stop haunting her. For several months now, the former Secretary of State has been forced ...

Watch Big Bash League Cricket Semifinal Live Online: Streaming, Adelaide Strikers Vs. Sydney Thunder
Australia’s fifth Big Bash League T20 cricket extravaganza finished off its 32-match schedule last weekend and fans can watch live online as ...

Star Wars Toys Brought in $700 Million in 2015
Star Wars toys were the hottest property in the toy industry last year, generating hundreds of millions of dollars of sales, according to new ...

Astronomers' findings point to a ninth planet, and it's not Pluto
Los Angeles Times Astronomers' findings point to a ninth planet, and it's not Pluto Los Angeles Times This artist's conception illustrates ...

Mumbai records year’s coldest day
The minimum temperature recorded is more than four degrees below normal, making it one of the coldest days recorded in the city this month.

Cheap Oil, Good for Consumers, Is Slamming Stocks. Why?
A plunging price of oil is dragging stock markets to their worst start to a year ever, even though low fuel prices are great for consumers and ...

Massacre Came as Pakistan University Honored a Hero of Nonviolence
New York Times Massacre Came as Pakistan University Honored a Hero of Nonviolence New York Times The university attacked by the Pakistani ...

WWE Rumors: Daniel Bryan Returns, AJ Styles Debuts, ‘Royal Rumble’ — Biggest Week In WWE History?
Rumors always fly around in the world of professional wrestling, but there are a lot happening right now. Actually, there are so many things ...

Resources last updated: 1/21/2016 8:06:02 AM