3

How would I get my JSON string to my asp.net using jquery I am confused about using web methods or arrays or functions all the examples i have seen are in C#. All i want is to take the json string parse it.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    cmd.Connection = conn
    conn.Open()
    Dim ds As New DataSet
    cmd.CommandText = "MY SELECT STATEMENT IS IN HERE(DIDNT WANT TO POST ONLINE)"
    da.Fill(ds)
    da.FillSchema(ds, SchemaType.Mapped)
    Dim myObject = ds.GetXml
    Dim jsonString = New JavaScriptSerializer().Serialize(myObject)
   conn.Close()
End Sub

I want to take either the jsonstring or myobject to the server side. jsonstring is json and myobject is xml. That way I can create a table client side that will include math functions. I am very new at this so I would need a clear explanation and maybe even an example. I have read about pagemethods and I have tried using this example on calling server side functions example

Or can you tell me a simple way to go from sql to ajax. What is the best method for placing large datasets on a asp.net page without using paging.

I am basically rewriting an asp page that they use now to use ajax, because it loads to slow, heres an example of the current asp code and what I am trying to achieve.

                response.write("<tr><td>")

            %><font face="Arial,Helvetica,sans-serif" size="-2"><%
            response.write(rscontest.fields.item("book7"))
            response.write("</td><td>")

            %><font face="Arial,Helvetica,sans-serif" size="-2"><%
            response.write(rscontest.fields.item("dep7"))
            response.write("</td><td>")

            %><font face="Arial,Helvetica,sans-serif" size="-2"><%
            response.write(rscontest.fields.item("lead"))
            response.write("</td><td>")

            %><font face="Arial,Helvetica,sans-serif" size="-2"><%
            response.write(rscontest.fields.item("hear"))
            response.write("</td><td>")

            %><font face="Arial,Helvetica,sans-serif" size="-2"><%
            response.write(rscontest.fields.item("cname"))
            response.write("</td><td>")

%><font face="Arial,Helvetica,sans-serif" size="-2"><%


response.write(rscontest.fields.item("theme") & " - " & rscontest.fields.item("tour"))
            response.write("</td><td align='right'>")

            %><font face="Arial,Helvetica,sans-serif" size="-2"><%
            response.write(rscontest.fields.item("userid"))
            response.write("</td><td align='right'>")

            %><font face="Arial,Helvetica,sans-serif" size="-2"><%
            response.write(rscontest.fields.item("pax_count"))
            pax = pax + rscontest.fields.item("pax_count")
            response.write("</td><td align='right'>")

            %><font face="Arial,Helvetica,sans-serif" size="-2"><%
            IF rscontest.fields.item("status") = "XL" then
                response.write(formatnumber(rscontest.fields.item("CXVALUE"),2))
                sales = sales - formatnumber(rscontest.fields.item("CXVALUE"),2)
            ELSE
                response.write(formatnumber(rscontest.fields.item("AMOUNT"),2))
                sales = sales + formatnumber(rscontest.fields.item("AMOUNT"),2)
            END IF

        response.write("<tr><td rowspan=1>")    
        %><font face="Arial,Helvetica,sans-serif" size="-2"></style><%
            response.write(rscontest.fields.item("description"))




            rscontest.movenext

        wend

anything it is writing is from an sql statement, and it uses a running total.

MyHeadHurts
  • 1,514
  • 5
  • 36
  • 87
  • It's not clear what you're asking for. Does the code you post work for showing your json in a textbox? And if so, do you want to do something with it in javascript when the page loads in the browser? – Joel Coehoorn Nov 12 '10 at 21:35
  • Yeah, basically right now it loads the JSON in the textbox, because thats the only way I could tell if I was actually getting a json string. – MyHeadHurts Nov 12 '10 at 21:36
  • I want to be able to get the json string into ajax so i can build tables. Or atleast get it parsed I am basically at a standstill. – MyHeadHurts Nov 12 '10 at 21:37
  • "What is the best method for placing large datasets on a asp.net page without using paging." = It depends on some factors including programming skill level, but for you I would recommend a DataGrid and set the AllowPaging property to false. – NoAlias Nov 19 '10 at 23:00

3 Answers3

2

First, read this SO thread, if it doesn't help - read on.

I'm not sure why you want to pass the json around after you get if from the webservice, but the following steps should get you there:

  1. Create a webservice (start with a default HelloWorld service from the template)
  2. Call the webservice from javasscript (jQuery's $.ajax() does just that)
  3. Assign the xml (json) that you get from the webservice to a hidden field (use asp:HiddenField to make sure it's available on the server)
  4. In your code behind - get the value of the hidden field, and do what you have to do with it.

You should really rephrase your question, and tell us what the ultimate goal is. When you say "I want to build a table", that doesn't mean anything.

Community
  • 1
  • 1
roman m
  • 26,012
  • 31
  • 101
  • 133
  • I have a large dataset, so wouldn't using the json and ajax make my page load faster, rather than using sql. Also the reason is I want to format my information using html and tables. Is this the right direction? – MyHeadHurts Nov 16 '10 at 13:43
1

In your Body tag in ASP.NET put an onload attribute that calls a Javascript function that makes the Ajax call (you can use JQuery or do it manually (just don't forget to put a ServiceReference in your ScriptManager) to a web service method. The return method (in javascript) will then have to parse the JSON. If you're going to build the tables on the server side there is no point in serializing the object into JSON.

NoAlias
  • 9,218
  • 2
  • 27
  • 46
  • i need help with the web service method part, is it a function or what is it? i have a script manager and set the property to true for the web service method but is it a function of some sort? – MyHeadHurts Nov 13 '10 at 00:28
  • When you start a new project in Visual Studio you pick a template, such as Windows Forms Application or Asp .Net Web Application. Choose the Asp .Net Web Service template and a hello world web service method is automatically generated for you. Change the Hello World function into the one that you'll be calling from your javascript or jquery. – NoAlias Nov 19 '10 at 03:44
  • do i need to use a webservice to accomplish this. Can i just take my json string from there vb.net side into asp.net another way. – MyHeadHurts Nov 19 '10 at 14:26
  • You can set the value of an ASP .Net hidden field control to the JSON string and then access it in JQuery or Javascript that way if you would rather not use a WebService. – NoAlias Nov 19 '10 at 22:53
  • so using a textbox i could make it hidden and access it that way – MyHeadHurts Nov 20 '10 at 20:21
  • If you make the textbox hidden it won't render client side and you won't be able to access the JSON from your jquery/javascript. Use the asp .net hidden control so that it renders invisible on the client side. – NoAlias Nov 21 '10 at 02:06
  • so only make it not visible in the asp code not the .net side – MyHeadHurts Nov 22 '10 at 03:06
1

I have a large dataset, so wouldn't using the json and ajax make my page load faster, rather than using sql. Also the reason is I want to format my information using html and tables. Is this the right direction?

Whether you pull the data from a web service call on the client or server side you are still loading it through a web service that has to retrieve it from some back end data store (i.e. SQL). Using a client side approach (JSON) will make the delivery of the page quicker but the user will still wait for the overall load which the data is being delivered to the client.

From a performance stand point you would probably want to look into LINQ to deliver only the page of data that you need (i.e. There are 5,000 records and the user is only viewing the first 100 on Page 1, so only return Page 1). Its a pit fall alot of new developers fall into and is a bit complex but you seem to have the appetite for it. Good luck.

meinmkv
  • 76
  • 1