javascript via button click event in code behind


I'm trying to open a new window on a click event.

If I use the attributes.add in the Page_Load, then pop-up window gets hit before an update occurs...hence, the pop-up window is just a little too early.

I'm trying this in my button click event, but it's not bringing up the new window:

Response.Write("<script language=""javascript"">'UpdateBudgetConfirm.aspx?UBID=" + strSAID + "','','width=500,height=320,left=200,top=200');"")</script>")


4/1/2008 5:27:19 PM 24353 articles. 2 followers. Follow

17 Replies

Similar Articles

[PageSpeed] 44
Get it on Google Play
Get it on Apple App Store

You wanna use Page.ClientScript.RegisterStartupScript.  This is the solution. 

4/1/2008 5:37:55 PM

Add a StartupScript something like this: 

protected void Button1_Click(object sender, EventArgs e)

Thanks, Ed

Microsoft MVP - ASP/ASP.NET

4/1/2008 5:40:28 PM


Thanks for the replay, I'm using VB, but this is not liking the Page.ClientScript part...saying 'Client Script' is not a member of '':

Page.ClientScript.RegisterStartupScript(Me.GetType(), "Window", "'UpdateBudgetConfirm.aspx?UBID=" + strSAID + "','','width=500,height=320,left=200,top=200');", True)

4/1/2008 5:52:46 PM

I got this to compile correctly, but it's not firing:

  Page.RegisterStartupScript("Window", "'UpdateBudgetConfirm.aspx?UBID=" + strSAID + "','','width=500,height=320,left=200,top=200');")


4/1/2008 6:02:16 PM

Are you using at least the 2.0 Framework?

Thanks, Ed

Microsoft MVP - ASP/ASP.NET

4/1/2008 6:03:58 PM


Page.RegisterStartupScript("Window", "'UpdateBudgetConfirm.aspx?UBID=" + strSAID + "','','width=500,height=320,left=200,top=200');")

Looks like you're not using 2.0. Add the script tags into your call.

Thanks, Ed

Microsoft MVP - ASP/ASP.NET

4/1/2008 6:07:22 PM


4/1/2008 6:13:53 PM

For future reference, please note that you are using 1.1 in any further posts. This will help your answerer greatly.

Thanks, Ed

Microsoft MVP - ASP/ASP.NET

4/1/2008 6:17:19 PM

Still no luck:

Page.RegisterStartupScript("Window", "<script language=""javascript"">'UpdateBudgetConfirm.aspx?UBID=" + strSAID + "','','width=500,height=320,left=200,top=200');"")</script>")

4/1/2008 6:23:55 PM

Sorry, but I don't event have a 1.1 environment that I can test this in anymore. Can anyone else test this?

Thanks, Ed

Microsoft MVP - ASP/ASP.NET

4/1/2008 6:40:25 PM

I've tried the following in Page_Load, but I get a syntax error:

Page.RegisterStartupScript("Window", "<script language=javascript>function AlertUpdate() {'UpdateBudgetConfirm.aspx?UBID=" + strSAID + "','','width=500,height=320,left=200,top=200');})</script>")

btnYes.Attributes("onclick") = "AlertUpdate()"


4/1/2008 7:26:24 PM


There’s a gramma issue in your code. It should be:

Page.RegisterStartupScript("jwb", " <script language=javascript>function a(){'UpdateBudgetConfirm.aspx?UBID=" + strSAID + "','','width=500,height=320,left=200,top=200');}</script> ");

In your code:
Page.RegisterStartupScript("Window", "<script language=javascript>function AlertUpdate() {'UpdateBudgetConfirm.aspx?UBID=" + strSAID + "','','width=500,height=320,left=200,top=200');})</script>")

There’s an unnecessary parenthesis.


Michael Jin.
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
4/7/2008 6:49:23 AM

Thank you for this post. but I tried it with Ajax enabled website but it is not working. is this code working with ajax sites or not if not can you please help me and post a code do the same thing working with Ajax websites.

 Best regards,

