LinkButton is not calling JavaScript function in OnClick? - c#

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

Related

Div onload function and embedding server code c#

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>

Messagebox pop-in in asp, javascript not Recognized?

I've got an issue with a messagebox user control. I wish for a control which can be given a message and the user can dismiss with a click of a button, which can be inserted into many places.
I have applied the javascript into the messagebox control in a hope i can keep everything to do with the messagebox centralized, however when browsing to a page with the messagebox control added i get this error:
CS1061: 'ASP.components_messagebox_ascx' does not contain a definition for 'HideBox' and no extension method 'HideBox' accepting a first argument of type 'ASP.components_messagebox_ascx' could be found
The control is as thus:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Messagebox.ascx.cs" Inherits="FosterNetwork.Components.Messagebox" %>
<script type="text/Javascript">
function HideBox() {
document.getElementById("PNL_Messagebox").setAttribute("visible", false);
}
</script>
<asp:Panel ID="PNL_Messagebox" runat="server">
<asp:Label ID="LBL_Message" runat="server" />
<asp:Button ID="BTN_Ok" Text="Ok" OnClick="HideBox()" runat="server" /> <!--Error happens on this line-->
</asp:Panel>
I'm fairly certain i've done this right but obviously i've done something wrong if it's not working. Any light on the situation at all would be grand.
Addendum: If i comment out the Button control the page loads fine, and the script loads fine too (Viewed page source)
The control ID's you're referencing are not the client ID's, but server ID's. So retrieve the 'ClientID' from the control in the JavaScript function and second, use the 'OnClientClick' property to show the JavaScript message.
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Messagebox.ascx.cs" Inherits="FosterNetwork.Components.Messagebox" %>
<script type="text/Javascript">
function HideBox() {
document.getElementById("<%= PNL_Messagebox.ClientID %>").setAttribute("visible", false);
}
</script>
<asp:Panel ID="PNL_Messagebox" runat="server">
<asp:Label ID="LBL_Message" runat="server" />
<asp:Button ID="BTN_Ok" Text="Ok" OnClientClick="HideBox()" runat="server" /> <!--Error happens on this line-->
</asp:Panel>
Onclick looks for a server side function, and not javascript. either, define your button as <input type='button' onclick='HideBox' or change the current code to:
<script type="text/Javascript">
function HideBox() {
document.getElementById("<%= PNL_Messagebox.ClientID %>").setAttribute("visible", false);
return false;
}
</script>
<asp:Button ID="BTN_Ok" Text="Ok" OnClientClick="return HideBox()" runat="server" />
returning false in OnClientClick, prevents the asp button from postback.
Edit: as Monty mentioned, your panel control's client id is not correctly set in your code.

Disable buttons onclick in asp wizard

