简体   繁体   中英

Pass an asp.net variable to javascript failed

I am going to pass an variable from asp.net server to the javascript but I got an exception.

The name 'serializer' does not exist in the current context In my Admin.aspx.cs

 protected static string urlEdit;
    protected void Page_Load(object sender, EventArgs e)
    {
        // blah blah...
        var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    }

Then in the markup code:

<script type="text/javascript">
            function Edit_Click() {
                var options = SP.UI.$create_DialogOptions();


                options.url = <%= serializer.Serialize(urlEdit) %>;

                              };

try wrapping it in quotes so the javascript will see it as a string...

options.url = '<%= serializer.Serialize(urlEdit) %>';

your code would render...

options.url = site.com/url;//invalid javascript

where as mine would render...

options.url = 'site.com/url';//valid javascript

serializer only exists in Page_Load . Instead of accessing serializer from your markup, why not create public method or property that returns the serialized value instead?

protected static string urlEdit;
protected void Page_Load(object sender, EventArgs e)
{
    // blah blah...
    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
}

public string GetSerializedUrl()
{
    return serializer.Serialize(urlEdit);
}

Then call this method in your markup:

options.url = "<%= GetSerializedUrl() %>";

Update

Actually... you don't need to serialize a string to use it javascript. So your code could be simplified to the following:

protected static string urlEdit = "www.example.com";

And the simplified markup:

options.url = "<%= urlEdit  %>";

serializer only exists within the Page_Load function. For your inline code to recognize it, it needs to be declared in the classes namespace.

protected static string urlEdit;
protected var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
protected void Page_Load(object sender, EventArgs e)
{
}

musefan is also right that options.url needs to be wrapped in quotes.

Normally instead to serialize the variable inject the value into the javaScript with this values

ClientScript.RegisterClientScriptBlock(this.GetType(), "variable", "<script language=javascript> var variableUrl='" + urlEdit+"' </script>");

Notes that this block of code should be in the load method of your page

then in you script, you could use the variable like this

    <script type="text/javascript">
        function Edit_Click() {
            var options = SP.UI.$create_DialogOptions();


            options.url = variableUrl;

                          };

Personally, I find it easy to make use of hidden fields to do the job.

ie

Markup (.aspx)

<asp:HiddenField ID="hiddenRequest" runat="server" ClientIDMode="Static" />

Code behind (.aspx.cs)

hiddenRequest.value = "Test!";
string test = (string)hiddenRequest.value;

Javascript with jQuery (for easy selection) (.js)

$('#hiddenRequest').val('Test!');
var test = $('#hiddenRequest').val();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM