0

I currently have a Gridview with a itemTemplate which acts as a link which calls a javascript function:

 <ItemTemplate>
            <a href="javascript:;" onclick="simpleCart.add( 'name=<%# Eval("Name") %>' , 'price=<%# Eval("Price") %>' , 'quantity=1' );">Add To Cart</a>
            </ItemTemplate>

I need to add some functionality which I can only do in C#, so I was thinking of replacing the link with a button and when the button is clicked it calls both the C# function and the javascript function (they don't need to interact/share any data). Is this even possible?

2
  • When do you need to call the javascript function? Before or after the server side? Commented Dec 3, 2011 at 11:12
  • Doesn't really make a difference in this case Commented Dec 3, 2011 at 11:21

3 Answers 3

1

You could use a LinkButton and set the OnClientClick property to execute a javascript function before going to the server. You could even cancel the server call if you return false from this function:

<asp:LinkButton 
    id="LinkButton1"
    text="Add To Cart"
    OnClientClick='<%# string.Format("&quot;name={0}&quot;, &quot;price={1}&quot;, &quot;quantity=1&quot;", Eval("Name"), Eval("Price")) %>'
    OnClick="LinkButton1_Click"
    runat="Server" />
Sign up to request clarification or add additional context in comments.

1 Comment

I think that's how I have to do it, but now I'm getting the error "The server tag is not well formed." The problem is in the javascript part (onClientClick), I just copied how it was in my original post. p.s. I apologise for taking so long to reply, went away for the rest of the weekend
1

You can use OnClientClick properyy of LinkButton in order to execute client script and OnClick event for server code

Here is code example and more detailed description http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.onclientclick.aspx

Comments

0

Yes, it's possible.

It sounds like you want to avoid a full-page postback, so you could call the server-side code either with an ASP.NET Callback or with a more vanilla Ajax call, perhaps using jQuery. (I'm assuming your C# code is on the server, rather than in a local Silverlight app).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.