4/8/2008 3:50:22 PM

That will not work with AJAX, since the code requires a full page load/re-load to fire.



4/8/2008 4:34:09 PM

Thanks for the reply but do you have any idea how can I solve this problem.




4/8/2008 4:58:58 PM

The only thing that I can suggest is to move to 2.0 since it has functionality built in for handling that situation.



4/8/2008 5:07:40 PM

Thank you so much...


4/8/2008 10:52:42 PM

Similar Artilces:

Open popup window on click of button event from code behind side
 Hi friends I have one button called Preview which is placed in upadate panel. Now I want to open popup window on click of that button. I can call javascript onclientclick event but before open popup window i want to get some values from current window.I dont want to pass that value in query string. Here is my code <pre> ClientScript.RegisterStartupScript(this.GetType(), "JS", "'PreviewPop.aspx?DesignID=" + txtImageHide.Value + "','MyTitle','height=250,width=505, top=100, left=100, scrollbars=yes, res...

executing server side code and client side (javascript) code simultaneously while clicking a button
how to execute server side code and client side (javascript) code simultaneously while clicking a button in You can't. When clicking a button: the client-side executes, the page does a PostBack, then the server-side executes.NC... what if i want to update a database and then open a new windows is it possible with

Calling Button Click Event From Another Event With Client Side Code
Hi All,I am trying to call a (server control) button's click event from another event where the button has a client side code as well. I defined my button as: <asp:Button runat="server" id="button1" onclick="button1_Click" onclientclick="someJSFunction(); return false;" text="..." />  The button1_Click event does not contain any server side code, I just need a reference for my other event to call to, like so: void OtherEvent(object sender, EventArgs e) { button1_Click(sender, e);}  What happens is that button1_Click is being fired but the client side code ...

button click event : how to prevent service code fire from client side script
Button have client side click event and server side click event, my question is: how can I prevent server side click event code execute in conditional from client side script code below: <script language="javascript" type="text/javascript">function Client_onclick() { if (cb.checked == false) do not execute service side code.... elseexecute service side code..... } </script> <form id="form1" runat="server"> <input id="cb" type="checkbox" /><asp:Button ID="Button1" runat="serv...

How to call a javascript function on a button click event after the server side code is run?
Hi, I have a button on click of which i navigate to a different search screen, select some items and come back. While I am coming back to the original screen, I want to invoke a javascript function which actually shows up a small dialog window. Is this possible ? I appreciate any help.   Thanks, Lavanya     On page_load event of the main page you can check the UrlReferrer to know from which address this request is coming and if it was from the search page use Page.ClientScript.RegisterStartyopScript(this.GetType(),"ss","alert('hell...

Javascript confirm message in c# code behind,by clicking ok popup window open
how can i set a confirm javascript message in c# code behind and by clicking ok i have to open a pop up .aspx page. Hello,     Add onclick attribute with javascript function containing within your c# code. btn.Attributes.Add("onclick", "'YourPage.aspx')")  Thanks,santosh_maharajaPlease mark as answer if you got expected solution.  Cool stuff // The action. $('span').mouseover(function() { $(this).html('Here is the offer'); });   $('span').confirm({ msg:'See my interesting offer?', stopAfter:'ok', eventType:'mouseover', timeout:3000, buttons: { ok:'Sure', cancel:'No thanks', separator:' ' } }); popups are disabled on lots of browsers but you can still use them.  modify the code a little.  Good Luck In this example the popup page ust be in the same folder as the calling page.  protected void Page_Load(object sender, EventArgs e) { btnPopup2.Attributes.Add("onclick", "if(confirm('Open Popup?'))'Popup.aspx');"); }    thanks to all. nothing working here. here's my code: ClientScript.RegisterStartupScript(this.Page.GetType(), "Report", "if(window.confirm('Do you want to see report?')){window.fnOpenReport='Reports/BuyerReportViewer....

unable to put the javascript validations & server side code together in a single button click event
 In one of my aspx pages , i am using the javascript validations to validate the Textboxes & dropdownboxes, using the following code below. <script language="javascript" type="text/javascript"> function validate() { if (document.getElementById("ddlWorkitemType").value=="Please Select...") { alert("Workitem Type Feild can not be blank"); document.getElementById("ddlWorkitemType").focus(); return false; } function valida...

How to access Client Side control in server side button click event?
Hi all, I need to access client side control from server side code. For an example <form id="form1" runat="server"> <table> <tr> <td>UserName:</td> <td><input type="text" id="textBoxUserName" /></td> <td><asp:Button ID="buttonShow" runat="server" Text="show" OnClick="buttonShow_Click" /></td> </tr> </table>   </form> I need to access the textBoxUsername in the server side button click event. How to do that? Than...

How to run JavaScript from server code, then trigger button click event from JavaScript?
I would like to have a user click on a button.  The button would first execute some code on the server, then it would trigger the ModalPopupExtender to display a popup form.  The problem is that when a user clicks the button that is the TargetControlID for the ModalPopupExtender, the processing occurs solely on the client. It immediatly pops up the popup and never goes to the server to run the server-side code associated with the button. I decided to make this happen by creating 2 buttons.  The first button will execute the server-side code.  The final step of the c...

Firing both Client side event and server side event for server side button
I am having some difficulties getting this to work.. I have button_click ( and ClientClick (Javascript).  Javascript is firing fine.  Server script is not. may be you have return return false in the in the javascript which is why its not firing server side event.You should add javascript evelt like thisbutton1.onclientclick = "functionname" Function should nopt return false. Vikram www.vikramlakhotia.comPlease mark the answer if it helped you...

how to bind Javascript Confirm ok event with server side button click event
Hello I ve a popup menu to "add a group" or "Delete Group" when i click "Delete Group" menu i want one Javascript confirm box which will ask permission whether to delete or not. When user will press "Ok" on confirm box, i want to call one server side button_click event? How should i proceed with this? Hi dee1j,When you are in your javascript at this point:if (confirm('Do you want to delete')){   var myButton = document.getElementById('IdOfButtonIWantToClickWhenConfirmIsOk');;} Please gi...

how to open popup window through javascript in code behind( popup window should be aligned center)
I want to use javascript in code behind to open popup window. I have Datalist inside which there is linkbutton. On click of linkbutton open popup window through javascript & that pop up window should be aligned centraly.Regards,Mahesh--------------------click "Mark as Answer" on the post that helps you. Friend, Try this sample . <html xmlns="" ><head runat="server">    <title>Untitled Page</title>        <style>    .blue{ bac...

Javascript and Code Behind Button Click
Setup: I have a webform that takes in information saves it to a database when a user clicks on a button. When a user clicks on another button (call it "preview") on that page, the information is pulled from teh database placed ona pdf and opened in a new window. Problem: I want to include the save to the database to the "preview" button. The javascript I have setup for that button fires first and opens up a new browser window with the pdf, the information pulled from the db may not be up to date, because the update portion of code behind may not have completed by the time the request for the page is complete. Question: Is there any way I can handle this situation if it arises, i.e. hold off pulling information until the db is finished with its update? > Is there any way I can ... hold off pulling information until the db is finished with its update? You could use the System.Thread.Sleep method to "hold off" until the DB has finished. Having said that, it would be more user-friendly to not return control to the user in the first instance, until the database update is complete. That way, you can report whether the database was successfully updated or not. Put another way, your current design will make available to the user a "Preview" button, whether or not the data was successfully saved. This may erroneously suggest that the data saved successfully. Hence, I'd recommend doing the "hol...

Calling parent window (using master page) button click event from an opened child window (not using master page)
I have a parent window that pops up a child window. In the child window, there is a server control LinkButton, with the following command at code-behind upon clicking:         Response.Write("<SCRIPT> javascript:window.opener.document.getElementById('<%=btnMas.ClientID%>').click(); </SCRIPT>");        Response.Write("<SCRIPT> javascript:window.close() </SCRIPT>");        Response.End(); What it would like to do are:1) Invoke the ...

javascript in c# code behind for
Hi all, So I have this code that I am using to pass variables in the query string but also use javascript in order to have a and window.close function on both pages.string WindowOpen = "'Notes.aspx?NoteTableId=" + id + "&NoteTable=" + Tables.InvoiceHeader + "'); return false;"; linkNoteButton.Attributes.Add("onclick", WindowOpen);   My question is where in that string would I implement things such as Heigth and Width of the window being open, also like setting menubar and toolbar = 0 so that those aren'...

Button Click Event not firing via Javascript
Hello all. I have a button that is hidden within an UpdatePanel. I use this button to update accordion panels with new information and then call the Update() method of the UpdatePanel. Well, in the Javascript I have one function that calls the click() event on the button, it seems to work fine. But when I call it from another function, the event never fires. There is no error and I stepped through the code and watched it hit the line of code in question and no errors were generated, there is nothing in the Trace, but the button never fires and no events happen. Here is a snippet of t...

open window from server side button click
Hi, I want to open new window and write HTML content to it on sever side button click event. I have written following code:   protected void btnClick_Click(object sender, EventArgs e) { Response.Write("<SCRIPT LANGUAGE=\"JavaScript\"&gt;\n"); Response.Write(" '','popup','toolbar=no,menubar=no,scrollbars=1,width=750,height=800')"); Response.Write("<script>"); Response.Write("<html><head>"); Response.Write("<head><...

open a popup window code behind
Hi I am trying to open a new window from a button in my aspx side. This works, but I have to push the button 2 times before it open. Any idea? Public Shared Sub OpenPopUp(ByVal opener As System.Web.UI.WebControls.WebControl, ByVal PagePath As String) Dim clientScript As String 'Building the client script- clientScript = "'" & PagePath & "')" 'register the script to the clientside click event of the opener control opener.Attributes.Add("onClick", clientScript) End Sub Sub Button1_Click(sender As Object, e As EventArgs) OpenPopup(Button1,"") End Sub Hello, please check my BLOG below, I have an article I posted it there that explains how to open a pop up window from inside regardsBilal Hadiar, MCP, MCTS, MCPD, MCTMicrosoft MVP - Telerik MVP Thanks But, What if you want to execute some server-side code before calling the client-side. Thats what I need to do. Hi, I advise you to check this post : view post 818881 regardsBilal Hadiar, MCP, MCTS, MCPD, MCTMicrosoft MVP - Telerik MVP...

How could add a javascript function to a button click event as a part of its click event handler?
Hi, The title might be confusing. What I want is simply two pages, Page1 and Page2, when I click on a linkbutton on page1, page2 pops up. I used to use a little bit of javascript to do this, and it worked just fine. I just added the following line of code to the Page_Load event of Page1:   protected void Page_Load(object sender, EventArgs e) { LinkButton1.Attributs.Add("onclick","'Page2.aspx',null,'height=500,width=650,status=no,resizable=yes,scrollbars=yes,toolbar=no,location=no,menubar=no')"); } But now, I have...

opening 2 windows when i click the button in c# .net
i have a registration page. when i complete the registation page and click on the submit button. i need to transfer the browser to home page and simultaneuosly i need to open new window shoeing summary of registration. i have home page and summary page already created. but how can i call to pages when i click on the submit button Once the user successfully registers.  You can register a client script block for the load event.  Send the client a Javascript block that 1) opens a window2) navigates to the new url psuedo;window.location = "http://...";...

How to call a client Javascript after server side button is clicked?
Hi all, In Ajax 1.0, is it possible to hook the <asp:Button> within UpdatePanel with a client side javascript to receive response message from server. Let me make it clearer, I have a form with some textboxes and a button, customer clicks button to submit form to server to process, based on different input server may return different message.<asp:Button id="btnSubmit" Text="Submit" runat="server" /> private void btnSubmit_ServerClick(object sender, EventArgs e) { // server returns something } I need to display server returning value in a <DIV> on client ...

opening new window from code behind using javascript
Hello,I am trying to open new window when i click on a button using javascript Page.ClientScript.RegisterStartupScript(this.GetType(), "openwindow", "'WebForm2.aspx','','height=200,width=300');", true);this is the code i am using.But the problem is it postsback whole page.Can some body tell me how i can open new window without posting back the page.ThanksVinod   Hi Vinod,Do you use a scriptmanager on your page?If so, then you should use the RegisterStartupScript method of the ScriptManager class (= ScriptManager.RegisterStar...

How to Call a Javascript Function from a Code Behind Click Event
In ASP.NET, C# web application I am trying to close a modal form from the on click event of a standard button The Javascript is as follows:     <script language="javascript" type="text/javascript">    function CloseFormOK()    {        window.returnValue = true;        self.close();    }     function CloseFormCancel()    {        window.returnValue = false;&nb...

How do I fire a server side button click event from javascript?
here is my server side event.plz can any one help me how to call this function using javascript.   protected void update_week(object sender, EventArgs e) {DataSet month_dataset = (DataSet)Session["month_dataset"]; LinkButton b = (LinkButton)sender;int week = Convert.ToInt16(b.Text.Substring(4,1)); GridView2.Visible = true;GridView3.Visible = true;ETA_funtions.etaclass obj = new ETA_funtions.etaclass(); weeknum = week; Hashtable hshtable = obj.get_strt_end_dates(weeknum, Convert.ToDateTime(monthlbl.Text));TimeSheet.wkenddt = (int)hshtable["wkenda...

Web resources about - javascript via button click event in code behind -

JavaScript Kit- Your comprehensive JavaScript, DHTML, CSS, and Ajax stop
Your comprehensive JavaScript, DHTML, CSS, and Ajax stop

Let's Code: Test-Driven Javascript by James Shore — Kickstarter
James Shore is raising funds for Let's Code: Test-Driven Javascript on Kickstarter! A screencast series focusing on rigorous, professional Javascript ...

Eloquent JavaScript: A Modern Introduction to Programming
... and pragmatics. I loved the tutorial-style game-like program development. This book rekindled my earliest joys of programming. Plus, JavaScript! ...

Lightbox (JavaScript) - Wikipedia, the free encyclopedia
On a Lightbox-enabled page, a user can click an image to have it magnified in a Lightbox window, which resizes itself according to the size of ...

Open Directory - Computers: Programming: Languages: JavaScript: Tools: Obfuscators
about dmoz - help Top : Computers : Programming : Languages : JavaScript : Tools : Obfuscators Tools for obfuscation/compression JavaScript code. ...

Facebook Open-Sources JavaScript Static-Type Checker Flow
The latest software to be open-sourced by Facebook is Flow , a static-type checker for JavaScript . (more…) New Career Opportunities Daily: ...

Under the Hood: The JavaScript SDK - The use of polyfills - Facebook
Facebook Engineering hat eine Notiz mit dem Titel Under the Hood: The JavaScript SDK - The use of polyfills geschrieben. Du kannst den vollständigen ...

Quickstart: Facebook SDK for JavaScript
This quickstart will show you how to setup the SDK, load social plugins, trigger a dialog, and get it to make some basic Graph API calls.

Facebook App Devs Get Javascript
It has been an oft-lamented fact of the Facebook Platform thus far in its life that, in order to use all but the most simple of Javascript functionality–the ...

Badass JavaScript (@badass_js) on Twitter
WebGLWorker is an open source library that allows using WebGL from Web Workers. Great work as always from @kripken .… ...

Resources last updated: 3/4/2016 10:11:35 AM