简体   繁体   中英

after adding querystring parameter to web application method fails

I have the following code that opens a hyperlink in a different frame from code behind using the function ClientScript.RegisterStartupScript. The hyperlink was priory retrieved from a database and assigned to a label.

    public void OpenWindow()
    {
        Formview_CurrentSelectedProcess.DataBind();

        string url = (Formview_CurrentSelectedProcess.FindControl("LabelLink") as Label).Text;
        string s = "window.open('" + url + "', 'main');";
        Test.Text = s;
        ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);
    }

This works perfect. Then I implemented a Querystring on that page. The Request Parameter is passed correctly and the hyperlink is opened on Pageload the way I expected. BUT: the next time this method is called, the hyperlink that will be opened, is the one belonging to the record specified in the Querystring Parameter. I placed a label inside to check if the correct parameter 's' is passed to ClientScript.RegisterStartupScript() and it is!!!

The mistake occurs with that function in case that the page had been loaded with a Querystring Parameter (eg.aspx?ID=324) Loading that same page without that parameter works perfect.

What happens? Why is ClientScript.RegisterStartupScript returning the old result, although it's input parameter has changed?

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
            PopulateRootLevel();
            string GetID = Request.QueryString["ID"];
            if (String.IsNullOrEmpty(GetID))
            {
            }
            else
            {
                InputNodeToOpen.Text = GetID;
                ButtonClick_transmit(button1, EventArgs.Empty);
                InputNodeToOpen.Text ="";
                IDNodesToBeOpened.Text = "";

            }

    }

Any hints on that? Martin

OK. Found the mistake:

After placing all actions in the page_load method inside separate brackets it works. I thought they were already assigned to a no-post-back situation; but they were NOT. The brackets are needed. So it must be:

protected void Page_Load(object sender, EventArgs e)
{

    if (!Page.IsPostBack)
    {
        PopulateRootLevel();
        string GetStructure = Request.QueryString["Structure"];
        if (String.IsNullOrEmpty(GetStructure))
        {
        }
        else
        {
            InputNodeToOpen.Text = GetStructure;
            ButtonClick_transmit(button1, EventArgs.Empty);
            InputNodeToOpen.Text = "";
            IDNodesToBeOpened.Text = "";

        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM