0

I am developing a website by using ASP.NET. I have a page where user can put advertisements. So after user fills the required fields I am redirect them to another page to review. In that page I am temporally shows what user has entered. So if they wrong they can go back or else can confirm. So I avoid using sessions to pass the data to another page because it has a cost to the server. Also these data are not critical. So I am put them in a Json string and post them.

Here what I have tried.

        JavaScriptSerializer json = new JavaScriptSerializer();
        Ad = new AdDetails.Ad();
        Ad.ContinentIdRef = byte.Parse(ddContinent.SelectedValue);
        Ad.ContinentName = ddContinent.SelectedItem.Text;
        Ad.CountryIdRef = byte.Parse(ddCountry.SelectedValue);
        Ad.CountryName = ddCountry.SelectedItem.Text;
        Ad.CityIdRef = UInt16.Parse(ddCity.SelectedValue);
        Ad.CityName = ddCity.SelectedItem.Text;
        //......
        string jsonString = json.Serialize(Ad);

My button code is

<asp:Button ID="btnCheckAd" runat="server" UseSubmitBehavior="false" Text="Check My Add" CssClass="btn btn-success" Width="130" OnClick="btnCheckAd_Click" PostBackUrl="~/CheckAd.aspx" />

So now I want to retrieve this Json string from CheckAd.aspx and read those values to set labels and textboxes values. So how to do that?

Prageeth Liyanage
  • 1,612
  • 2
  • 19
  • 41

1 Answers1

0

Try like this:

Request.InputStream.Position = 0;
System.Text.Encoding encoding = Request.ContentEncoding;
using (var inputStream = new StreamReader(Request.InputStream,encoding))
{
  strJSON = inputStream.ReadToEnd();
}

JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
object serJsonDetails = javaScriptSerializer.Deserialize(strJSON, typeof(object));
Dgan
  • 10,077
  • 1
  • 29
  • 51
  • Thank you Ganesh. I almost got it. strJSON got the Json string correctly. But at the end of the line it throws an error saying "Invalid JSON primitive: ctl00." Also I tried like this adObject = javaScriptSerializer.Deserialize(jsonString); adObject is a instant of a class. But it also throws the same error. Also I found that Json string that catch from my destination page is difference than my source page. – Prageeth Liyanage Nov 29 '14 at 12:07