HANDLING THE SEQUENCE OF CODE BEHIND CODE AND CLIENT SIDE SCRIPTING?

In a form on a page I want to save the data values entered into the varieous server controls, such as RadioButtonLists, TextBoxes, etc. before displaying the saved information in a new page suitable for later print.

I have a LinkButton on page1.aspx, whose Click event uses the code behind to save the values into a dataset. The LinkButton also has a OnClientClick then execute the JavaScript on page1.aspx, that simply does a window.open of page2.aspx that uses a different Masterpage from Page1 in order to respect the printing requirements.

But!!!!!!!

The client side script is executed before the code behind, and therefore you will have the new window opened and seeing that the values you entered are not yet saved

How best to change the sequence of events here? to give the Save a chance to complete before the window.open(.....)

 

1
trugge
5/28/2006 12:29:14 PM
asp.net.web-forms 93655 articles. 3 followers. Follow

16 Replies
571 Views

Similar Articles

[PageSpeed] 14

You might output the client-script from the server-side handler instead, with a RegisterStartupScript or similar. Anyway, in your specific scenario, i would just issue a Response.Redirect from the server-side...

HTH. -LV


Julio P. Di Egidio
Software Analyst Programmer
=BUSINESS AND SCIENTIFIC=
=SOFTWARE DEVELOPMENT=
http://julio.diegidio.name

(Peace X Love] = [++1)
1
LudovicoVan
5/28/2006 1:29:21 PM

Thanks

I used to have a Response.Redirect in the code behind, right after the "save", but doing it this way, I don't know how to control the sie of the new window and other options.

1
trugge
5/28/2006 1:53:42 PM

Is it possible to include a JavaSript in the code behind for the button click event. This would solve the problem

 

 

1
trugge
5/28/2006 2:13:23 PM
LudovicoVan:

You might output the client-script from the server-side handler [...], with a RegisterStartupScript or similar.

-LV


Julio P. Di Egidio
Software Analyst Programmer
=BUSINESS AND SCIENTIFIC=
=SOFTWARE DEVELOPMENT=
http://julio.diegidio.name

(Peace X Love] = [++1)
-1
LudovicoVan
5/30/2006 6:39:43 PM

Hi Ludovico,

Letting the server send out a script to the client is all new to me. I have no idea where to look for examples.

Can you write a small example for me, please.

This problem is a major issue for me

Thanks in advance

Torben

-1
trugge
6/1/2006 6:28:43 AM

Torben, search RegisterStartupScript in documentation. And, even if you don't like documentation, this particular question gets asked around every day in this same forum only, not to talk about the rest of the web.

Also, i won't give samples because i actually don't like this approach, and avoid it at all. In ten years and more of web programming, i've never output a single line of client-side code from the server-side! My way is by exposing some properties:

Server-side (C#):

   private int _Mtx = 0;
   protected int Mtx { get { return _Mtx; } }
   protected void Page_Load(...) { ...; _Mtx = 0; ... }
   protected void Some_Click(...) { ...; _Mtx = 1; ... }

Client-side (JS):

   if(<%= Mtx %> == 1) alert("Hello, world!");

HTH. -LV


Julio P. Di Egidio
Software Analyst Programmer
=BUSINESS AND SCIENTIFIC=
=SOFTWARE DEVELOPMENT=
http://julio.diegidio.name

(Peace X Love] = [++1)
1
LudovicoVan
6/1/2006 12:59:25 PM

private void openWindowButton_Click(object sender, System.EventArgs e)
{
 // *** Server-side processing here **


     string scriptKey = "OpenWindowScript";

     System.Text.StringBuilder sbScript = new System.Text.StringBuilder();
     sbScript.Append("<script language=JavaScript>\n");
     sbScript.Append("<!--\n");
     sbScript.Append("window.open('Page2.aspx');\n");
     sbScript.Append("// -->\n");
     sbScript.Append("</script>\n");

     this.RegisterStartupScript(scriptKey, sbScript.ToString());
}

NC...

 

1
NC01
6/1/2006 3:14:35 PM

NC, imho you could at least just output a function call.

But ok, i know you are a fan of "mixing responsabilities"... <lol>.

;) -LV


