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.NewLin e;
Page.ClientScript.Register ClientScri ptBlock(th is.GetType (), _jFunction1, js1, true);
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);
Page.ClientScript.Register
what about simply echoing the script in the right place of the generated html, between script tag?
ASKER
I did not understand,
ASKER
Do I need " <script type="text/javascript"> "
ASKER
I added script tag, same issue
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.Register ClientScri ptBlock(th is.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.Register ClientScri ptBlock(th is.GetType (), "_jFunction1", js1, true);
string js1 = "function FN(list)" +
"{var selectedType = GetSelectedItems(list);" +
"<%= Select.ClientObjectId %>.Callback(selectedType);
Page.ClientScript.Register
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);}"
Page.ClientScript.Register
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
_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(sele ctedType); }" + System.Environment.NewLine ;
Page.ClientScript.Register ClientScri ptBlock(th is.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);Cli entObjId.C allback(se lectedType );}
//]]>
</script>
string _jFunction1 = "_jfunction1";
string js1 = "function FN(list)" +
"{var selectedType = GetSelectedItems(list);" +
"ClientObjId.Callback(sele
Page.ClientScript.Register
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);Cli
//]]>
</script>
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.LoadC ontrol("Un iMenuContr ol.ascx");
placeHolder1.Controls.Add( uc);
}
Here's my Reference directive on the aspx web form:
<%@ Reference Control="UniMenuControl.as cx" %>
My PlaceHolder from the aspx page markup:
<asp:PlaceHolder ID="placeHolder1" runat="server"></asp:Place Holder>
All the markup on my User Control:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UniMenuControl .ascx.cs" Inherits="testApp1.UniMenu Control" ClassName="UniMenuControl" %>
<div id="testDiv" style="width:100px;height: 100px;back ground-col or:red">te st</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(sele ctedType); }" + System.Environment.NewLine ;
Page.ClientScript.Register ClientScri ptBlock(th is.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);Cli entObjId.C allback(se lectedType );}
//]]>
</script>
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.LoadC
placeHolder1.Controls.Add(
}
Here's my Reference directive on the aspx web form:
<%@ Reference Control="UniMenuControl.as
My PlaceHolder from the aspx page markup:
<asp:PlaceHolder ID="placeHolder1" runat="server"></asp:Place
All the markup on my User Control:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UniMenuControl
<div id="testDiv" style="width:100px;height:
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(sele
Page.ClientScript.Register
}
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);Cli
//]]>
</script>
ASKER
I did not understand this
Inherits="testApp1.UniMenuControl"
Inherits="testApp1.UniMenuControl"
testApp1 is the name of the application I use for testing. Yours will be different.
ASKER
I have same what you did but it does not work,
Does ajax ScriptManager in masterpage prevent to inject javascript in user control?
Does ajax ScriptManager in masterpage prevent to inject javascript in user control?
What is the value of jFunction1?
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.
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?
" This is no source code available for the curent location"
Do you have any solution?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Can I call Javascript function in parent page from .cs ?
ASKER
Thanks