Solved

Call Javascript from Server side

Posted on 2007-11-19
5
1,539 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

16 Experts available now in Live!

Get 1:1 Help Now