2009-08-03 23 views
6

Questo è il codice effettivo da quello a cui sto lavorando che accetta un ID dalla stringa di query, recupera un oggetto e analizza in json. Avrò bisogno di archiviare e manipolare questi dati lato client. Qual è il modo migliore per impostare la stringa JSON generata su un oggetto lato client?Quali sono le migliori pratiche per l'aggiunta dinamica di javascript in una pagina asp.net?

Nota: NewObjectCase è una classe con il metodo Get (strID) che restituisce un nuovo oggetto. Tools.GetQueryObject (string key) è un Metod che restituisce il valore stringa della chiave

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Web; 
    using System.Web.UI; 
    using System.Web.Script.Serialization; 
    using System.Web.UI.WebControls; 

     public partial class Testing: System.Web.UI.Page 
     { 
      protected void Page_Load(object sender, EventArgs e) 
      { 
       if (!IsPostBack) 
       { 
        string strJson = new JavaScriptSerializer().Serialize(NewObjectCase.Get(Tools.GetQueryObject("id"))); 
        // add in js to page that will set an client-side js object to this generated json 
       } 
      } 
     } 

risposta

1

Questo è il mio piano originale ma sembrava che ci potrebbe essere un modo migliore per aggiungere un oggetto json e nell'intelligenza ho notato che ha [Deprecato] accanto a Page.RegisterClientScriptBlock().

Page.RegisterClientScriptBlock("clientScript", 
    string.Format("<script type='text/javascript' language='javascript'>objCase = {0}</script>", strJson)); 
5

paio di semplici modi di ottenere il lavoro fatto:

1: Utilizzare i ClientScriptManager.RegisterClientScriptBlock chiamate per inserire lo script direttamente sulla pagina:

protected void Page_Load(object sender, EventArgs e) { 

    // Not sure what your Tools.GetQueryObject is doing, but it should at 
    // the least be performing a UrlDecode to convert the string from any 
    // Url encoding, and as you're about to pass this back to javascript, 
    // you should also HtmlEncode it. 
    string valueFromCodeBehind = "var myValue = " + 
       Server.HtmlEncode(Server.UrlDecode(Request.QueryString["id"])); 

    // Get a ClientScriptManager reference from the Page class. 
    ClientScriptManager cs = Page.ClientScript; 

    // Output the script block to the page. 
    // Notes: 
    // 1) I'm passing true as the final parameter to get the script manager 
    // to auto generate the script tags for me. 
    // 2) You might need to call "RegisterStartupScript" if you need the 
    // JS variables earlier in the page. 
    cs.RegisterClientScriptBlock(this.GetType(), 
           "SetValues", 
           valueFromCodeBehind, 
           true); 
    } 
} 

2: proprietà in code- dietro, di riferimento sul lato pagina:

Nella tua pagina. aspx, qualcosa del genere:

<script type="text/javascript"> 
    var myValue = <%=ValueFromCodeBehind%> 
</script> 

Nel codice dietro, si dichiara la variabile e assicurarsi che sia impostato:

public partial class Testing: System.Web.UI.Page { 
    protected string ValueFromCodeBehind; 

    protected void Page_Load(object sender, EventArgs e) { 

    ValueFromCodeBehind = 
       Server.HtmlEncode(Server.UrlDecode(Request.QueryString["id"])); 
    } 
0

Questo registrerà lo script nella parte superiore della pagina.

ClientScriptManager.RegisterClientScriptBlock Method (Type, Key as String, Script as String) 

L'aggiunta di un tipo e di una chiave assicura che solo una versione dello script venga aggiunta alla pagina per quel tipo e chiave.

In questo overload del metodo RegisterClientScriptBlock, è necessario assicurarsi che lo script fornito nel parametro script sia racchiuso in un blocco di elementi.

Problemi correlati