execute javascript from codebehind in asp.net

Learning

I need to fire this script from code behind only on certain dates specified in the database

I need to show an image as a fancybox based on the date specified in CMS system.

I will write my logic to show image or not in the Default.aspx and then somehow pass this piece of code from default.cs to MarterPage Javascript $(window).load(function () Code Block

    <Script>
     $(window).load(function () {

    I want this code to fire at a particular location of master page. I am not sure suppose here.
///HERE???

    });
    </Script>

I want to pass below script as a value so that it js execute ///HERE??? part of code

Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "$('a.fancybox-messageboard').fancybox({ width: 600, height: 440,closeClick: true, hideOnOverlayClick: true, href: 'http://upload.wikimedia.org/wikipedia/commons/1/1a/Bachalpseeflowers.jpg'   }).trigger('click');", true);

I am bit lost with this ...

I simple want to do my logic check in Default.aspc file and show image as a fancy box only in default.aspx page. But my ' $(window).load(function () { });code block is in MasterPage file if i write another ' $(window).load(function () { }); in default.aspx file then fancy box is not showing properly.

How can i achieve this without any issue

UPDATE:

I managed to pull it off.. this is based on the solution posted by Irina Bogomaz.

So far this is working I can add full logic to code-behind later on.

    $(window).load(function () {

       if (window.showMessage) {
            // alert(imgPath);
            //logic to create fancybox
                    $("a.fancybox-messageboard").fancybox({
                        width: 600,
                        height: 440,
                        closeClick: true,
                        hideOnOverlayClick: true,
                        href: imgPath
                    }).trigger('click');
        }

   });


CODE BEHIND

    protected override void OnPreRender(EventArgs e)
    {
        string imgMB = "'http://upload.wikimedia.org/wikipedia/commons/1/1a/Bachalpseeflowers.jpg'";
        string sScript = "var showMessage = true; var imgPath=" + imgMB;
        Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", sScript, true);
    }
Irina Bogomaz

You can achieve this in the following way:

Master page:

<script type="text/javascript">
     $(window).load(function () {
         if (window.isFancybox) {
             //logic to create fancybox
         }
     });
</script>

Default.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
    {
        var fanceBoxDate = new DateTime(2013, 11, 20); //get date from CMS system
        if (DateTime.Today == fanceBoxDate)
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "fancyBox", "var isFancybox = true", true);    
        }
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Stop javascript from executing on page load and execute from ASP.NET codebehind

From Dev

Stop javascript from executing on page load and execute from ASP.NET codebehind

From Dev

How to call a codebehind function from javascript in asp.net?

From Dev

Asp.net how can I send a list from codebehind to javascript for displaying coordinates in googlemap

From Dev

asp.net Run Javascript confirm from codebehind with custom text and return if OK selected

From Dev

Submit POST request from codebehind in ASP.NET

From Dev

Submit POST request from codebehind in ASP.NET

From Dev

Calling server-side C# function from either javascript or codebehind c# function ASP.NET

From Dev

GridView not found in CodeBehind ASP NET

From Dev

GridView not found in CodeBehind ASP NET

From Dev

Call Javascript function from codebehind using vb.net

From Dev

execute javascript from middle of asp.net button click procedure

From Dev

call javascript method from codebehind

From Dev

pass the values from javascript to Codebehind

From Dev

Having trouble geting data from asp.net table from codebehind

From Dev

Having trouble geting data from asp.net table from codebehind

From Dev

how can I get html attribute from ASP/vb.net Codebehind

From Dev

How to pass the SelectedValue from a DropDownList as a query string in a LinkButton with/without using CodeBehind in ASP.net C#

From Dev

how can I get html attribute from ASP/vb.net Codebehind

From Dev

Read text from textarea/textbox in HTML into C# codebehind. (ASP.NET)

From Dev

Asp.net getting label value from codebehind. for google maps coordinate

From Dev

How to make dynamic, in-memory ASP.NET charts purely from codebehind?

From Dev

Execute batch from asp.net hangs

From Dev

Execute Javascript function from GridView ItemTemplate button click ASP.NET Web Forms

From Dev

How to add a JavaScript function to an HmlGenericControl from codebehind

From Dev

How to create a table from CodeBehind using ASP.NET Table Control methods (using, Table,TableRow,TableColumn,TableHeader etc.)

From Dev

In an ASP.NET website with a codebehind at what point are the .cs files compiled?

From Dev

How to get the validator's controltovalidate attribute in ASP.NET codebehind?

From Dev

checkboxlist items as checked by default in codebehind asp.net

Related Related

  1. 1

    Stop javascript from executing on page load and execute from ASP.NET codebehind

  2. 2

    Stop javascript from executing on page load and execute from ASP.NET codebehind

  3. 3

    How to call a codebehind function from javascript in asp.net?

  4. 4

    Asp.net how can I send a list from codebehind to javascript for displaying coordinates in googlemap

  5. 5

    asp.net Run Javascript confirm from codebehind with custom text and return if OK selected

  6. 6

    Submit POST request from codebehind in ASP.NET

  7. 7

    Submit POST request from codebehind in ASP.NET

  8. 8

    Calling server-side C# function from either javascript or codebehind c# function ASP.NET

  9. 9

    GridView not found in CodeBehind ASP NET

  10. 10

    GridView not found in CodeBehind ASP NET

  11. 11

    Call Javascript function from codebehind using vb.net

  12. 12

    execute javascript from middle of asp.net button click procedure

  13. 13

    call javascript method from codebehind

  14. 14

    pass the values from javascript to Codebehind

  15. 15

    Having trouble geting data from asp.net table from codebehind

  16. 16

    Having trouble geting data from asp.net table from codebehind

  17. 17

    how can I get html attribute from ASP/vb.net Codebehind

  18. 18

    How to pass the SelectedValue from a DropDownList as a query string in a LinkButton with/without using CodeBehind in ASP.net C#

  19. 19

    how can I get html attribute from ASP/vb.net Codebehind

  20. 20

    Read text from textarea/textbox in HTML into C# codebehind. (ASP.NET)

  21. 21

    Asp.net getting label value from codebehind. for google maps coordinate

  22. 22

    How to make dynamic, in-memory ASP.NET charts purely from codebehind?

  23. 23

    Execute batch from asp.net hangs

  24. 24

    Execute Javascript function from GridView ItemTemplate button click ASP.NET Web Forms

  25. 25

    How to add a JavaScript function to an HmlGenericControl from codebehind

  26. 26

    How to create a table from CodeBehind using ASP.NET Table Control methods (using, Table,TableRow,TableColumn,TableHeader etc.)

  27. 27

    In an ASP.NET website with a codebehind at what point are the .cs files compiled?

  28. 28

    How to get the validator's controltovalidate attribute in ASP.NET codebehind?

  29. 29

    checkboxlist items as checked by default in codebehind asp.net

HotTag

Archive