I have an asp wizard on my page with three steps.
<asp:Wizard ID="wizardBestellen" runat="server" ActiveStepIndex="0" DisplaySideBar="False" onfinishbuttonclick="wizard_NextButtonClick" onnextbuttonclick="wizard_NextButtonClick" onpreviousbuttonclick="wizard_PreviousButtonClick">
<StartNavigationTemplate></StartNavigationTemplate>
<StepNavigationTemplate></StepNavigationTemplate>
<FinishNavigationTemplate></FinishNavigationTemplate>
<WizardSteps>
<asp:WizardStep runat="server" ID="step1" StepType="Start">
<uc1:control ID="uc1" runat="server" />
</asp:WizardStep>
<asp:WizardStep runat="server"ID="step2" StepType="Step">
<uc2:control ID="uc2" runat="server" />
</asp:WizardStep>
<asp:WizardStep ID="step3" runat="server" StepType="Finish">
<uc3:control ID="uc3" runat="server" />
</asp:WizardStep>
</WizardSteps>
</asp:Wizard>
Now every control has a next and a previous button which after the click validates your data and sends you to the next step. The buttons all look like:
<asp:LinkButton ID="bntPrevious" runat="server" CommandName="MovePrevious" CssClass="buttonOrange" CausesValidation="false"><span>Previous</span></asp:LinkButton>
<asp:LinkButton ID="btnNext" runat="server" CommandName="MoveNext" CssClass="buttonOrange" CausesValidation="true"><span>Next</span></asp:LinkButton>
So far it all works perfectly..
Now i wanted to disable the buttons after clicking on it and show a div with a loader image. So i created a div named divLoader and a javascript function which hides the div the buttons are in and shows the div with the loader.
function ToggleDiv(divButton, divLabel)
{
if (divButton.style.display == 'block')
{
divButton.style.display = 'none';
divLabel.style.display = 'block';
}
else
{
divButton.style.display = 'block';
divLabel.style.display = 'none';
}
}
But i can't get this to work. The ToggleDiv function works great in another situation, so thats not the problem.
I've tried calling the function from the OnClick attribute in the linkbutton but this gave me an error. I tried the OnClientClick, but this didn't work and i also tried setting the onclick attribute in the code behind, this also was a dead end.
Can anybody explain to me what i am doing wrong or is there another way to prevent users clicking the button twice?
Sounds like you're not getting the binding to work.
The first thing I would do is check the emitted control IDs by doing a view-source. Some implementation of the .NET framework add extra fluff to these control IDs, so can't guarantee that it will be the same as appears on the form.
Next, I would try some JavaScript late binding. If you have a JavaScript file, put it there. If not create one or add a JavaScript block to the foot of your form (create a new file for preference).
All this would be much easier with a JAvaScript lbrary such as jQuery, but for the moment lets assume you don't have one.
Add a window onload event handler
window.onload = function(){
//code to go here
}
Now add the click binding for your button:
window.onload = function(){
document.getElementById("***YOUR BUTTON ID***").onclick = function(){
ToggleDiv(this, document.getElementById("***YOUR LABEL ID***"));
}
}
I said this would be a little easer with jQuery, well, this is the equivalent implementation:
$(document).ready(function(){
$("#***YOUR BUTTON ID***").click(function(){
$(this).toggle();
$("#***YOUR LABEL ID***")).toggle();
});
});
The above sample removes the need for your ToggleDiv function entirely.

Syntax error in dynamic aspx Javascript function call?

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.

How to execute in JavaScript function in ASP.NET using C#

i have a textarea which i want to disable when I check a checkbox. I made a working demo in plain HTML and JavaScript , but When I am migrating to Asp.net, neither the function is being executed nor I can keep the OnSelect event in my check box.
<asp:CheckBox ID="ChkOthers" runat="server" Text="Others(Please Explain)" Width="205px" AutoPostBack="True" OnCheckedChanged="ChkOthers_CheckedChanged" />
<asp:TextBox ID="TxtOtherReason" runat="server" MaxLength="1024" TextMode="MultiLine" />
i want to create client side validation when check box is checked textbox is enable else disable.
I guess you problem is in the id of the controls so here is my solution for your problem
//Remove the AutoPostBack property and the OnCheckedChanged="ChkOthers_CheckedChanged"
<asp:CheckBox ID="ChkOthers" runat="server" Text="Others(Please Explain)" Width="205px" OnClick="javascript:YourFunction()" />
<asp:TextBox ID="TxtOtherReason" runat="server" MaxLength="1024" TextMode="MultiLine" />
and put the following javascript to handle the enable and diable of the textbox
<script>
function YourFunction() {
document.getElementById('<%= TxtOtherReason.ClientID %>').disabled = document.getElementById('<%= ChkOthers.ClientID %>').checked == false ? true : false;
}
</script>
It has been a while for me since I used ASP.NET but from memory there is something like an "OnClientClick" event and a validation property in asp.net properties for a control that you can use to call client side JavaScript code..
Sorry for the vague answer - perhaps someone can expand on it.
You are using a server side handling of the event. Use a client side handling with "OnClientClick". You can do something like:
<asp:CheckBox ID="ChkOthers" runat="server"
Text="Others(Please Explain)"
Width="205px"
AutoPostBack="True"
OnClientClick="alert(this.checked);"
OnCheckedChanged="ChkOthers_CheckedChanged" />
It seems this also does not work. I am yet to prove.
Also see a similar post on StackOverflow: OnClick vs OnClientClick for an asp:CheckBox? and there is also an issue in MS site: http://connect.microsoft.com/VisualStudio/feedback/details/103398/checkbox-onclientclick
you can put the code in cs files .after it loaded you can make the onclientclick method to an exist javascript function .
and in the javascript function you can get the text box use the name or the simple id which you can get from the source file the server gives to the browser!

Categories

Resources