Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP.NET 2.0 - return a value from a popup window

Status
Not open for further replies.

tahirk

Programmer
Oct 24, 2005
47
GB
Hi,

Need some advice on how best to resolve a issue with a page I am designing. I am developing a site using ASP.NET 2.0 with C# code behind.

I have a page that allows the user to upload 3 images to the server and then displays the uploaded images on the page. To do this I have placed 3 image buttons on the page, when clicked they execute this function:

Code:
        private void PopUp()
        {
            // use the StringBuilder class to create our JS code block

            // create a new instance of StringBuilder
            StringBuilder sb = new StringBuilder();

            // build the string containing the JS script code
            sb.Append("<script type='text/javascript' language='javascript'>");
            sb.Append("window.open('./Dialogs/FileUploadForm.aspx', 'CustomPopUp', ");
            sb.Append("'width=350, height=280, menubar=no, resizable=no');");
            sb.Append("</script>");

            // register with ClientScript
            Type t = this.GetType();
            if (!ClientScript.IsStartupScriptRegistered(t, "PopupScript"))
            {
                ClientScript.RegisterStartupScript(t, "PopupScript", sb.ToString());
            }
        }

This displays a popup window showing a aspx page that allows the user to upload a image. I am using the .NET 2.0 control called FileUpload to handle the file and save it to the server using a postback as per Microsofts direction:

Code:
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                Boolean fileOK = false;
                String path = Server.MapPath("~/Uploads/");
                if (BIUpload1.HasFile)
                {
                    String fileExtension =
                        System.IO.Path.GetExtension(BIUpload1.FileName).ToLower();
                    String[] allowedExtensions = 
                { ".gif", ".jpeg", ".jpg" };
                    for (int i = 0; i < allowedExtensions.Length; i++)
                    {
                        if (fileExtension == allowedExtensions[i])
                        {
                            fileOK = true;
                        }
                    }
                }

                if (fileOK)
                {
                    try
                    {
                        BIUpload1.PostedFile.SaveAs(path
                            + BIUpload1.FileName);
                        ImageUploadStatus.Text = "File uploaded!";
                    }
                    catch (Exception ex)
                    {
                        ImageUploadStatus.Text = "File could not be uploaded. \n " + ex.ToString();
                    }
                }
                else
                {
                    ImageUploadStatus.Text = "Cannot accept files of this type.";
                }
            }
        }

Now what I want to do the following:

1. When the user hits confirm (which causes a postback and uploads the image as above) - close the popup window posting back (or some other way) the full filepath+name from the FileUpload control to a hidden field on the main aspx page.

2. When the above occurs the main page's image button takes the value stored in the hidden field and loads the image as per the filepath+name.

Simple enough but I am finding about a dozen ways of doing something similar i.e. returning a value from the popup window and I really don't know which is the right way of doing it.

Thanks in advance,

Fz
 
popups are usually considered "bad design". to pass values from one window to another requires javascript. To my knowledge this can't be done server side as that's the nature of web.

a google search for "javascript pass values from popup" should get you started.

for a "prettier" interface you could look into the MS AJAX library []. they have a modal popup control which provides the functionality you want. It's driven by javascript and DHTML so everything stays within 1 browser window.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Aye, I started looking into the AJAX and ajax like solution.

I looked into the Callback feature, works great however its more suited to in-page solutions i.e. when your pulling from a drop down box or similar.

I have just installed Ajax for VS.NET 2005 so gonna give that a try now.

Thanks,

Fz
 
Great, seems AJAX 1.0 is bugged to hell and just doesn't work as intened (just google for "ajax 1.0 sys undefined" to get an idea of how bad the problems are).

Seems AJAX 1.0 won't work with Master Page and custom user control websites so I am ditching that for now. Will either try using a more mature ajax solution like DoJo or Mootools or I might just cheat and create hidden panels that I display from an on-click event.

 
there are no problems with MS AJAX, Master pages or user controls. what does your aspx look like that's causing so many errors?

do you include any custom javascript with your pages? i believe there is an 'if' statement that needs to be included if you load your js from the script manager/proxy.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Wont the your popup only work on computers with .net framework installed?
No. Active Server Pages so the code runs on the server not the client.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Hello to FinalZero.

Does it have to be a popup? I'm doing the same thing and thought about a solution. My testing shows popups to be unreliable.
What if you used an overlay? Or a layer?
make it look like it's a popup but it's really part of the page?

I have an app that gives the user an excel spreadsheet out of the columns they picked from a grid.

They will be editing that spreadsheet then uploading it back into the system.
A popup stinks for this.

Thanks for the info on the FileUpload control. I was almost going to code it by hand.

~BF
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top