Julio P. Di Egidio
Software Analyst Programmer
=BUSINESS AND SCIENTIFIC=
=SOFTWARE DEVELOPMENT=
http://julio.diegidio.name

(Peace X Love] = [++1)
1
LudovicoVan
6/1/2006 3:22:11 PM

LOL, no I just copied it out of code that I posted for someone else last week.

NC...

 

1
NC01
6/1/2006 3:36:24 PM

Hi NC

I am having a little problem with converting your fine code into VB, as all of my code-behinds are VB, and there is a lot of stuff on this particular code-behind page as is.

I have written this much until I get a syntax error:

Protected Sub tilKontinuation(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton9.Click

 Call DoSave()

Dim scriptType As ??????

 Dim scriptKey As String = "OpenWindowScript"
 Dim sbScript = New System.Text.StringBuilder
 sbScript.Append("<script language=JavaScript>\n")
 sbScript.Append("<!--\n")
 sbScript.Append("window.open('kontinuation.aspx');\n")
 sbScript.Append("// -->\n")
 sbScript.Append("</script>\n")

Here comes the problem:
Documentation says that RegisterSartupScript needs 3 parameters of (type, string, string)
So: 1) How to specify the type of script in this parameter?

this.RegisterStartupScript(scriptType, scriptKey, sbScript.ToString())

Kind regards
Torben

and 2) How do I replace the "this" in the above line with something VB valid?

 

-1
trugge
6/3/2006 8:06:58 AM

Finally got it working.

Here is the code for anyone else to use. However, it did not solve the original problem posed, namely how to have the server side script execute before the client side script.
Window.open fires before DoSave() , shame. So this is back to square one

Torben

