Link to home
Start Free TrialLog in
Avatar of samprg
samprg

asked on

Inject Javascript

Hi,

I try to inject Function in client side, but I do not find in client side !
So it is not working.
Thanks
  string js1 = "function FN(list)" +
                "{var selectedType = GetSelectedItems(list);" +
                "<%= Select.ClientObjectId %>.Callback(selectedType);}" +System.Environment.NewLine;
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), _jFunction1, js1, true);
Avatar of ienaxxx
ienaxxx
Flag of Italy image

what about simply echoing the script in the right place of the generated html, between script tag?
Avatar of samprg
samprg

ASKER

I did not understand,
Avatar of samprg

ASKER

Do I need     " <script type="text/javascript"> "
Avatar of samprg

ASKER

I added script tag, same issue
Avatar of Tom Beck
Your code will print your javascript to the page if you put _jFunction1 in quotes.

string js1 = "function FN(list)" +
                "{var selectedType = GetSelectedItems(list);" +
                "<%= Select.ClientObjectId %>.Callback(selectedType);}" + System.Environment.NewLine;
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "_jFunction1", js1, true);

However, this part, <%= Select.ClientObjectId %>, will not translate into a client id when the js is printed to the page because of the order in which events occur. What is that syntax anyway? ColdFusion?

The work around should be to create a javascript variable in the head section to hold the object's id.

<script type="text/javascript">
    var ClientObjId = '<%= Select1.ClientID %>'   //This would be the asp.net syntax for client id
</script>

Then just use the variable in your C# code:

string js1 = "function FN(list)" +
                "{var selectedType = GetSelectedItems(list);" +
                "ClientObjId.Callback(selectedType);}" + System.Environment.NewLine;
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "_jFunction1", js1, true);
Avatar of samprg

ASKER

Thanks ,
_jFunction1 is variable,
I put that jabvascript in user control, and  in the debugger I see that script under clientscrip, but still gives me error and when I go to View Source in IE , I did not find it
The second parameter in a RegisterClientScriptBlock method calls for a "key" value in string format. Here's my version with _jFunction1 as a variable.

            string _jFunction1 = "_jfunction1";
            string js1 = "function FN(list)" +
                "{var selectedType = GetSelectedItems(list);" +
                "ClientObjId.Callback(selectedType);}" + System.Environment.NewLine;
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), _jFunction1, js1, true);

I put the C# code in the code behind of a User Control and added the user control to an aspx page. Here's the View Source from IE.

<script type="text/javascript">
//<![CDATA[
function FN(list){var selectedType = GetSelectedItems(list);ClientObjId.Callback(selectedType);}
//]]>
</script>
Avatar of samprg

ASKER

Right me too, if added the user control to an aspx page,  but when the application added that user control dynamically( at runtime), does not work
Ok, lets take inventory.

1.) RegisterScriptBlock doesn't work (original question)
2.) Oh, BTW: jFunction1 is a variable
3.) Oh, BTW: The script block is in a User Control
4.) Oh, BTW: The User Control is loaded dynamically

Where there any OTHER important details left out of your original question that I should know about?

Here's my C# from aspx code behind for loading the User Control dynamically:

            if (!Page.IsPostBack)
            {
                UniMenuControl uc =
                  (UniMenuControl)Page.LoadControl("UniMenuControl.ascx");
                placeHolder1.Controls.Add(uc);
            }

Here's my Reference directive on the aspx web form:

<%@ Reference Control="UniMenuControl.ascx" %>

My PlaceHolder from the aspx page markup:

<asp:PlaceHolder ID="placeHolder1" runat="server"></asp:PlaceHolder>

All the markup on my User Control:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UniMenuControl.ascx.cs" Inherits="testApp1.UniMenuControl" ClassName="UniMenuControl" %>
<div id="testDiv" style="width:100px;height:100px;background-color:red">test</div>

My code behind from the User Control:

        protected void Page_Load(object sender, EventArgs e)
        {
            string _jFunction1 = "_jfunction1";
            string js1 = "function FN(list)" +
                "{var selectedType = GetSelectedItems(list);" +
                "ClientObjId.Callback(selectedType);}" + System.Environment.NewLine;
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), _jFunction1, js1, true);
        }

The User Control loads on the page and here's the View Source from IE8:

<script type="text/javascript">
//<![CDATA[
function FN(list){var selectedType = GetSelectedItems(list);ClientObjId.Callback(selectedType);}
//]]>
</script>
Avatar of samprg

ASKER

I did not understand this
Inherits="testApp1.UniMenuControl"
testApp1 is the name of the application I use for testing. Yours will be different.
Avatar of samprg

ASKER

I have same what you did but it does not work,
Does ajax ScriptManager in masterpage  prevent to inject javascript in user control?
What is the value of jFunction1?
Avatar of samprg

ASKER

the value is "FN"
I can tell you that it still works when the User Control with the injected javascript is embedded in a Content Page of a Master Page. I cannot test the ScriptManager in the Master Page with my current test environment.
Avatar of samprg

ASKER

I put a jvascript in Page "Report" which called User control and I found it in page source , but still gives me same error
" This is no source code available for the curent location"

Do you have any solution?
ASKER CERTIFIED SOLUTION
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of samprg

ASKER

Can I call Javascript function in parent page from .cs ?
Avatar of samprg

ASKER

Thanks