Solved

Help building javascript with StringBuilder

Posted on 2008-06-26
3
620 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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
What are the simplest ways to create a Merge Sort? 3 29
jquery tab header text 1 22
Html input text color the frame line 2 20
asp.net open new page without popup blocker 8 18
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

839 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