Solved

Call Javascript from Server side

Posted on 2007-11-19
5
1,538 Views
Last Modified: 2012-08-14
Greetings

Noit to long ago "CodeClay" very kindly helped me with some JavaScripting and explained how the ASP.Net code executes on Server side, while the JavaSCript does it's thing on Client side (I hope that I've said thgis correctly). That explain why in a procedure with several lines of code, followed by JS call, I never saw the JS (in this a messagebox) executed - you'd have to move the JS to Server-side (again, I hoep thatI'm saying this correctly!).

I now have a need for such an event - where after an insertion I want to display a message.

WHAT CAN I DO TO GET THIS TO WORK ?

My Code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            // Set up event handlers for local buttons.
            btnLocalInsert = (Button)this.Master.FindControl("btnInsert");
            btnLocalInsert.Click += new EventHandler(btnLocalInsert_Click);
        }
    }

    protected void btnLocalInsert_Click(object sender, EventArgs e)
    {
       ..............................................
       ........................................
       ......................................................
       .....................................
        Alert.Show("Your password will be emailed to you!");       // Alert is a simple class that contains
    }                                                                                                // teh code for the JS scripting

0
Comment
Question by:allanmark
  • 2
  • 2
5 Comments
 

Author Comment

by:allanmark
ID: 20317716
Sorry ...

Just had another thought, re-lloking at my code.The lines below - coudl they not be placed under the
PageInit?

            btnLocalInsert = (Button)this.Master.FindControl("btnInsert");
            btnLocalInsert.Click += new EventHandler(btnLocalInsert_Click);
0
 
LVL 5

Expert Comment

by:gocemi
ID: 20318096
Hi,

Thing is frend that you can register javascript from the server side to be executed on a client side.
This is done trough Page.RegisterClientScriptBlock("id of the script","<script>alert('here content')</script>");
After page is finished loading, on render will put this script in the document so it'll be executed as soon as document is loaded.

Personally, i would recommend for you to use label with same text and hide/ show depending on whether button is clicked on not.

But anyway Page.RegisterClientScriptBlock will do your job.

Br,
0
 
LVL 9

Expert Comment

by:codeclay
ID: 20318663
Hello Allan,

Your code will work fine .If its the last statement in the btnLocalInsert_Click event .

Thanks.
Aby.
0
 

Author Comment

by:allanmark
ID: 20322340
Thanks, both of you for the speedy replies.

I fiddled around and guess what was giving me the ehadache ....

   if (!Page.IsPostBack)  .....  I REMOVED THIS AND IT WORKS!!!!

Once again, I'm going to eb really cheeky and slip anothe rquestion in, but coiuld you point me in the direction of some decent literature regarding POSTBACK and PAGES  ... quite obvious that I need some :(  :(.
0
 
LVL 9

Accepted Solution

by:
codeclay earned 375 total points
ID: 20325786
Here is acloser look at the POstBack feature in ASP.Net http://www.xefteri.com/articles/show.cfm?id=18
And this will be a good place to start learning ASP.NET .
http://www.asp.net
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now