5

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;
}
1

3 Answers 3

8

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. That worked only when the css visibility is 'visible'. I would like the span to stay hidden. On button's click, it should fade in and out.
Ok Figured that out...$(".saved").css("visibility","visible").fadeIn(500).fadeOut(500);
2

When using asynchronous post backs is good to understand the events that you can actually handle on the client side (aspx file).

If you want to execute some client function (jquery, javascript) after an asynchronous post back has been made, I suggest using the pageLoaded event on the client side. So your javascript will be something like the following:

function pageLoad(sender, args) { 
    $(".saved").fadeIn(500).fadeOut(500);
}

For more information please refer to this MSDN page on the Event Order for Common Scenarios section.

Hope this helps


Edit after comment to add filter example

To filter logic in the pageLoad event depending on the element that raised the event, as stated in this MSDN page, on the Animating Panels section, you can do something like this:

var postbackElement;

function beginRequest(sender, args) {
    postbackElement = args.get_postBackElement();
}
function pageLoaded(sender, args) {
    if (typeof(postbackElement) === "undefined") {
        return;
    } 
    else if (postbackElement.id.toLowerCase().indexOf('btnsave') > -1) {
        $(".saved").fadeIn(500).fadeOut(500);
    }
}

2 Comments

There are other postback events too that do not require the span to fadein. How can I filter then.
There is an example for that in this page msdn.microsoft.com/en-us/library/bb398976.aspx , on the Animating Panel section... the main thing is to use the beginRequest event to store in a variable the element that is raising the postback and using that variable to filter the pageLoad event code.
0

Try this...

ClientScript.RegisterClientScriptBlock(this.GetType(), "script", "Confirm();", true);

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.