ASP.NET: Postbacks for Algernon
We're hip deep into this bank today. Doing some amazing stuff and making a very rich user experience with as few moving parts as possible. Reuse, Reuse, Reuse.
Here are some interesting things I've learned/been reminded of
- Pretty PostBacks when using RewritePath: When you call HttpContext.RewritePath to make “junk.aspx?specialparam=Y“ look like “someotherpage.aspx“ it will look nice in the Browser's Location Bar, but not in the Form Action. Consequently, when you postback, you'll see the ugly URL in the Browser's Location Bar.
- So, call Page.RegisterStartupScript with this little block:
Page.RegisterStartupScript("rewritepathfix","<script language='javascript'>document.YourFormNameHere.action = document.location.href;</script>"); - Complex Custom Confirmation Dialogs on LinkButtons in DataGrids while STILL maintaining PostBacks (whew!): If you want to have a javascript:confirm() dialog popup when clicking on a LinkButton (not a Button), BUT you want the javascript dialog to contain a context-specific message based on data, AND you still want a PostBack event to occur:
- Chain the JavaScript events by grabbing the LinkButton in the Grid's ItemDataBound. Make a parallel HyperLink with an onclick that contains your javascript confirm, THEN the PostBack javascript, which can be got to via Page.GetPostBackEventReference. In this example the function GetConfirmJavascript builds a confirm(”yada yada”) string based on the object passed in.
public void GridDataBound(Object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
string target = string.Empty;
HyperLink linkCancel = new HyperLink();
LinkButton linkButtonCancel = (LinkButton)e.Item.Cells[CancelColumn].FindControl("LinkButtonCancel");
linkEdit.Text = linkButtonEdit.Text;
target = Page.GetPostBackEventReference(linkButtonEdit);
linkCancel.NavigateUrl = GetConfirmJavascript(e.Item.DataItem as MyObject, target);
linkButtonCancel.Visible = false;
e.Item.Cells[CancelColumn].Controls.Add(linkCancel);
}
}
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter

It's out and here's the scoop.
Seems like so many different technologies get promoted by Microsoft and some really great ones get lost in the fray. Remember Microsoft Ultimate TV? It was a BETTER Tivo and it could record TWO SHOWS AT ONCE WITH TWO SEPARATE TUNERS. I got the feeling it died because Microsoft let it die...rather that on any technical merit.