Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Call Javascript from Server side

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Moving code from one TFS branch to another 3 33
Reading the web config for a running service on Windows 10 16 49
Save ms data to server side. 19 55
SSRS Deployment problem 5 65
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

856 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