I'm writing a bit of aspx code to call a JavaScript function from a server control. Here's the JavaScript:
<script type="text/javascript">
function checkInfoPortalUnpin(portalareaid) {
if(portalareaid == 1) {
alert('a message');
}
}
</script>
And here's the calling aspx code:
<asp:ImageButton OtherFields="omitted..."
OnClientClick='checkInfoPortalUnpin(<%# Eval("PortalAreaID") %>);' />
When I view the source of that line in the browser (IE8) it is rendering as this:
... onclick="checkInfoPortalUnpin(<%# Eval("PortalAreaID") %>);"
and I get a syntax error when I click on the ImageButton. I know the OnClientClick does work because if I replace the <%# ... %> with a hard-coded '1' the function runs fine. Am I missing something?
Use this :
... OnClientClick="checkInfoPortalUnpin('<%# Eval(\"PortalAreaID\") %>');" ...
Just use:
<asp:ImageButton runat="server" OnClientClick="checkInfoPortalUnpin('<%# Eval(\"PortalAreaID\") %>');" />
You forget to put single quote outside the server tag.
Related
How to get value from javascript to C# code behind ? I have an idea with the code below. In javascript code I want to assign the value of "HiddenField" Control with string value and then I want to take this value from "HiddenField" in code behind. But with this code I cannot do it. Can you tell me how to make it ?
<script>
$(function () {
document.getElementById('HiddenField').value = "active";
console.log(<%= this.HiddenField.Value %>)
});
</script>
<asp:HiddenField ID="HiddenField" runat="server" Value="5" Visible="true" />
you need to use ClientID property of control to get actual element ID in DOM.
<script>
$(function () {
document.getElementById('<%= HiddenField.ClientID%>').value = "active";
console.log(document.getElementById('<%= HiddenField.ClientID%>').value)
});
</script>
<asp:HiddenField ID="HiddenField" runat="server" Value="5" Visible="true" />
Use the Control ID for HTML markup that is generated by ASP.NET.
document.getElementById('<%= HiddenField.ClientID%>').value = "active";
When a Web server control is rendered as an HTML element, the id
attribute of the HTML element is set to the value of the ClientID
property. The ClientID value is often used to access the HTML element
in client script by using the document.getElementById method.
Then send the Hidden value through the javascript function as a variable while calling the controller
Surely works,
Cheers
Phani*
you may take a look at mshtml
As far as I know you call with this C# functions from your javascript code ;-)
I am trying to call c# properties in .aspx page, where myfunc is jquery function.
this function take parameters which i am passing through c# public properties. and I want to call this function as soon as div is rendered.previously i have done this on prerender event in ascx control. wha is exact ssyntax for below code and calling jquery function from here is valid?
<div class ="v" onload ="Javascript:myfunc('<%= this.articleID %> '
,'" +<%=this.UserID %>+"','" +<%=this.EncryptedUserID %>" +','" +<%#
this.ItemIndex %>)">
thanks
Edited
Ok If i do like this way
<div class ="v" >
<script language ="javascript" >
JGetTotalVotes(<%= this.articleid %> ,<%
this.ThisEncryptedUserID %>,<% this.ItemIndex %>,'aaa');
</script>
I do not want server side hidden fields solutions and if i use hiden field ( non server html tag) then i still need to call <%%> to fetch value from server side to hiddent field
I see you are missing single quote after the last parameter if that is not the problem.
You can use hidden fields and call the function in javascript
Add this to your page
<asp:HiddenField runat="server" ID="hdnArticleId" ClientIDMode="Static" />
<asp:HiddenField runat="server" ID="hdnUserId" ClientIDMode="Static"/>
<asp:HiddenField runat="server" ID="hdnEncryptedUserID" ClientIDMode="Static"/>
<asp:HiddenField runat="server" ID="hdnItemIndex" ClientIDMode="Static"/>
bind those hiddenfields in Page_Load event remember to add ClientIDMode="Static" to set the id as you set not a generated one by asp.net to have the ability to use them in javascript
Add To your javascritp
function YourFunction(){
var ArticleId = document.getElementById("hdnArticleId").value;
var UserId= document.getElementById("hdnUserId").value;
var EncryptedUserID= document.getElementById("hdnEncryptedUserID").value;
var ItemIndex= document.getElementById("hdnItemIndex").value;
// your code goes here
}
and don't forget to call your javascript function in page onload
Div elements don't pull in external content, so they don't have load events.
If you want to run some JS after a div element has been parsed:
<div></div>
<script> foo(); </script>
This is my JavaScript:
$(document).ready(function () {
function ShowHelp() {
window.open('../WebHelp/' + '<%= SessionManager.CurrentDictionaryId %>' + '/mweb.htm#cshelp/assetsdetail.htm', '', 'toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes');
}
function RunPrint(values) {
window.open('../Reports/Assets/AssetProfile.aspx?id=' + values, '', 'toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes');
}
});
And I have two <asp:Hyperlink> tags set like this:
<asp:HyperLink ID="lnkHelp" runat="server" EnableViewState="False" ImageUrl="~/Images/Help.png"
NavigateUrl="javascript:ShowHelp();" />
<asp:HyperLink ID="lnkPrint" ImageUrl="~/Images/PrintMed.png" runat="server" EnableViewState="false"
NavigateUrl="javascript:GetSelectedToPrint();" />
When running the debugger tools in Chrome I get the message:
"Uncaught ReferenceError: ShowHelp is not defined"
"(anonymous function)"
I am fairly ignorant in javascript, but I feel like I know enough to read what is there and i don't see the issue.
did you try to remove the $(document).ready(function(){ ?
that code is not needed if you are just wanting to create a function that will be fired after the controls were loaded (which is in your case).
<% string s=LinkButton_ID.Text; %>
<asp:LinkButton ID="LinkButton_ID" runat="server" Text="LinkButton_Text" onclientclick="JscriptFunction(...)">
</asp:LinkButton>
I want to send the "s" as parameter to JscriptFunction. I try JscriptFunction(<% s %>); but doesnt work. Do you have any suggestions? thank you
Try this:
JscriptFunction('<%= s %>');
Don't forget to quote the text value in your JS function call.
EDIT:
I didn't notice that your call to JscriptFunction is inside your server tag. If you don't have access to jQuery, the easiest way to pass the client-side text of the button is probably this:
onclientclick="JscriptFunction(this.innerHTML)"
Here's an example working on jsFiddle.
You can also pass the client-side ID of the control and manipulate the value using JavaScript. Make sure you have ClientIDMode="Predictable" or ClientIDMode="Static" set on the control if you do it this way.
You'll need to declare your code section to be javascript, and then you need to output the asp.net/c# value as a string. The easiest way is something like:
<script type="text/javascript">
var s = <% Response.write(LinkButton_ID.Text); %>;
//do your other javascript stuff
</script>
or
<script type="text/javascript">
var s = <%= LinkButton_ID.Text %>;
//do your other javascript stuff
</script>
If you needed to do more logic around this, you could also stick a literal control there and output to it in the code-behind:
<script type="text/javascript">
var s = <asp:literal id="litMyValue" runat="server">;
//do your other javascript stuff
</script>
Simple way u can pass/ instead you can use it in the function itself
like
<script>
function JscriptFunction()
{
var s = <%= LinkButton_ID.Text %>;
// do what you want here.
}
</script>
<asp:LinkButton ID="LinkButton_ID" runat="server" Text="LinkButton_Text" onclientclick="JscriptFunction();"></asp:LinkButton>
I'm trying to call a jquery function from a asp link button. Here is my link button html:
<div style="padding-left:75px">
<asp:LinkButton ID="lbAddCC" runat="server" ClientIDMode="Static" OnClick="ShowCCControls()" Text="Add CC"></asp:LinkButton>
</div>
Here is my jquery function:
function ShowCCControls() {
$('#lblCC').show();
$('#txtCC').show();
} //end ShowCCControls()
When I try to build, I get the error:
ASP.internal_email_aspx does not contain a definition for 'ShowCCControls' and no extension method 'ShowCCControls' accepting a first argument of type 'ASP.internal_email_aspx' could be found (are you missing a using directive or an assembly reference?)
I have this working on another page using a check box:
<asp:CheckBox ID="chkNew" TabIndex="8" runat="server" Text="New Tank" OnClick="SetNewTankControls()"
ClientIDMode="Static" />
Anybody see an issue? Thanks
Here is all of the javascript:
<script type="text/javascript" language="javascript">
//document.ready is used for jquery, waits until the doc is ready to be manipulated
$(document).ready(function () {
HideControls();
}); //end document.ready
function HideControls() {
$('#lblCC').hide();
$('#txtCC').hide();
$('#lblBCC').hide();
$('#txtBCC').hide();
} //end HideControls()
function ShowBCCControls() {
$('#lblBCC').show();
$('#txtBCC').show();
} //end ShowBCCControls
function ShowCCControls() {
$('#lblCC').show();
$('#txtCC').show();
} //end ShowCCControls()
OnClick is for specifying handlers in code-behind. If you want to specify a javascript function, you should use OnClientClick attribute.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.onclientclick(v=vs.80).aspx
<div style="padding-left:75px">
<asp:LinkButton ID="lbAddCC" runat="server" ClientIDMode="Static" OnClientClick="ShowCCControls()" Text="Add CC"></asp:LinkButton>
</div>
You could just set the handler in your client script like this:
$('#lbAddCC').click(function() {
$('#lblCC').show();
$('#txtCC').show();
});
Since you're not intending to perform server side behaviours with this click event, there isn't any need to define a handler on the server control and then have it render out a call to the client side function when you could just call it directly.
EDIT:
You will of course need to couple the client side script with the removal of the erroneous OnClick handler on the server control as below:
<asp:LinkButton ID="lbAddCC" runat="server" ClientIDMode="Static" Text="Add CC"></asp:LinkButton>
it could be that the page load the function call before loading jquery code, make sure you put the jquery before the link button and reference jquery liabrary