Time again to dig into the next question on Scott Hanselman’s list of ASP.NET interview questions.  This question focuses on PostBacks; specifically, this question is posed:

"Explain how PostBacks work, on both the client-side and server-side. How do I chain my own JavaScript into the client side without losing PostBack functionality?"

Lets start with what a simple definition: A PostBack happens when the contents of a web page is sent back to the server for processing.

From the definition, it would seem that most (if not all) PostBacks happen on the server side.  Indeed, server-side PostBacks are the type you’re probably most familiar with.  For example, any time a component is declared with runat=server, it is taking advantage of the server-side handling of PostBacks.  When a PostBack happens, the information entered into a form is sent back to the same page for processing.  Newer versions of ASP.NET allow for cross-page PostBacks, but the vast majority of pages still post back to themselves.  That being said, there are a few things to remember when working with PostBacks:

  • The PostBack request contains the current value of every control within your form declared with runat=server.  The data is commonly referred to as the Post Data
  • The Post Data will also contain the content of the page’s ViewState.  In this case (unless specified otherwise), the ViewState will contain the original value of these same controls before their values were changed
  • Finally, the Post Data will contain the ID of the control that initiated the PostBack (for example, if you clicked a ‘Submit’ button, the ID of that button will be part of the Post Data

There is some additional information on ViewState and server-side PostBacks on MSDN, but that should get you started.

Regarding client-side PostBacks, and despite my best efforts to fully research the topic, without being able to directly talk to a potential interviewer posing this question, I’m not exactly certain what kind of answer Mr. Hanselman is looking for.  Almost every article I’ve found online seems to suggest that a client-side PostBack is little more than having an ASP.NET button, link button, or image button call a bit of JavaScript either before doing a server-side PostBack (i.e. displaying a confirmation dialog or doing some form validation) or in lieu of doing a server-side PostBack at all.  If that is indeed the case, this can be accomplished a few different ways.

For example, assume you have an ASP.NET button declared with ID = "btnSubmit"

<asp:button id="btnSubmit" runat="server" text="Do Something Potentially Destructive?"/>

And you want to add a confirmation dialog before the server-side PostBack occurs.  One way is to add an attribute to the control on PageLoad:

void Page_Load(object sender, EventArgs e)
{
  
btnSubmit.Attributes.Add("onclick", "return confirm(‘Are you want to do something potentially destructive?’);");
}

Starting with .NET 2.0, you can also take advantage of the OnClientClick property to accomplish the same goal:

<asp:button id="btnSubmit" runat="server" OnClientClick="return confirm(‘Are you sure you want to do something potentially destructive?’);" text="Do Something Potentially Destructive?" />

Assuming I’ve correctly interpreted the question, I believe that should set you on your way.  If not, please let me know in the comments.  This blog is as much a learning experience for me as it is for any potential readers, so don’t be shy. :)

Now, there is one lingering branch of this question that still needs answered.  Specifically, how do we chain our own JavaScript without losing PostBack functionality?  Well, again, to really answer this question, it would be best to get some more information as to exactly what is being asked.  That is, if you use one of the two methods above to add a confirmation dialog to a button, you won’t lost PostBack functionality.  In fact, I had to dig a bit deeper to really find a good example of how to answer this question.  Surprisingly (or maybe not-so-surprisingly), the answer came from Scott’s site.

What I think is being asked here is better understood when thinking about an ASP.NET LinkButton as opposed to your everyday Button.  If you’ve ever noticed, ASP.NET achieves the functionality of LinkButtons by creating and then calling a JavaScript function called __doPostBack that takes in two arguments — an EventTarget (the server-side function to be called) and an EventArgument (any arguments to that function).  This is important to note because if you now wanted to create your own JavaScript function that did some client-side processing and then called the appropriate server-side method, you would need to know the signature of this __doPostBack method.  It turns out you can get this method signature by calling Page.GetPostBackEventReference.  Tying this altogether, assume we have a LinkButton with ID = "lnkSubmit" and you want to set the NavigateUrl on PageLoad to your own JavaScript function that calls __doPostBack when it is finished.  You could do this in the following manner:

string PostBackReference = Page.GetPostBackEventReference(lnkSubmit);
lnkSubmit.NavigateUrl = BuildMyJavaScriptFunction("My first parameter", PostBackReference);

That should wrap it up.  If you have any questions, leave them in the comments!