Last post Sep 13, 2008 02:39 PM by TATWORTH
Sep 10, 2008 01:51 PM|ivan_samuelson|LINK
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:
Public Event SelectedPageChanged As EventHandler
Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
Dim newPage As Integer
If Integer.TryParse(eventArgument, newPage) Then
Me.SelectedPage = newPage
Protected Overridable Sub OnSelectedPageChanged(ByVal e As EventArgs)
RaiseEvent SelectedPageChanged(Me, e)
Here is the C# code:
public event EventHandler SelectedPageChanged;
void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
if (int.TryParse(eventArgument, newPage))
this.SelectedPage = newPage;
protected override void OnSelectedPageChanged(EventArgs eventArgs)
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#.
Sep 10, 2008 02:24 PM|shados|LINK
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
Sep 10, 2008 02:25 PM|jameswright|LINK
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.
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!
Sep 10, 2008 03:04 PM|ivan_samuelson|LINK
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"
2 SelectMethod="List" TypeName="SBAA.Web.News">
4 <asp:ControlParameter DefaultValue="1" Name="pagenum"
ControlID="pn1" PropertyName="SelectedPage" Type="Int32" />
5 <asp:ControlParameter ControlID="ddlSports" DefaultValue="0"
6 Type="Int32" />
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!
Sep 13, 2008 02:39 PM|TATWORTH|LINK
Excellant - please mark this thread as answered.