September 9, 2005

LoadVars, C# and HTML

This may or may not be a commonly known issue with Flash/.Net developers.

It was brought to my attention from a friend of mine that he was having an issue when using LoadVars.SendAndLoad() to send a flash form with information and retrieve a status code. What was happening was the result was returning, but with all of the HTML appended to the value of the result.

Immediately, I asked him if the ASPX page was calling Response.End() after the result was written to the page. Sure enough it wasn't. After making the small change to the ASPX code, all was working perfectly.

Below is an example illustrating corrected implementation:

The Flash Code:

var lv:LoadVars = new LoadVars();
var res_lv:LoadVars = new LoadVars();
res_lv.onLoad = function(success:Boolean){
if(success){
          trace(this.nameSent);
     }
}

lv.strName="DesDev";
lv.sendAndLoad("http://localhost/loadVarsExample/vars.aspx",res_lv);


The ASPX (C#) Code:

<head>
<title>An HTML Page</title>
<script runat="server">
protected void Page_Load(Object Src, EventArgs E)
{
  if (!IsPostBack){

     string strName = Request.Form["strName"];
     
     Response.Write("nameSent=" + strName);
     Response.End();
  }
}
</script>
</head>
<body>
<p>This is a paragraph</p>
<p><strong>Bold Text </strong></p>
</body>
</html>


Test out the differences by commenting out Response.End() in the ASPX page.

-DesDev

No comments: