Pages

Friday, 18 December 2015

Calling a client side script form server side in asp.net

Problem 8:

 In my asp.net website I have been in a situation where I have multiple forms on my page and some other content as well.
I need to move to the top of the page on successful submission. But obviously successful submission is determined at a server side event.

Solution 8:

So for that purpose I have created following function. which uses 'Page.ClientScript.RegisterStartupScript' Method which actually Registers the startup script with the Page object using a type, a key, a script literal, and a Boolean value indicating whether to add script tags.


private void ScrollToTop()
{
 
StringBuilder strScript = new StringBuilder();
  strScript.Append("$('html, body').animate({ scrollTop: 0 }, 800);");
  Page.ClientScript.RegisterStartupScript(this.GetType(),"Script", strScript.ToString(), true);

} 

And then I have just simply call this function on my button click event when the condition meets. as shown

 
protected void btnSubmit_Click(object sender, EventArgs e)
{
      bool isSubmitted= true;

     //You may write some submission logic here
      if(isSubmitted)   
         ScrollToTop();
}

 

And that did the trick for me.

want to achieve this functionality in asp.net MVC

2 comments:

  1. Given so much information in it. its very useful .perfect explanation about Dot net framework.Thanks for your valuable information. dot net training in chennai | best dot net training in chennai

    ReplyDelete
  2. Thank you for your guide to with upgrade information.
    Dot Net Online Training Hyderabad

    ReplyDelete