Results 1 to 10 of 10

Thread: Check this out

  1. #1

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Cool Custom Postback Class[Check this out]

    Friends, Romans, Frogs lend me your ears.

    Do you need to load Data based on one page based on an action from another page? Do you need to do some weird JavaScript and then post back? Do you want to use the JavaScript setTimeOut to create a really funky Server Side Timer?

    I say unto you no longer shall you fight to be able to raise a custom PostBack Event from your JavaScript. Behold the very short, very ugly, very over rated PostBackEvent.

    Simply drop one on your WebForm write your event code and invoke it from JavaScript you write and only JavaScript you write.

    Not only that but it is only slightly agrivating at Design Time..........

    PHP Code:
    using System;
    using System.ComponentModel;

    using System.Web;
    using System.Web.UI;
    using System.Web.UI.Design;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    namespace 
    Controls
    {
        
    /// <summary>Provides a quick drop on controless PostBackEvent.</summary>
        /// <developer>Zeke Naulty</developer>
        /// <createdOn>3/17/05</createdOn>
        
    [Designer(typeof(PostBackEvent.PostBackDesigner), typeof(ControlDesigner))]
        public class 
    PostBackEvent ControlIPostBackEventHandler
        
    {
            
    internal class PostBackDesigner ControlDesigner
            
    {
                public 
    override string GetDesignTimeHtml()
                {
                    
    PostBackEvent c null;
                    if(
    Component is PostBackEvent= (PostBackEvent)Component;
                    if(
    != null) return "<!-- PostBackEvent " c.ID "(" c.UniqueID ")  Purpose: " c.EventPurpose " -->";
                    return 
    null;
                }
            }
            public 
    event EventHandler PostBack;
            private 
    string _eventName "CustomPostBack";
            public 
    string EventPurpose
            
    {
                
    get{return _eventName;}
                
    set{if(_eventName != value_eventName value;}
            }
            public 
    PostBackEvent(){}
            protected 
    virtual void OnPostBack(EventArgs e){if(PostBack != nullPostBack(thise);}
            public 
    void RaisePostBackEvent(string eventArgument)
            {
                
    EventArgs e = new EventArgs();
                
    OnPostBack(e);
            }
            protected 
    override void Render(System.Web.UI.HtmlTextWriter writer)
            {
                
    writer.Write("<!-- PostBackEvent " this.ID "(" this.UniqueID ")  Purpose: " _eventName " -->");
            }
            protected 
    override void OnPreRender(System.EventArgs e)
            {
                
    this.Page.GetPostBackClientEvent(this"");
                
    base.OnPreRender(e);
            }
        }

    Last edited by Magiaus; Apr 8th, 2007 at 02:37 PM. Reason: title change
    Magiaus

    If I helped give me some points.

  2. #2
    Hyperactive Member Coool's Avatar
    Join Date
    Feb 2006
    Location
    System.Coool
    Posts
    333

    Re: Check this out

    Really Bump for me..
    Can anyone explain how to implement this.. ?
    I am using .NET 2010 with Windows 7

  3. #3

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Re: Check this out

    Drop the control on a page(we say it has an id of "PBEvent"). Implement an event handler for it's event(
    Code:
    protected void PBEvent_PostBackEvent(object sender, EventArgs e){/*your code*/}
    ). Then when ever you need to raise an event from JavaScript
    Code:
    <script>__doPostBack('PBEvent','');</script>
    Magiaus

    If I helped give me some points.

  4. #4

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Re: Check this out

    I HATE VB
    Code:
        Public Class PostBackEvent
            Inherits Control
            Implements System.Web.UI.IPostBackEventHandler
    
            Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
    
            End Sub
    
            Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
                Dim s As String = Me.Page.GetPostBackEventReference(Me) 'force the page to have __doPostBack
    
                MyBase.OnPreRender(e)
            End Sub
    
            Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
                writer.WriteLine("<!-- Custom PostBack JavaScript Provider:" & Me.ID & " -->")
            End Sub
        End Class
    You'll notice the vb one is not completed, because VB is EVIL!!!!
    Magiaus

    If I helped give me some points.

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Check this out

    Nice. Could you rename the thread title to something search friendly, like "Custom Postback Class" ? ZEKE.

  6. #6

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Re: Check this out

    Good, idea.
    Magiaus

    If I helped give me some points.

  7. #7
    Hyperactive Member Coool's Avatar
    Join Date
    Feb 2006
    Location
    System.Coool
    Posts
    333

    Re: Check this out

    Sorry for late posting. I checked the above code and i noticed that it works fine. But instead of creating a whole new control i have put this in Page Init.

    Me.Page.ClientScript.GetPostBackEventReference(Me, "")

    I had one question in my mind .. When I put the above code in my Page i actually made it commented. so that I can check whether __doPostBack class really created without it or not? I have no control in my form. After running the appication i confirmed - __doPostBack event was not created. Now I removed the comment and run the project and i can Now see the __doPostBack event generated. until this it run as per my expectation.

    Now but when i made the Code commented again, page still generate __doPostBack.

    My Basic understanding is - at least one control needs to be set to Visible, for the server to generate the __doPostBack function in our page.

    Can anyone tell this strange behavior ?
    I am using .NET 2010 with Windows 7

  8. #8

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Re: Check this out

    One control must call Me.Page.GetPostBackEventReference(ctl,"arg"), and MS states the place to call this is in prerender; invisible controls don't render or prerender

    Page.GetPostBackEventReference(control, argument)

    invokes internal/friend Page.RegisterPostBackScript() and returns you the needed jscript call.

    Think about in terms of page weight, unless a rendered control is going to call that js func. It is a waste to render it. That is the point of the render chain.
    Magiaus

    If I helped give me some points.

  9. #9
    Hyperactive Member Coool's Avatar
    Join Date
    Feb 2006
    Location
    System.Coool
    Posts
    333

    Re: Check this out

    Thanks for Information but I wanted to know the strange behaviour i mentioned above. Anyway No Pro. Whatever it is i can now manage to force the page to have __doPostBack method with RIGHT METHOD. All bec'z of u.

    Thanks a lot once again..
    I am using .NET 2010 with Windows 7

  10. #10

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Re: Check this out

    that's the point of the post
    Magiaus

    If I helped give me some points.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width