简体   繁体   中英

Differences between Script & Code behind?

Please let me ask something. I just confused for what is the difference of javascript, JQuery and code behind attributes. for example: ASPX

<tbody id="toggleSup" runat="server">

C#

toggleSup.Visible = false;

--------------------------------------- OR ---------------------------

C#

CallScript((string)(Session["toggle"])); 

private void CallScript(string str)
    {
        string scriptx = "<SCRIPT LANGUAGE='javascript'>";
        scriptx += "toggle('" + str + "');";
        scriptx += "</script>";

        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "scriptx", scriptx, false);
    }

Script

function toggle(para1) {           
        if (para1 == 0) {
            $('#toggleSup').hide();
        }
        else {
            $('#togglePO').hide();
        }
    }

for these two difference things, most of the developers use script. Why? Actually C# code is only one row. different thing is if I use script, no need to use runat="server" but if I used code behind, need to use runat = "server". So I think there may be definately have advantages. Please explain me, if possible...

Thanks

If you say toggleSup.Visible = false; in your C#, then the toggleSup does not even get rendered to the DOM. Meaning it's not on the page at all. If you want to make that element visible from some action on the page, then you have to make a round trip to the server and re-render all (postback) or part (ajax) of the page.

Alternatively, if you allow the toggleSub control to be manipulated from JavaScript (jQuery in this case), then it's part of the DOM and can be acted upon in response to other events on the page. Mainly, this means that the client browser can do things without asking the server for more HTML.

So, the C# method looks simpler to code, but the jQuery method is more flexible if you need a rich client-side experience.

When using "script", the browser performs the work. With runat server, then the browser must either get/post a HTTP request or do "AJAX" to the server.

Using "script" is much faster and is easier to maintain state.

In simple words Java Script and JQuery code runs on the client browser whereas C# code runs on the server.

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