Scott Hanselman

ASP.NET: Postbacks for Algernon

November 21, 2003 Comment on this post [2] Posted in ASP.NET | Javascript
Sponsored By

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.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
November 21, 2003 5:29
It's a PAIN to merge server AND client side scripting together.. but customers are always right! right?
October 26, 2005 23:05
FYI, here is a server-side solution to fixing the form action:
http://odetocode.com/Blogs/scott/archive/2004/09/22/509.aspx

Regards
Tim

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.