locked
How to redirect from one form to another using javascrip???? RRS feed

  • Question

  • User-394508673 posted

    Hi i ve a form in folder test.aspx, i want to redirect page to some other form, which is on root level (using javascript). m using the following syntax:

    btn.Attributes.Add("OnClick", "javascript:window.location.href='rootForm.aspx'");

    but it seach it within a folder(like filderName/rootForm.aspx) but it is on root level so m getting page not found exception...

    Monday, November 10, 2008 6:08 AM

Answers

  • User798903548 posted

    No bro it still giving the same error it finding the form in a folder and not going to root level:

    The resource cannot be found. UI/Folder/rootForm.aspx

    i should search it in UI/rootForm.aspx

    The question is where is the "UI" folder contained?

    If it is in the application folder, this will work: string windowUrl = CreateWebUrl(this, "UI/Folder/rootForm.aspx")
    Inetpub
         WWWRoot
              AppFolder
                   UI
                     Folder
                         rootForm.aspx

    If it is off of the web site root, that will not work.
    Inetpub
         WWWRoot
              UI
                Folder
                    rootForm.aspx

    You'd need to do something like this:
    string qualifiedFilename = "UI/Folder/rootForm.aspx";
    string windowUrl = string.Format(
         "{0}/{1}",
         Page.Request.Url.GetLeftPart(UriPartial.Authority),
         qualifiedFilename);

    NC...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 11, 2008 7:31 AM

All replies

  • User948943842 posted

    The new href will be interpreted relative to the location of the current page, so for example if the page containing the button is in a sub-folder, the command would need to be 

    btn.Attributes.Add("OnClick", "javascript:window.location.href='../rootForm.aspx'");

    Monday, November 10, 2008 8:39 AM
  • User798903548 posted

    Or create a fully qualified URL

    // string windowUrl = CreateWebUrl(this, "YourPage.aspx");
    // ASSERT(windowUrl == http://localhost/Test/YourPage.aspx)
    //
    // string windowUrl = CreateWebUrl(this, "subFolder/YourPage.aspx");
    // ASSERT(windowUrl == http://localhost/Test/subFolder/YourPage.aspx)
    public string CreateWebUrl(System.Web.UI.Page currentPage, string qualifiedFilename)
    {
     return currentPage.Request.Url.GetLeftPart(UriPartial.Authority) +
      currentPage.Request.ApplicationPath + "/" + qualifiedFilename;
    }

    Then your code would be:
    Button1.Attributes.Add("onclick", string.Format("window.location.href='{0}'; return false;", CreateWebUrl(this, "rootForm.aspx")));

    The "return false" is very important and without it, the code will not work, so which ever way that you choose, make sure that it is there.

    NC...

    Monday, November 10, 2008 9:00 AM
  • User-394508673 posted

    No bro it still giving the same error it finding the form in a folder and not going to root level:

    The resource cannot be found. UI/Folder/rootForm.aspx

    i should search it in UI/rootForm.aspx

    Monday, November 10, 2008 11:48 PM
  • User-615084531 posted

    hi,

    try this

    btn.Attributes.Add("OnClick", "javascript:window.location.reload('~/rootForm.aspx'");return false;);

    Tuesday, November 11, 2008 12:15 AM
  • User948943842 posted

    That is no good because ~ will only work server side, and this sends it to the client as text; If you need to resolve ~ there is a property on the Page object which will do this, but the solution is really very simple -  you just need to send a correct relative path; it sounds like the page might be two levels down so you will need ..\..\rootForm.aspx.

    Tuesday, November 11, 2008 2:52 AM
  • User798903548 posted

    No bro it still giving the same error it finding the form in a folder and not going to root level:

    The resource cannot be found. UI/Folder/rootForm.aspx

    i should search it in UI/rootForm.aspx

    The question is where is the "UI" folder contained?

    If it is in the application folder, this will work: string windowUrl = CreateWebUrl(this, "UI/Folder/rootForm.aspx")
    Inetpub
         WWWRoot
              AppFolder
                   UI
                     Folder
                         rootForm.aspx

    If it is off of the web site root, that will not work.
    Inetpub
         WWWRoot
              UI
                Folder
                    rootForm.aspx

    You'd need to do something like this:
    string qualifiedFilename = "UI/Folder/rootForm.aspx";
    string windowUrl = string.Format(
         "{0}/{1}",
         Page.Request.Url.GetLeftPart(UriPartial.Authority),
         qualifiedFilename);

    NC...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 11, 2008 7:31 AM