Solved

Help building javascript with StringBuilder

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error handling in asp.net site 5 26
Passport Expiry 3 44
Firing an ASP.NET event on selection of a JQuery datepicker 4 16
Select case on click 3 16
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…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

773 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