views:

71

answers:

3

Hello

I am writing a javascript in asp.net server side (with in a button click event), this script is called if the user id and password matches and it supposed to close current window and open welcome page, but it is not happening. Below is my code, can anyone help me figure out what is the problem?

protected void okbtn_Click(object sender, EventArgs e)
    {
        account.txtuser = txtuid.Text;
        account.txtpwd = txtupwd.Text;
        account.login();
        if (account.data == true)
        {
            string script = "<script language='javascript' type='text/javascript'>function f11(){window.close();var strLocation ;var strProfileID ;if (top.opener == null){strLocation = 'YourAccount.aspx';window.location = strLocation;}else{strLocation = 'http://' + top.opener.location.hostname+':'+ window.location.port + '/SendMail/' + 'YourAccount.aspx';top.opener.location = strLocation;top.opener.focus();}}</script>";
            ClientScript.RegisterStartupScript(GetType(),"abc", script, true);

        }
        else
        {
            Label1.Text = "Invalid user name or password";
        }
    }
+1  A: 

Add a OnClientClick event instead. For example:

<asp:Button id="okbtn" runat="server" text="OK" OnClientClick="window.close();" />

As for registering the startup script, you'd probably want to do this on Page_Load.

Druid
Without additional context, this would seem more appropriate than going through the effort of ClientScript.RegisterStartupScript.
jimyshock
A: 

Don't include <script></script> tag.

string script = "setTimeout(f11,10); function f11(){ window.close();}";
ClientScript.RegisterStartupScript(GetType(), "abc", script, true);
adatapost
+1  A: 
ClientScript.RegisterStartupScript(GetType(), "abc", script, true);

if you pass true argument at the end of the function parameters you don't need to add tags in your javascript codes because true means " tags will automatically wrap your javascript code"

Try in this way. If it doesn't help, please let us know.

Braveyard