c# - building dynamic table from json serialized list object -


i'm trying serialized json list via ajax , create dynamic table in html jquery mobile page. list comes sql database in code behind. don't know if i'm returing json object right, , how access ajax success function.

my goal create dynamically table of members in html.

i created member class:

public class member {     public member()     {         //         // todo: add constructor logic here         //     }      public string fname { set; get; }      public string lname { set; get; }    } 

this function return member list in code behind: (written in class connects sql)

public list<member> return_member_list()     {         list<member> member_list = new list<member>();          string fname;         string lname;           sqlconnection user_con = connect("actconstring");         sqlcommand user_command = create_command(user_con, "select m_first_name, m_last_name member");          sqldatareader rdr = user_command.executereader();           while (rdr.read())         {             member m1 = new member();             fname = convert.tostring(rdr["m_first_name"]);             lname = rdr["m_last_name"].tostring();               m1.fname = fname;             m1.lname = lname;                member_list.add(m1);           }          rdr.close();         user_con.close();          return member_list;     } 

then use aspx page web method return list serialized:

   [webmethod]     public static string return_member_list()     {         dbservices db1 = new dbservices();          list<member> member_list = db1.return_member_list();          var jsonserialiser = new javascriptserializer();          var json = jsonserialiser.serialize(member_list);          return json;     } 

and ajax:

 $(document).ready(function () {                $.ajax({                 type: "post",                 url: "getdata.aspx/return_member_list",                 data: "{}",                 contenttype: "application/json; charset=utf-8",                 datatype: "json",                  success: function (res) {                      //this need                   },                 error: function (res, msg, code) {                     // log error console                     alert("the following error occured: " + msg + " " + code);                 } //error              });          }); 

i'd appriciate creating table dynamically : )

you need success function below

success: function (res) {     var parseddata = json.parse(res.d);     var tablestr = "<table>";     $.each(parseddata, function(){         tablestr +="<tr><td>"+this.fname+"</td><td>"+this.lname+"</td></tr>";     });     tablestr += "</table>";     $('#tablediv').html(tablestr); } 

assumes have div id tablediv


Comments

Popular posts from this blog

shader - OpenGL Shadow Map -

stringtemplate - StringTemplate4 if conditional with length -