Solved

Help building javascript with StringBuilder

Posted on 2008-06-26
3
611 Views
Last Modified: 2010-05-18
In my ASP.Net 1.1, VB.Net application, I am opening a modal popup window.
When the submit button in that window is clicked, I am trying to write some javascript
that will set the window.returnValue and kick off the __doPostBack of the parent
page.

So far, it goes so far as to force the parent page postback, but the __doPostBack
arguments I am passing are not being received in the parent page.  In the page load event,
I am checking the Request.Form("__EVENTTARGET") and Request.Form("__EVENTARGUMENT")
but they are blank.

This is my code:

            result = SavePDFsToDatabase(fileList)
            Dim script As StringBuilder = New StringBuilder

            script.Append(vbCrLf)
            script.Append("<script for=""window"" event=""onload"" language=""javascript"">")
            script.Append(vbCrLf)
            script.Append("window.returnValue=")
            script.Append(result.ToString)
            script.Append(";")
            script.Append(vbCrLf)
            script.Append("window.close();")
            script.Append(vbCrLf)
            script.Append("_doPostBack('RefreshData,window.returnValue');")  <==THESE VALUES ARE NOT MAKING INTO REQUEST.FORM FOR THE  PARENT PAGE.
            script.Append("</script>")

            RegisterClientScriptBlock("", script.ToString)

This is the Javascript being generated:

<script for="window" event="onload" language="javascript">
window.returnValue=1;
window.close();
_doPostBack('RefreshData,window.returnValue');</script>

Help!
Thanks.
0
Comment
Question by:JeffDrummond
3 Comments
 
LVL 10

Accepted Solution

by:
orbulat earned 250 total points
ID: 21881748
http://www.velocityreviews.com/forums/t76583-lose-state-information-when-using-dopostback-from-javascript.html

see if it helps, u may also have a look on the examples that overrides doPostBack

just a quick thought, how about using "RegisterStartupScript" instead of "RegisterClientScriptBlock"
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 250 total points
ID: 21882023
When a showModalDialog runs, it does

var someVar = showModalDialog(....

and someVar is updated with returnValue when the window is closed
That is as far as I know all that happens unless asp.net generates more complex code for the parent window on the doPostBack
0
 

Author Comment

by:JeffDrummond
ID: 21916111
Thanks for your comments.  I have been able to get this to work.  My modal window.returnValue
does get passed back to the invoking Javascript in the parent page.  With that value I can
determine whether or not to use __doPostBack.  In my parent Page_Load event, I look for
the __EventTarget and __Event Argument values.  

In Javascript:

// Open window that allows user to add PDF files.
function OpenAddPDFWindow()
{
var rowsInserted = window.showModalDialog('AddPDFs.htm','','dialogWidth:640px;dialogHeight:510px');
if (rowsInserted > 0)
     {
      __doPostBack('RefreshData', rowsInserted);
     }
}

In Page_Load:

                '-- Check if this postback is the result of uploading a .pdf file.
                '-- Set the message label if so.
                '-- This postback is actually a __doPostBack in Javascript.
                Dim target As String = Request.Form("__EVENTTARGET")
                Dim rowsInserted As Integer

                Try
                    rowsInserted = Int32.Parse(Request.Form("__EVENTARGUMENT"))
                Catch ex As Exception
                    rowsInserted = 0
                End Try

                If target = "RefreshData" Then

                    If rowsInserted > 0 Then
                        Me.lbl_Message.Text = rowsInserted.ToString & " PDF file(s) saved successfully."
                    End If

                End If

                PopulateDatagrid()

If there is a better way of doing this, I would of course appreciate the advice.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 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

20 Experts available now in Live!

Get 1:1 Help Now