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(.....)
![]() |
0 |
![]() |
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)
![]() |
0 |
![]() |
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.
![]() |
0 |
![]() |
Is it possible to include a JavaSript in the code behind for the button click event. This would solve the problem
![]() |
0 |
![]() |
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)
![]() |
-2 |
![]() |
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
![]() |
-2 |
![]() |
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)
![]() |
0 |
![]() |
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...
![]() |
0 |
![]() |
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)
![]() |
0 |
![]() |
LOL, no I just copied it out of code that I posted for someone else last week.
NC...
![]() |
0 |
![]() |
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
Torbenand 2) How do I replace the "this" in the above line with something VB valid?
![]() |
-2 |
![]() |
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
TorbenProtected 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
![]() |
0 |
![]() |
Sorry folks,
My mistake, the above code generates an error
![]() |
0 |
![]() |
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
![]() |
0 |
![]() |
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_ClickC# to VB translator site for future use:
http://authors.aspalliance.com/aldotnet/examples/translate.aspxNC...
![]() |
0 |
![]() |
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
![]() |
0 |
![]() |
"\n" is C#. ControlChars.Lf is the same in VB.
NC...
![]() |
0 |
![]() |