2

Hi i wanted to show or hide duplicate records according to query. So, I need to know how to call the javascript function from C# codebehind.

  <a onclick="Grid1.insertRecord(); return false;" id="a2"  href="javascript:">Save</a>

When I click save i need to show a popup which i have written in javascript.

 if (!exist)//exists is the query
        {
            System.Web.UI.Control my = FindControl("a2");
              a2.Attributes.Add("onclick", "retrun HideDuplicate()");

This line returns an error saying "a2 doesnot exist in current context."

slugster
  • 49,403
  • 14
  • 95
  • 145
Sayamima
  • 205
  • 1
  • 8
  • 24
  • Is this what you are looking for? http://stackoverflow.com/questions/1265887/call-javascript-function-on-hyperlink-click – paparush Mar 26 '11 at 23:16

3 Answers3

1

Why not use an asp.net LinkButton? It has a server side Click event and is accessible from c# code-behind.

Bala R
  • 107,317
  • 23
  • 199
  • 210
  • I think you meant OnClientClick event which would render out to a javascript onclick event? http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.onclientclick.aspx – rtpHarry Mar 26 '11 at 23:18
  • if you have to call something like `Grid1.insertRecord()`, should he be using the OnClick server side event handler ? – Bala R Mar 26 '11 at 23:21
  • But the problem is that I need to check a query before I go onto call the jascript – Sayamima Mar 26 '11 at 23:23
  • yes, I have a insert record function defined..... Now I need to check a query and then call the javascript accordingly – Sayamima Mar 26 '11 at 23:25
  • @Nishanth look into http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerclientscriptblock.aspx There are lots of examples out there. – Bala R Mar 26 '11 at 23:29
1

The basic <a> tag is not turned into a control by asp.net unless you add a runat="server" to it. Its then turned into a HtmlGenericControl.

<a onclick="Grid1.insertRecord(); return false;" id="a2" href="#" runat="server">Save</a>

This might work for you - its not clear if you have more than one of these links on the page (such as in a row of a gridview?) or if its just on there once.

Also the way you have used javascript is not following best practices but thats a discussion for another day :)

rtpHarry
  • 13,019
  • 4
  • 43
  • 64
  • I have it in a row. However, the runat doesn't solve the problem – Sayamima Mar 26 '11 at 23:16
  • no this wont solve your problem in a row. You need to handle the `RowCreated` event of the `GridView` control and then use `e.Row.FindControl()` – rtpHarry Mar 26 '11 at 23:22
1

MSDN documentation for programatic creation of client side callbacks without postback with an example where the code behind is in C# might give a good overview of how it is supposed to work in general.

In your case, the corresponding code-behind should implement the interface 'ICallbackEventHandler' and the two methods it describes. In addition, you would need two more client side Javascript functions to prepare and handle the callback, besides the executor/invoker (in your case, a 'save' method). However one of the additional two Javascript functions could be registered in the codebehind, as the example shows.

JustinC
  • 416
  • 5
  • 9