1

I had a form which is called contact.aspx and it has a dropdown which includes user list.

I add below line to add user.

<a href="#" id="userLink" onclick="insertUser()" class="addnew" runat="server">New User</a>

and the javascript of insertUser is below:

<script type="text/javascript">
    function insertUser() {
        var win = window.open('stackoverflow.aspx?t=1', 'User Insert', 'width=800,height=600');
}
</script>

And when I click "New User", stackoverflow.aspx is opened and I want to enter new user data and click save. After clicking save buton, how can I close stackoverflow.aspx and only refresh dropdown at the contact.aspx?

Stack Over
  • 625
  • 1
  • 6
  • 10

4 Answers4

0

You can use the following Javascript code to close child window and refresh parent(ref):

window.close();
if (window.opener && !window.opener.closed) {
window.opener.location.reload();
} 

or you can do partial refresh. see here

Zaki
  • 5,540
  • 7
  • 54
  • 91
  • But after saving button there will be some database operation, I think the javascript will work first and there wont be database operation. Isn't it? – Stack Over Jun 24 '13 at 08:10
  • You can always delay this operation if you use Ajax web-methods and only run it on success. see here(http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/) – Zaki Jun 24 '13 at 08:11
  • I use window.close(); for closing child window and I write a reload javascript function called Foo() in parent and I reached it with window.opener.Foo().. – Stack Over Jul 02 '13 at 12:42
0

you must stored variable in session , db or ...

and use this link.

http://forums.asp.net/t/1606853.aspx/1

Ali Rasoulian
  • 419
  • 6
  • 12
  • You should not alter a server-side control through JavaScript, otherwise you'll get a security error: http://stackoverflow.com/q/5024154/91696 – Albireo Jun 24 '13 at 08:34
0

Don't do this on different page, because you can't update your field from an other page. You can make ajax requests from the contact.aspx by intervals, and you can check the DB if there is a new user, and refresh it. But it's not a nice solution. So I suggest to make this registration (which is on stackoverflow.aspx) on contact.aspx, inside an iframe or just inside a div. You can hide each content in each action (when you are registering, hide the rest of content just display the registration fields, if you are done refresh the dropdownlist reset your registration fields and hide them, display the dropdownlist).With this approach you can refresh your dropdownlist, when you are done with registration.

To refresh dropdownlist make an ajax call, where you can get the new values from the db (there can be multiple new values submitted by different user at the same time) and then you cen refresh dropdown (wich is a select with options in HTML) But if you do this, you can get an eventvalidation error, which is a built in asp.Net feature (defends against xss attack). So the solution can be to make a full postback after registration, and refresh the values by server code, or don't use server control to dropdownlist, only html select, and fill it on document ready, and refresh it async by javascript or jQuery.

speti43
  • 2,886
  • 1
  • 20
  • 23
0

mmm, On the stackoverflow.aspx after user clicks save, it posts to the server, creates a new user and get a JSON representation of the object. then register a script to close the window and pass the json to the opener.

private btnSave_Click(object sender, EventArgs args)
{
    //Save data
    ...

    string objectJson = GetJSON(); // {"userId": 100, "name": "John Smith"}

    ClientScriptManager cs = Page.ClientScript;
    StringBuilder cstext1 = new StringBuilder();
    cstext1.Append("<script type=text/javascript> window.opener.appendObject(" + objectJson  + ") </");
    cstext1.Append("script>");

    cs.RegisterStartupScript(this.GetType(), "RefreshScript", cstext1.ToString());
}

On the contacts.aspx have the following script:

<script type="text/javascript">
    function appendObject(json) {
        var obj = JSON.parse(json); //convert json to object

        //Add item to the drop down list.
        var x = document.getElementById("mySelect");
        var option = document.createElement("option");
        option.text = obj.name;
        try
        {
            // for IE earlier than version 8
            x.add(option, x.options[null]);
        }
        catch (e)
        {
            x.add(option,null);
        }

    }
</script>

Didn't test it but I think it works.

Tamim Al Manaseer
  • 3,554
  • 3
  • 24
  • 33