views:

365

answers:

5

I have an LinkButton control which I need to add a c# method to. Currently when I use parse control and add the control to the page it changes the call to javascript and I get undefined. I figure a way around this would be to define a javascript function that would call the c# method from the code behind, but I cannot figure out how to do this.

Specfically when a linkbutton is clicked I need javascript to pass the ID of the linkbutton to a C# method.

I have tried this:

foreach (Control Control in myctrl.Controls)
           {
              if (Control is LinkButton)
                   {
                      LinkButton lb = (LinkButton)Control;
                      lb.Click += LinkButton_Click;

                    }
              Panel1.Controls.Add(myctrl);
           } 



public void LinkButton_Click(Object sender, EventArgs e)
        {
   BREAKPOINTHERE>         Console.Write(e.ToString());

        }

It never fires when clicked. here is the code generated:

<a id="dnn_ctr954_ViewPromotions_LinkButton2" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;dnn$ctr954$ViewPromotions$LinkButton2&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true))">Get your Free 2</a>

now if I put this in my aspx page, it works fine:

 <asp:LinkButton ID="test" OnClick="LinkButton_Click" runat="server">mybutton</asp:LinkButton>

I get this and it works fine:

<a id="dnn_ctr954_ViewPromotions_test" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;dnn$ctr954$ViewPromotions$test&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true))">mybutton</a>
+7  A: 

You can't do this per-se. Javascript exists on the client. C# code exists on the server. They can't possibly have any contextual knowledge of each other. Server code runs in response to an HTTP request.

That being said, you can create a web service method in C# and invoke it using javascript. Judging from the context of your question though, it may not do what you would like. Web service methods are static and don't have the same context as a regular postback.

However, you can check out a few articles like this one: Client-Side Web Service Calls with Ajax Extensions

Edit: With your edit, it sounds like you just want to make your button postback to the server. This is the core functionality of ASP.Net, and is easy to do. As other answers suggest, you just need to hook up a click event handler in your server code.

womp
no, it's a bit more involved as I am looking for a quick work around to an issue I am having, see my other question : http://stackoverflow.com/questions/2289831/parsecontrol-in-dotnetnuke-asp-net-changing-onclick-to-javascript-instead-of-c-m
James Campbell
+4  A: 

You could call a WebService or PageMethod from javascript written in C#.

Update: Since your using a LinkButton, you could simply define a click handler in your codebehind:

button.Click += button_Click
jrummell
Maybe I'm missing something in the question, but using the built in click handler seems like the easiest, best thing to do. Good catch. +1
Greg
I missed the LinkButton part when I first answered. But yes, I think what he's looking for is simply a click handler.
jrummell
I tried this first beofre my post, and this is what is causing my issue, as I am using DotNetNuke, please see other question as to why I am at this point: http://stackoverflow.com/questions/2289831/parsecontrol-in-dotnetnuke-asp-net-changing-onclick-to-javascript-instead-of-c-m
James Campbell
Glad I could help.
jrummell
A: 

You're looking for WebMethods, there's an example with jquery here, but can be adapted to basic javascript easily. The good sauce is on the "PageMethods.SomeMethod" part.

F.Aquino
tried this and it won't fire the function.
James Campbell
+1  A: 
Mark Schultheiss
This is far from easy:-) But yeah don't seem to work right.
James Campbell
A: 

In my company's toolkit, we implemented a technology we call remote invoke. This consists of two parts - on the client side, there is a javascript method called RemoteInvoke() which takes a method name and an array of arguments to pass to the method. On the server side, you decorate a public method you would like to call with the attribute [RemoteInvokable]. RemoteInvoke matches signature as best as it can (type casting if needed) and then called the appropriate public method (if it found one).

It's not for things as simple as click actions, but it if you need more complicated things, it will work just great.

plinth
Wow this is neat!!! Thanks for info!!!
James Campbell