Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am using a sandboxed solution (sharepoint 2010 project is on office 365 hence using sandboxed solution) and want to go from one page to another on a button click event. This is achieved by javascript but the operations in the click event are not being performed.

For example, I assign the javascript on page load to the desired event then the event performs the redirection without going into the code which is inside the event.

The javascript used for redirection is :

string redirectURL = "http://ksreejit:32512/sites/SplTeam/Pages/QuizMasterDashboard.aspx";
btnCancel.Attributes.Add("OnClick", "javascript:{window.location='" + redirectURL + "';return false;}");

And the event code is:

protected void btnSubmit_Click(object sender, EventArgs e)
        {
        if (ViewState["QuestionID"].ToString() != string.Empty)
        {
            SaveDetails(ViewState["QuestionID"].ToString());
            foreach (Control contrl in this.Controls)
            {
                contrl.Visible = false;
            }

        } 
        else
        {
            SaveDetails();
            foreach (Control contrl in this.Controls)
            {
                contrl.Visible = false;
            }

        }
        Label lblMessage = new Label();
        lblMessage.Visible = true;
        lblMessage.Text = "The Question is successfully saved and sent to reviewer for reviewing. Thanks for uploading.";

}

As you can see the redirection works for btnCancel successfully. I have not assigned it for btnSubmit cos it will then only redirect and will not go to the above specified code. Answers will be appreciated. Thanks.

share|improve this question

2 Answers 2

So basically you need to redirect the page after the event has fired: Try something like this:

    protected void btnSubmit_Click(object sender, EventArgs e)
            {
            if (ViewState["QuestionID"].ToString() != string.Empty)
            {
                SaveDetails(ViewState["QuestionID"].ToString());
                foreach (Control contrl in this.Controls)
                {
                    contrl.Visible = false;
                }

            } 
            else
            {
                SaveDetails();
                foreach (Control contrl in this.Controls)
                {
                    contrl.Visible = false;
                }

            }
            Label lblMessage = new Label();
            lblMessage.Visible = true;
            lblMessage.Text = "The Question is successfully saved and sent to reviewer for reviewing. Thanks for uploading.";

            //Redirect.
            Page.Controls.Add(new LiteralControl("<script>document.write('window.location = newPageUrl.aspx')</script>"));
}
share|improve this answer
    
this did nt work... –  Sree Kuttan Aug 16 '12 at 16:04
1  
This won't work and it's bad practice. The theory however is correct. Combine this answer with stackoverflow.com/questions/503093/… and don't use literals you can instead use a generic tag, define it as a script and place the code inside. Literals shouldn't be used for this. –  Hugh Wood Feb 1 '13 at 9:23

Just add this to your Button Click event.

 Literal ltrl = new Literal();
 ltrl.Text = "<script  language='javascript'>window.location.href='http://www.google.com';</script>";
 this.Controls.Add(ltrl);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.