Gilad Adar
Gilad Adar

Reputation: 167

Using c# string property inside a javascript in asp.net

I have the following JavaScript and i need to set the "file" to some string from the c# code.

how can i do that ?

<script type="text/javascript">

    jwplayer("myElement").setup({
        file: "rtmp://localhost/someFolder/ + VideoFile/,
        height: 240,
        image: "/assets/myVideo.jpg",
        rtmp: {
            bufferlength: 0.1
        },
        width: 280
    });

</script>

c#

String VideoFile = "Some video file link"; // example Video01.flv

So, the result will be:

file: "rtmp://localhost/someFolder/Video01.flv/,

Thanks for the helpers.

Upvotes: 1

Views: 446

Answers (4)

J0e3gan
J0e3gan

Reputation: 8938

You can use the .NET string property's value in the markup as follows:

<script type="text/javascript">

    jwplayer("myElement").setup({
        file: "rtmp://localhost/someFolder/<%= this.VideoFile %>",
        height: 240,
        image: "/assets/myVideo.jpg",
        rtmp: {
            bufferlength: 0.1
        },
        width: 280
    });

</script>

A related SO question speaks to using .NET class properties in markup more generally.

Upvotes: 1

jasonwarford
jasonwarford

Reputation: 746

You can set the value in the code behind and use it in the markup like this:

<%= MyUrlVariable %>

Upvotes: 1

mason
mason

Reputation: 32694

Create a public property on your code behind. Notice it has to be public in order to be seen by the markup page. Let's initialize it in page load.

Code Behind

public string VideoFile {get; set;}

protected void Page_Load(object sender, EventArgs e)
{
    VideoFile="myfile.mp4";
}

Then change your script so that you embed the property inline.

Markup

<script type="text/javascript">
                    jwplayer("myElement").setup({
                        file: "rtmp://localhost/someFolder/<%= VideoFile %>",
                        height: 240,
                        image: "/assets/myVideo.jpg",
                        rtmp: {
                            bufferlength: 0.1
                        },
                        width: 280
                    });
                </script>

For more info about inline server tags, see this blog post.

Upvotes: 1

Rashmin Javiya
Rashmin Javiya

Reputation: 5222

Create property and set the value at the page load time and access the same in aspx file

Code behind

public string VideoFile { get; set; }

public void Page_Load(object sender, EventArgs e)
{
    VideoFile = "Video01.flv";
}

Aspx

jwplayer("myElement").setup({
    file: "rtmp://localhost/someFolder/<%= VideoFile %>",
    height: 240,
    image: "/assets/myVideo.jpg",
    rtmp: {
        bufferlength: 0.1
    },
    width: 280
});

Upvotes: 3

Related Questions