tags:

views:

56

answers:

3

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;
        }  
A: 

You can not actual send data from code behind.

You can render on the page a call to this function and this will called when the page load.

For example, one way.

<asp:Literal run="server" id="txtForCallTheFun" />


txtForCallTheFun.Text = "\n addTab('2','1'); \n";

and you place the Literal somewhere on the end of your page, or inside a onLoad function or something similar.

Aristos
A: 

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.

Joel Etherton
+3  A: 

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.

Alison