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;
}
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.
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.
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.
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;
C#
protected void btn_click(object sender, Eventargs e)
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "script", "Confirm();", true);
}
<script>
function Confirm()
{
$(".saved").fadeIn(500).fadeOut(500);
}
</script>
C#
protected void btn_click(object sender, Eventargs e)
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "script", "Confirm();", true);
}
<script>
$(document).ready(function () {
function Confirm() {
$(".saved").fadeIn(500).fadeOut(500);
};
});
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With