ScriptManager using in C#

296 Views Asked by At

This is my code:

String url = "http://blablabla.blablabla.blabla/blabla/bla";

    String html_sonuc;
    WebResponse objResponse;
    WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
    objResponse = objRequest.GetResponse();

    using (StreamReader sr =
           new StreamReader(objResponse.GetResponseStream()))
    {
        html_sonuc = sr.ReadToEnd();
        sr.Close();
    }

    ////////////Script Kodu//////////////
    string script = @"  function layerCek({0}) {
        var features = geojson.read({0});
        var bounds;
        if (features) {
            if (features.constructor != Array) {
                features = [features];
            }
            for (var i = 0; i < features.length; ++i) {
                if (!bounds) {
                    bounds = features[i].geometry.getBounds();
                } else {
                    bounds.extend(features[i].geometry.getBounds());
                }

            }
            vectors.addFeatures(features);
            map.zoomToExtent(bounds);

        } else {
            alert('okuma hatası !');
        }
    }"; 
    /////////////////////////////////////
  string result = string.Format(script, html_sonuc);


    ScriptManager.RegisterClientScriptBlock(
        this.Page, 
        this.Page.GetType(), 
        "whatever", 
        result, 
        true);

I want to use javascript in aspx.cs file. But I can not work this code.

string result = string.Format(script, html_sonuc); this line,I am taking error.

error code: FormatException was unhandled by user code.

How can I solve this or which I try different way for solve?

2

There are 2 best solutions below

1
On

Look at the string.Format() documentation.

You need to specify the format, so use something like:

string result = string.Format("Script: {0} - html: {1}", script, html_sonuc);
3
On

Your format string:

string script = @"  function layerCek({0}) {{
    var features = geojson.read({0});
    var bounds;
    if (features) {{
        if (features.constructor != Array) {{
            features = [features];
        }}
        for (var i = 0; i < features.length; ++i) {{
            if (!bounds) {{
                bounds = features[i].geometry.getBounds();
            }} else {{
                bounds.extend(features[i].geometry.getBounds());
            }}

        }}
        vectors.addFeatures(features);
        map.zoomToExtent(bounds);

    }} else {{
        alert('okuma hatası !');
    }}
}}";

Look at {{.