Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call Jquery function from C#

Tags:

jquery

c#

asp.net

I would like to fadein and out a div after performing some code. This JQuery function is working fine when using the button's onclientclick property. But, Unable to call it from c#.

JS:

function Confirm()
{
 $(".saved").fadeIn(500).fadeOut(500);
}

C#:

protected void btnsave_click(object sender, Eventargs e)
{
  //some code
  upd.update();
  ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Confirm", 
   "<script type='text/javascript'>Confirm();</script>", true);
}

HTML:

<asp:updatepanel id="upd" runat="server" updatemode="conditional">
 <triggers>
  <asp:asyncpostbacktrigger controlid="btnsave" eventname="click"/>
 </triggers>
 <contenttemplate>
  <div style="position:relative">
    <span class="saved">Record Saved</span>
    <asp:gridview....../>
  </div>
  <asp:button id="btnsave" runat="server" text="save" onclick="btnsave_click"/>
 </contenttemplate>
</asp:updatepanel>

CSS:

.saved
{
  position:absolute;
  background:green;
  margin:0 auto;
  width:100px;
  height:30px;
  visibility:hidden;
}
like image 342
Ruby Avatar asked Mar 06 '14 05:03

Ruby


People also ask

How do you call a function in jQuery?

How to Call a Function Using jQuery. After you declare function, you need to call it in the place where you want to execute that piece of code. You need to first find at which event you want to execute the code. Here, you have to create a function and call that function when someone clicks on the button.

How to call a JavaScript library function from a jQuery script?

Calling a JavaScript library function is quite easy. You need to use the script tag. As almost everything we do when using jQuery reads or manipulates the document object model (DOM), we need to make sure that we start adding events etc. as soon as the DOM is ready.

How to run an AJAX call using jQuery?

JQueryAjaxCall.zip I will use the jQuery.ajax () or $.ajax () method to call the C# method (WebMethod). The $.ajax () method does an asynchronous HTTP (Ajax) request. This method will take various parameters. You need add the jQuery library to you project in order to run this jQuery Ajax call. Here is the jQuery Script. Here is the C# function.

How to declare a function in jQuery?

To declare a function, you have to use the simple syntax given below. Here, you need to change the function name with the name of the function you want to create. Inside that function, put the jQuery code you want to execute when you call the created function. jQuery. <script> function FunctionName () { //Enter your code here to execute on call;


1 Answers

C#

protected void btn_click(object sender, Eventargs e)
{
    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "script", "Confirm();", true);
}

JS:

<script>
   function Confirm()
   {
      $(".saved").fadeIn(500).fadeOut(500);
   }
</script>

Edit Improve Code

C#

protected void btn_click(object sender, Eventargs e)
{
    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "script", "Confirm();", true);
}

JS:

<script>
    $(document).ready(function () {

       function Confirm() {
           $(".saved").fadeIn(500).fadeOut(500);
       };
    });
</script>
like image 140
Jaykumar Patel Avatar answered Sep 26 '22 05:09

Jaykumar Patel