简体   繁体   中英

How to Call javascript function with argument from code behind ASP.NET ,C#?

I want to pass some argument to javascript function from code behind. My Javascript function as follow. how to send arg to this func. from server side code.

 function addTab(tabid,tablist) {
            //to Find the Panel
            var Panel = $find("<%=RadPanelBar2.ClientID %>");
            //To find the Tab Strip
            var tabStrip = Panel._findItemByValue("QueueGridPanel").findControl("RadTabStrip2")
            tabStrip.get_tabs().clear();

            var newTabNames = "MyTab1,MyTab2,MyTab3";
            //var newTabNames = document.getElementById("ctl00_GridContentPlaceHolder_hfTabs").value;

            var TabsNames = newTabNames.split(',');
            var i = 0;
            while (i < TabsNames.length) {
                var ntab = new Telerik.Web.UI.RadTab();
                tabStrip.get_tabs().add(ntab);
                ntab.set_text(TabsNames[i]);
                i++;
            }
            return false;
        }  

Check out RegisterClientScriptBlock at MSDN. You can use it to insert Javascript after a postback.

String csName = "myScript";
Type csType = this.GetType();

// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;

// Check to see if the client script is already registered.
if (!cs.IsClientScriptBlockRegistered(csType, csName))
{
  StringBuilder csText = new StringBuilder();
  csText.Append("<script type=\"text/javascript\"> ");
  csText.Append("addTab(" + myTabID + ", " + myTabList + "); </");
  csText.Append("script>");
  cs.RegisterClientScriptBlock(csType, csName, csText.ToString());
}

The RegisterClientScriptBlock method inserts the client-side script immediately below the opening tag of the Page object's element.

For the uses which you're describing here, I use an asp:Literal control and pump in global javascript declarations.

<script type="text/javascript">
    var FirstNameId = "<%=Me.txtFirstName.ClientID%>";
    var MiddleNameId = "<%=Me.txtMiddleName.ClientID%>";
    var LastNameId = "<%=Me.txtLastName.ClientID%>";
</script>

Then when I need them later on, they are available to the entire page.

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