[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 361
  • Last Modified:

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);
0
samprg
Asked:
samprg
  • 11
  • 7
1 Solution
 
ienaxxxCommented:
what about simply echoing the script in the right place of the generated html, between script tag?
0
 
samprgAuthor Commented:
I did not understand,
0
 
samprgAuthor Commented:
Do I need     " <script type="text/javascript"> "
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
samprgAuthor Commented:
I added script tag, same issue
0
 
Tom BeckCommented:
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);
0
 
samprgAuthor Commented:
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
0
 
Tom BeckCommented:
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>
0
 
samprgAuthor Commented:
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
0
 
Tom BeckCommented:
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>
0
 
samprgAuthor Commented:
I did not understand this
Inherits="testApp1.UniMenuControl"
0
 
Tom BeckCommented:
testApp1 is the name of the application I use for testing. Yours will be different.
0
 
samprgAuthor Commented:
I have same what you did but it does not work,
Does ajax ScriptManager in masterpage  prevent to inject javascript in user control?
0
 
Tom BeckCommented:
What is the value of jFunction1?
0
 
samprgAuthor Commented:
the value is "FN"
0
 
Tom BeckCommented:
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.
0
 
samprgAuthor Commented:
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?
0
 
Tom BeckCommented:
That error is a problem that I believe is unrelated to the inject javascript problem. Your .dll files are out of sync

Look here: http://social.msdn.microsoft.com/Forums/en-US/csharpide/thread/0405312e-1769-4618-99eb-df82e13479fb/
0
 
samprgAuthor Commented:
Can I call Javascript function in parent page from .cs ?
0
 
samprgAuthor Commented:
Thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 11
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now