Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

I found a nice sample (http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx) on how to set the action attribute of a form HTML element. However when an ajax request is made the code throws a NPE:

 

public class FormControlAdapter : ControlAdapter
{
   protected override void Render(HtmlTextWriter writer)
   { 
       base.Render(new FormWriter(writer)); //Throws NPE when a ajax request is made
   }
}

public class FormWriter : HtmlTextWriter
{
   public FormWriter(HtmlTextWriter writer)
       : base(writer)
   {
       base.InnerWriter = writer;
   }

   public FormWriter(TextWriter writer)
       : base(writer)
   {
       base.InnerWriter = writer;
   }

   public override void WriteAttribute(string name, string value, bool fEncode)
   {
       if (name == "action")
       {
           if (HttpContext.Current.Items["ActionWritten"] == null)
           {
               value = HttpContext.Current.Request.RawUrl;
               HttpContext.Current.Items["ActionWritten"] = true;
           }
       }

       base.WriteAttribute(name, value, fEncode);
   }

}



System.NullReferenceException was unhandled by user code
 Message="Object reference not set to an instance of an object."
 Source="System.Web.Extensions"
 StackTrace:
      at System.Web.UI.PageRequestManager.RenderFormCallback(HtmlTextWriter writer, Control containerControl)
      at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
      at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
      at System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer)
      at System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output)
      at System.Web.UI.Adapters.ControlAdapter.Render(HtmlTextWriter writer)

      at FormControlAdapter.Render(HtmlTextWriter writer) in line 23

      at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
      at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
      at System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer)
      at System.Web.UI.HtmlFormWrapper.System.Web.UI.IHtmlForm.RenderControl(HtmlTextWriter writer)
      at System.Web.UI.PageRequestManager.RenderPageCallback(HtmlTextWriter writer, Control pageControl)
      at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
      at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
      at System.Web.UI.Page.Render(HtmlTextWriter writer)
      at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
      at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
      at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
      at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
 InnerException:

 

I would appreciate any solution on how to set the action attribute for a form or solving this peculiar problem.. Thanks

Edited by JCDenton

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...