Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,
I want to use ASP.NET LinkButton with JavaSript,

i am using Client CallBack without postback,

my javascript is:

<<pre lang="xml">script type="text/javascript">
function ReceiveBikeColor(colorFile)
{
    // any code here
}



<pre lang="cs">public partial class About : System.Web.UI.Page, ICallbackEventHandler
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string cbRef = ClientScript.GetCallbackEventReference(this, "arg", "ReceiveBikeColor", "context");
        string cbScript = "function GetBikeColor(arg, context) {" + cbRef + "; }";
        ClientScript.RegisterClientScriptBlock(this.GetType(), "GetBikeColor", cbScript, true);
    }

    public string GetCallbackResult()
    {
        //////////
    }
    public void RaiseCallbackEvent(string eventArgument)
    {
        ///////
    }
}



but when assign onclick attribute to Javascript method like that:

<asp:LinkButton id="lbtnAddToFavorites" runat="server"  OnClick="GetBikeColor()" >Add to favorites</asp:LinkButton>


it tell me that:

Error 'ASP.specifictopic_aspx' does not contain a definition for 'GetBikeColor'

and when use OnClientClick attribute it casue Postpack

<asp:LinkButton id="lbtnAddToFavorites" runat="server"  OnClientClick="GetBikeColor()" >Add to favorites</asp:LinkButton>
Posted
Updated 26-Jul-11 3:18am
v3
Comments
_Zorro_ 26-Jul-11 9:05am    
You should check the option 'Display a notification about every script error' if you're using Internet Explorer, just in case.

Are you sure you declared the javascript function GetBikeColor?

I tried to reproduce, and had no problems.

try that way

function GetBikeColor()
{
//Some Implementation
return false;
}

<asp:LinkButton id="lbtnAddToFavorites" runat="server"  OnClientClick="return GetBikeColor();" >Add to favorites</asp:LinkButton>
 
Share this answer
 
Comments
DominicZA 26-Jul-11 9:35am    
Correct!
If you're trying to call a js function when someone clicks the LinkButton, OnClientClick is the way to go.

Make sure you defined the function GetBikeColor and that it's accessible.

LinkButton.OnClientClick Property [^]

LinkButton.OnClick Method [^]

Hope this helps!
 
Share this answer
 
Hi,
is this correct?,use onclientclick to call javascript.
try once again.best of luck.
regards,
shefeek
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900