简体   繁体   中英

how to write a JavaScript function in c# in code behind?

I have to call things from code behind.I am able to call Html from code behind like this :

Which looks Like this

<h1>cubus outperform EV Analytics</h1>

<div style="margin:0px; position:absolute; top:12px; left:0px; bottom:0px; right:0px;" id="EVObject_xml">
        <object id="EVObject" name="EVObject" lang="en-US" 
            width="100%" height="100%" CodeBase="http://kl12ACUC/EVServer/Client/Ctrl.cab#version=11,0,0,0" 
            ClassId="clsid:80AC1200-0BBE-499A-A9E9-5F334DBC8E89">
                <param Name="Server" Value="kl12ACUC" />
                <param Name="EnableTabBar" Value="True" />
                <param Name="Theme" Value="Ribbon" />
        </object>
    </div>
    <div id="ribbon" />
    <div id="backstage-container" /> 

And Now It Looks Like this in Code Behind

 string strHTMLGrid = "";
            strHTMLGrid = strHTMLGrid + "<h1>" + sHeading + "</h1>";
            strHTMLGrid = strHTMLGrid + "<div id='EVObject_xml' style='margin: 0px; position: absolute; top: 12px; left: 0px; bottom: 0px; right: 0px; '>";
            strHTMLGrid = strHTMLGrid + "<object name='EVObject' width='100%' height='100%' id='EVObject' codebase='" + sUrlHtml + "' lang='en-US' classid='clsid:80AC1200-0BBE-499A-A9E9-5F334DBC8E89'>";
            strHTMLGrid = strHTMLGrid + "<param name='Server' value='" + sServername1 + "'>";
            strHTMLGrid = strHTMLGrid + "<param name='Server' value='" + sTheme1 + "'>";
            strHTMLGrid = strHTMLGrid + "<param name='Server' value='" + sHeading + "'>";
            strHTMLGrid = strHTMLGrid + "</object>";
            strHTMLGrid = strHTMLGrid + "</div>";
            strHTMLGrid = strHTMLGrid + "<div id='ribbon'>";
            strHTMLGrid = strHTMLGrid + "<div id='backstage - container'> </div>";
            strHTMLGrid = strHTMLGrid + "</div>";

Now I need to call the JavaScript Function on the load of the body of this html document.The code I do have with me looks like this

function OpenCube()
         {
                EVObject.Enable(UIAuthorisationType.UIAuthorisationToolbar, true);
                EVObject.Enable(UIAuthorisationType.UIAuthorisationTabBar, true);
                EVObject.TabBarPosition = TabBarPositionType.TabBarPositionBottom;
                EVObject.Allow(ActionAuthorisationType.UIAuthorisationToolbarText, false);
                EVObject.ToolBar.LargeButtons = false;
                EVObject.Enable(UIAuthorisationType.UIAuthorisationLocalViews, false);
                EVObject.Allow(ActionAuthorisationType.ActionAuthorisationDataEntry, false);
                EVObject.Allow(ActionAuthorisationType.ActionAuthorisationSaveView, true);
                EVObject.Allow(ActionAuthorisationType.ActionAuthorisationExport, true);
                EVObject.Allow(ActionAuthorisationType.ActionAuthorisationExportToExcel, true);
                EVObject.ViewLocations = "General=/";
                EVObject.object.attachEvent("NeedDataSourceCredentials", DataSourceCredentials);
                EVObject.object.attachEvent("NeedServerCredentials", ServerCredentials);
                EVObject.Views.Open("/Outdb/mis");
                LeaveBackstage();
                ExpandRibbons(false);
                ShowBackstage(false);

                 }

1)Problem No.1 ====Now How do I call the javascript Function in C#.
2)Problem No.2===== How to call the Javascript function the load of body in which I have written the html document.

You should use RegisterStartupScript

private void Page_Load(object sender, System.EventArgs e)
{
    string jScriptValidator;
    jScriptValidator="<script> function ReqFieldValidator()" + 
                " { if (document.forms[0].txtField.value == '') \n"; 
    jScriptValidator+="{ alert('TextBox cannot be empty') \n ";
    jScriptValidator+="return false; \n";
    jScriptValidator+="} \n";
    jScriptValidator+=" return true \n";
    jScriptValidator+=" } </script>";
    Page.RegisterStartupScript("regJSval",jScriptValidator);
    btnSubmit.Attributes.Add("onclick","return ReqFieldValidator()");
}

For more you can refer this page : http://www.codeproject.com/Articles/11098/Use-Call-RegisterStartUpScript-RegisterClientScrip

As I heard from an interviewer Before visual studio 2013,people had this problem,they wanted to call javascript in code behind file.So below is one of the way using runat="server" tag.

<script runat="server">
//your java script code
</script>

Above code will be called after client side ie it will be executed at server side

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