views:

912

answers:

6

Hi!

My problem is simple, but I cannot find a way to solve it :(
When the user clicks a specific button, I need to open a new aspx page (in a new window) so that he is able to select something that needs to be returned to the original page. I was able to open the new page with window.open, however, I'm not able to set the selected value back in the original page.
In summary, an input field needs to receive a string value from the opened page. I've tried with window.opener.getElementById(), but ASP.NET messes with the control's Id's, so I'm not able to determine the correct Id of the input field at runtime.
Is there a simpler way to do this only with server side code (open the page and return the value), instead of using Javascript and window.opener (which is pretty bad, in my opinion)?
If not, Any other simple solution is also welcome!

Thanks all!

Felipe

A: 

Perhaps instead of opening a seperate page in a new window you should show an element in the current window and use AJAX controls to make sure that the relevant parts of the page are all updated. Take a look at the ASP.NET AJAX PopUp extenders and, of course, the UpdatePanel.

AdamRalph
A: 

why are the control IDs changing? Are you using master pages? If so, you can just get the control IDs at runtime, though they are different then what you named them they will always have the same value.

That is the best solution.

Sara Chipps
A: 

You can use Server.Transfer on the second .aspx page (which can be a popup) to return the querystring and form variables to the first .aspx page.

Server.Transfer("firstPage.aspx", true)

Then on the first page, retrieve the value of the control you want like this:

string x = Request.Form["SomeTextbox"].ToString();
DOK
A: 

Yes, I'm using master pages, this is why the ids change.
I believe I found a solution. I'm using the control class attribute, instead of the id or name, since the class is always the same. Then, i'm using the class name to obtain the id and pass it in the querystring to the new page. The new page, then, gets the id from the querystring and getElementById to obtain the control object. With the object, it is able to set its value to the desired value.
This is not the simplest solution, but worked for me. Thanks for the replies. I'm done!

Felipe Lima
A: 

I use the following methods to achieve a similar result to yours:

In the main page, I check to see if the browser can show a Modal window (basically, Internet Explorer), and if so, pop the new window as modal - this enables me to return a value directly back to the calling method, otherwise I just pop a new window and hope:

if (window.showModalDialog)
{
    // showModalDialog returns a result, so pass that into the receiving funtion
    // directly.
    CallbackFunctionName(ShowModal(displayPath));
}
else
{
    // Just use the standard window.open methods.
    ShowWindow(displayPath);
}

function ShowWindow(displayPath)
{
    var remoteWin=window.open(displayPath,"EditContact",
        "resizable=yes,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,copyhistory=0,modal=yes,width="
        + dialogWidth + ",height=" + dialogHeight);
    remoteWin.creator=self;
    remoteWin.focus();
}

function ShowModal(displayPath)
{
    return window.showModalDialog(displayPath, "editContact",
        "center:yes;resizable:no;dialogWidth:" + dialogWidth +
        "px;dialogHeight:" + dialogHeight + "px;");
}

Then, on the page that is opened, I again check for the ability to display a modal window, as before:

if(!window.showModalDialog)
{
    // Call the callback method directly
    window.opener.CallBackFunctionName(resultValue);
}
else
{
    // Return the value back to the caller, who will pass the result along.
    window.returnValue=resultValue;
}
self.close();

ASP.NET only messes with the control IDs in a known way - once the control has been loaded by the runtime on the server, you can retrieve the ClientID which will contain the runtime id of the control - you could either write this value into a JS variable, or use some JS to pick up the control value based on other features of the control.

Zhaph - Ben Duguid
+2  A: 

If you use something like jquery, which, by the way, is highly recommended instead of using basic DOM functions, you can try this:

window.opener.$("[id$=txtValue]").val(valueToSet);

which should do the following:

  • window.opener.$ if the query function from the opener window
  • ("[id$=something]") is a selector that selects all elements that have an "id" attribute that ends with "txtValue" (this is what gets you around the ugly Id issue).
  • .val("some value") sets the value of that input field to "some value".

I don't recommend hardcoding the asp.net ID in the javascript code. That ID will always change at some point in the future for one reason or another and your page will break. Not to mention it's ugly :)

Also, while the window.opener solution has its downsides, doing this on the server-side will usually lead to worse things, like using the Session to pass data between pages or race conditions between which page (the parent or the child) postbacks first.

c15c8ra1n