Protected Sub tilKontinuation(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton9.Click

Call DoSave()

Dim MyClientScript As ClientScriptManager = Page.ClientScript

Dim scriptKey As String = "OpenWindowScript"

Dim sbScript = New System.Text.StringBuilder

'System.Text.StringBuilder(sbScript = New System.Text.StringBuilder())

sbScript.Append(

"<script language=JavaScript>\n")

sbScript.Append(

"<!--\n")

sbScript.Append(

"window.open('kontinuation.aspx');\n")

sbScript.Append(

"// -->\n")

sbScript.Append(

"</script>\n")

MyClientScript.RegisterStartupScript(

Me.GetType(), scriptKey, sbScript.ToString())

 

End Sub

 

1
trugge
6/3/2006 11:25:04 AM

Sorry folks,

My mistake, the above code generates an error

1
trugge
6/3/2006 11:34:42 AM

Protected Sub tilKontinuation(ByVal sender As Object, ByVal e As System.EventArgs)

Call DoSave()

Dim MyClientScript As ClientScriptManager = Page.ClientScript
Dim scriptKey As String = "OpenWindowScript"
Dim sbScript = New System.Text.StringBuilder
sbScript.Append(
"<script language=JavaScript>\r\n")
sbScript.Append(
"<!--\n")
sbScript.Append(
"window.open('kontinuation.aspx')\n")
sbScript.Append(
"//-->\n")
sbScript.Append(
"</script>\n")

MyClientScript.RegisterStartupScript(

Me.GetType(), scriptKey, sbScript.ToString())

 

End Sub

When clicking the "Play" button it Gives an error: Invalid character:  in line 509, which is the </div> tag
When saying yes to debug I can see that the generated line of Javascript evidently contains an invalid charactr, But which?

<

div>

<input type="hidden" name="__SCROLLPOSITIONX" id="__SCROLLPOSITIONX" value="0" />

<input type="hidden" name="__SCROLLPOSITIONY" id="__SCROLLPOSITIONY" value="0" />

<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWTQLd8YyMAwKOkrn2CAKLkrn2CAKMkrn2CAKRkrn2CAKSkrn2CAKPkrn2CAKQkrn2CAKFkrn2CAKGkrn2CAKOkvn1CALh8tHdBgKuo87aCQKCsYb1AwLk3qCaDwKv2LXUCgL7o46+BQLeq83dBQKbm9LVAQLul4HZCgLupNenAgLCq6tPAoL8icYHAs6kjb8JApz70aMGAo7h5NgCAvOStJIPAsDzs5YEArmav7ABAuGPlJ0IAr60nYcPAumnsfsNAvTn2qoEApaemLUNApyemLUNApeemLUNAp2emLUNApiemLUNAp6emLUNApmemLUNAp+emLUNApqemLUNApee2LYNApuemLUNAsmR1IcJApKm850LAtyp5ckGAv2P578IAvK39MkIAt3NiPwPAtvX4/QNAt/KufAFAriI7sADAt2/z7cDAqWV5ZYEAsm0y4kOAt2gtIUMAv6vrpYCAviZuNsCAq3k/ZMMAr2KqJkDAv2I5tQPAsnCyvMHAsb62YUHAve1k4oJAt2skYwJAtDA2vsHAr7D59AHAqClzNEOAoLOxuoFAv3klrYIArDe3/MDAu60rZQLAoz0vcwBAtKdgtgGAtiey9MCAoPiyWNPy18eZo9bVttQ2awnv5aDqfKRjQ==" />

</

div>

<

script language=JavaScript>\r\n<!--\nwindow.open('kontinuation.aspx')\n//-->\n</script>\n
1
trugge
6/3/2006 12:30:59 PM

Private Sub openWindowButton_Click(sender As Object, e As System.EventArgs)
   ' *** Server-side processing here **
  
   Dim scriptKey As String = "OpenWindowScript"
  
   Dim sbScript As New System.Text.StringBuilder()
   sbScript.Append("<script language=JavaScript>" + ControlChars.Lf)
   sbScript.Append("<!--" + ControlChars.Lf)
   sbScript.Append("window.open('Page2.aspx');" + ControlChars.Lf)
   sbScript.Append("// -->" + ControlChars.Lf)
   sbScript.Append("</script>" + ControlChars.Lf)
  
   Me.RegisterStartupScript(scriptKey, sbScript.ToString())
End Sub 'openWindowButton_Click

C# to VB translator site for future use:
http://authors.aspalliance.com/aldotnet/examples/translate.aspx

NC...

1
NC01
6/3/2006 6:04:21 PM

Hi NC, and thanks

I have made the thing work by removing the "\n"s alltogether. A simple string with no "new line"s works fine.

Torben

1
trugge
6/3/2006 7:15:15 PM

"\n" is C#. ControlChars.Lf is the same in VB.

NC...

 

1
NC01
6/4/2006 2:21:44 PM
Reply:

Similar Artilces:

Web Forms Designer error
Hi, The web forms designer keeps adding things in a generated InitializeComponent() method. I believe these things shouldn't be there because I've made a new webform with the same code that doesn't add the same stuff. Is there anyway I can see the Web Designer code? Thanks. Just expand out the hidden #region. It is labeled Web Form Designer CodeStarting with ASP.NET 2.0? Look at:Programming Microsoft Web Forms My Blog...

Question about client side and code behind code
How is code behind code processed on client side? I ask because I'm trying to figure out a problem I have with a link button in a gridview. If I open the page and run it as normal everything fires fine. I click on the link button and it loads the new page. However if I email the page to myself and click on the link button nothing happens. I do not think that the Gridview_Rowcommand fires. The link button represents a DB ID field and all the code is in the GridView_RowCommand event in the code behind and fires a Server.Transfer at the end to load the new page. I have tried Response.Redi...

Calling a sever side code behind sub routine from client side java script
Hi is it possible to call a server side sub routine from client side javascript what I what to do is call a form based email routine from the client, I am hoping that by doing it this way I can disable the button used to call the routine imeadiatly so it's only possible to click it 1 time to prevent the same email being send multiple times, and in addtion start a client routine to indicate the email is underway with some graphic of some sort so the use knows that something is happening, all this is because if the attacment is quite large it takes a little while for the procees to...

Call client side script from code behind
Hi there, I'm trying to add two listboxes to a page so a user can move items between the lists. I need to create these list boxes in the code, as different listboxes appear for different users. The problem I'm having is that once I move the listbox creation to the code behind, I can't seem to get it to call the client side move function.  Here's me code (lines 13 and 20 contain the call to the javascript function that I'm getting wrong):   1 TableRow tableRow = new TableRow(); 2 TableCell lb1tableCell = new TableCell(); 3 lb1tableCell.Te...

client side code & server side code
what is the difference between client side coding & server side coding ? when to use client side & server side code? Client side code runs on the user's browser and includes languages such as javascript and vbscript. Server side code runs on the web server and can be written in languages such as ASP.NET, PHP, ASP, ColdFusion etc. Website Design Darlington - http://mdssolutions.co.ukhttp://lessthandot.com - Experts, Information, Ideas & Knowledgehttp://aspnetlibrary.com - An online resource for professional ASP.NET developersPlease remember to click "Mark as Answer...

how to change these windows form code to web form code?
hi, i'm a beginner of c#,i want to know how to change these windows form code to web form code //////////// private void buttonSign_Click( object sender, System.EventArgs e) { //get original message as byte array byte[] messagebytes = Encoding.UTF8.GetBytes( textOriginalMessage.Text); //create digest of original message using SHA1 SHA1 sha1 = new SHA1CryptoServiceProvider(); byte[] hashbytes = sha1.ComputeHash(messagebytes); //display hash bytes in hex format StringBuilder sb = new StringBuilder(); for (...

how can i convert code behind web forms pages to inline code pages
hi all of you, I have a problem, a have registered a free asp.net space at Brinkster , so i can just upload my files there one by one, but can not install my web application directly, and i have to upload my projects there, so one way is o convert all my VS.NET web application which is code behind, convert that into inline code, so how can i do that Thanks in advanceWarm Regards,Prajesh Kumar JhaE-MAIL : prajesh.jha@rediffmail.com Convert from Code-Behind to Code-Inline: http://www.codinghorror.com/blog/archives/000174.html Darrell Norton, MVPDarrell Norton's BlogPlease m...

validate web form entries via textbox again in the C# (code-behind) code again
I have requiredfieldvalidators, comparevalidators and a regualarexpressionvalidator for the email on my login form for the textboxes. My question is do I have to check in the C# to make sure the textboxes have values and if so what does a experienced programmer think of my code. What would you change??? Are the if statements the way to go??? Thanks for making me a better coder................newbieprotected void LoginButton_Click(object sender, EventArgs e){string emailAddress = String.Empty;string password = String.Empty;if (!String.IsNullOrEmpty(TextBoxEmailAddress.Text)){emailAddress = Te...

Code Behind or not Code Behind
Ok i am confuse now.... A lot of books are showing pages without code behind (the code is integrated in the .aspx) Is it better to use the code behind or not. Is there any difference between using VB.NET or C#.NET except for the code synthax. Thank you and come again. A lot of samples show inline code just for simplicity.. i personally use code-behind because I like to seperate my code from presentation. As for VB.NET or C#, its your preference.. there is no difference really when its all said and done. From a purely mathematical sense, having everything in a single file would s...

what about the code-behind (code behind)
One MAJOR difference between AJAX.NET and Atlas.NET seems to be that AJAX.NET is able to expose methods in your codebehind to the client. Why doesn't Atlas do this? Or am I mistaken? With AJAX.NET it's just a simple line or two:  Ajax.Utility.RegisterTypeForAjax(GetType(myClass)) <Ajax.AjaxMethod()>Public Function myFunction PLEASE tell me Atlas has something comparable to this. I don't have the time to rewrite all my code-behind methods in javascript or atlas script. All you need to do in Atlas to expose a method on your page (or its code behind class) is to put a [WebMet...

Handling MaskEditValidator suceeds or failed event in code behind and in client side
Hi,When I use MaskEditValidator along with a maskedit ajax control. I need to trap the event when validation has taken place to check if it suceeded or it failed in order to implement some logic. I want to do that in both client side and code behind. Can u please tell me how? if it s only possible in one place (client or code behind), can u tell me how?Thank you. Thanks a lot, I appreciate your taking the time to help me. Hi tutus, To handle whether the input value is valid or not, please refer to this code:  <%@ Page Language="vb" AutoEventWireup="false"...

Calling a Form Code-Behind function using Javascript from the Client Side
Hi,Is there any way to call a function in the form codebehind from the client side using javascript? If yes, how? How to send parameters?Thanks. Try reading this article.  It is kind of the opposite of what you are trying to do but maybe it will help.http://www.tek-tips.com/viewthread.cfm?qid=1027513&page=1Hope this helps,MattADV Enterpriseswww.adventerprises.net do a view source on the page and you most liekly will see a do post back function. You can call that with JavaScript and it forces the page back. You can send parameters back by placing them inside of hidden form e...

difference between code inside a <script> tag and code in the code-behind file
What is the difference between code that exists inside the <html><script runat="server" > tag and code that exists in the code-behind file? Thanks, Keith There really isn't much of a difference other than the code inside the <script runat="server"> tag is part of the web form while code in a class file is part of the class file and inherited in the web form. HTH,RyanRyan OlshanASPInsider | Microsoft MVP, ASP.NEThttp://ryanolshan.comHow to ask a question...

Code behind vrs. non code behind in VS.NET 2003
Does anyone know what the advantages and disadvantages are for using code behind vrs. non code behind in Visual Studio.Net 2003? One important difference is that you are able to separate the design of a web site from the code. This makes for ease of maintainablity and much more structured applications. If you ever worked with classic ASP the code-behind is a great invention. sivilianMy Weblogs...

Web resources about - HANDLING THE SEQUENCE OF CODE BEHIND CODE AND CLIENT SIDE SCRIPTING? - asp.net.web-forms

Scripting News: Home Page
Scripting News, the weblog started in 1997 that bootstrapped the blogging revolution.

Talk:Cross-site scripting - Wikipedia, the free encyclopedia
This article is within the scope of WikiProject Computer Security , a collaborative effort to improve the coverage of computer security on Wikipedia. ...

Scripting language - Wikipedia, the free encyclopedia
A scripting language or script language is a programming language that supports the writing of scripts , programs written for a software environment ...

Scripting News
Scripting News: Roy Wildstein . # Scripting News: Title-less items in my feed . # Scripting News: A celebratory podcast! # Today's background ...

Scripting News: A reprieve for Google Reader readers.
I hate to hear the stories of readers who will muddle along and keep reading even though they wish there was a way to get the full text of the ...

Scripting News: He called the President 'boy'.
Just tuned into a little of the Sunday morning news discussion on various networks. The big issue seems to be whether or not VP Biden was disrespectful ...

Thread: The new River tab on Scripting.
Today there is a new tab on the Scripting News home page. Screen shot . I've felt for a long time that every serious news organization and blog ...

Scripting News: It's a good day.
Lots of good stuff today. 1. Obama won re-election. 2. He gave a great speech. 3. Romney gave a great concession speech. 4. As of today, ...

Scripting News: Election almanac e-book?
It would be useful to have an e-book you could buy for say $10 put out by a reputable news organization, that gathers, in book form, all the ...

Scripting News: The world is socialist, part II.
I try to save big ideas for January 1, every year. Hard things to write, things that take a while to think about, both as ideas, and how I want ...

Resources last updated: 1/17/2016 2:53:43 AM