Mathew Nolton Blog

Software dementia unleashed...

June 2003 - Posts

Hippo.Net

I keep meaning to spend some time with Hippo.Net. The project that Jan Tielen is developing. It appears really promising.

Anytime something can take the tediousness out of the build process, I am all for it.

Posted: Jun 04 2003, 02:48 PM by MatiasN | with 1 comment(s)
Filed under:
Forcing a Postback using ASP.Net and JavaScript

I am in the process of putting together an article for how to write a relatively unique custom control. During its writing, I had to force a post back within some client-side javascript. It is a pretty handy piece of code so I thought I would share it with the world.

First, in my aspx code, I put in this piece of code for a div tag.

onmousedown="javascript: <%#getPostBack()%>;"

Next, I wrote the getPostBack piece of code.

protected string getPostBack()

{

   return this.Page.GetPostBackEventReference(this, "@@@@@buttonPostBack"");

}

The getPostBack() method is taking advantage of the GetPostBackEventReference .net method call that enables you to hijack the same client-side javascript postback code for your own use. The second parameter enables you to create a custom event argument that is unique to your own uses. You will need this event argument later...

Next, we need to modify the Page_Load() code in order to determine when a postback occurred.

protected void Page_Load(object sender, System.EventArgs e)

{

       // this Is a postback, then we care

       if( this.IsPostBack )

       {

              // determine who caused the post back

              string eventArg = Request[ "__EVENTARGUMENT" ];

              // if null ( could it ever be null? )

              if( eventArg != null )

              {

                     // this post back can occur if we raise the event or if the Web Form itself raises the event.

                     // therefore, i always like to put something in the eventarg that lets me identify it as an event

                     // that i raised. i use @@@@@. but you can use whatever you like, just make sure it is unique. 

 

                     // i also like to make the ClientId part of the value if i am posting back within a user control or

                     // custom control. including the ClientId in a user or custom control enables me to programmatically

                     // determine which instance of the control executed the postback. again we do this because all postbacks

                     // for all instances of all controls on the page are being funneled through this one method.

                     int offset = eventArg.IndexOf( "@@@@@" );

                     if( offset > -1 )

                     {

                          // this is an event that we raised. so do whatever you need to here.

                     }

              }

       }

}

Hope this helps someone out in VirtualWorld. As always, comments and feedback are appreciated.

Mathew



--
Composed with Newz Crawler 1.4 http://www.newzcrawler.com/
More Posts