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).
Related
I would like to add a textbox value to my url so I am able to click on a button and open a clients record on the button click. I have tried the below as well as other methods but I am unable to get this working (the below is using a session but I have the client ID value stored in a textbox). Is there a way to do this please?
<asp:Hyperlink runat="server" NavigateUrl='<%# Eval("Client_ID","~/ViewCustomers.aspx?id={0}") %>' />
Result should be ViewCustomers.aspx?id=2 for example.
I am using ASP.NET C# and using HTML 5 for the front end development.
Any help is greatly appreciated.
You can give the HyperLink a class and bind a javascript function to it.
<asp:HyperLink runat="server" CssClass="LinkWithID" NavigateUrl='<%# Eval("ride_id","~/ViewCustomers.aspx?id=") %>' />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
Then simply append the value of the TextBox to the url
<script type="text/javascript">
$('.LinkWithID').click(function () {
location.href = $(this).attr('href') + $(this).next('input').val();
return false;
});
</script>
My aspx page:
<asp:TableCell>
<asp:TextBox ID="tbFirstName" ClientIDMode="Static" runat="server" onblur="javascript:CheckFirstNameInput(this)">
</asp:TextBox>
</asp:TableCell>
My C# class
[ScriptService()]
public static class InputChecks
{
[WebMethod()]
[ScriptMethod()]
public static bool CheckForLettersSpacesStripes(string input)
{ //SomeCode }}
My masterpage
<asp:ScriptManager runat="server" ID="smScriptManager" EnablePageMethods="true">
<Scripts>
<!-- And some standard imported scripts -->
<asp:ScriptReference Path="~/Resources/JavaScriptFunctions.js" />
</Scripts>
</asp:ScriptManager>
As you might notice, the idea is that I import a javascript here, which works fine. Tested it out with an alert and several console.logs and stuff. But I can't reach the C# methods, I used several stuff. I used an ajax call:
function CheckFirstNameInput(InputElement)
{
var Input = InputElement.value;
console.log(Input);
$.ajax({
url: "Test",
data: "{input : " + Input + "}",
success: function(response)
{
alert(response.url);
}
});
}
But also I just used PageMethods. None of the above worked, I tried placing the scriptmanager in the aspx page instead of the masterpages, didn't work out. I am completely out of ideas, and I am not finding any new stuff on the net. So, is there anyone who knows the answer, I would be eternally gratefull for the answer which releases me from this annoying burden.
You need to change the URL
change url: "Test", to url: "Application Path /CheckForLettersSpacesStripes",
or try another one way by using PageMethods
You don't need any lines , just try this code instead of your code
PageMethods.CheckForLettersSpacesStripes(input);
referred from
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'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
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.