简体   繁体   English

将querystring参数添加到Web应用程序方法后失败

[英]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. 我有以下代码,使用功能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. 然后,我在该页面上实现了一个Querystring。 The Request Parameter is passed correctly and the hyperlink is opened on Pageload the way I expected. 正确传递了请求参数,并以预期的方式在Pageload上打开了超链接。 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. 但是:下次调用此方法时,将打开的超链接是属于Querystring参数中指定的记录的超链接。 I placed a label inside to check if the correct parameter 's' is passed to ClientScript.RegisterStartupScript() and it is!!! 我在其中放置了一个标签,以检查是否将正确的参数s传递给ClientScript.RegisterStartupScript(),并且它是正确的!

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. 如果该页面已加载了Querystring参数(例如,aspx?ID = 324),则该函数会发生错误,而无需该参数的情况下加载同一页面的效果很好。

What happens? 怎么了? Why is ClientScript.RegisterStartupScript returning the old result, although it's input parameter has changed? 尽管输入参数已更改,为什么ClientScript.RegisterStartupScript返回旧结果?

    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. 将所有动作放在page_load方法中的单独括号内后,它可以工作。 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 = "";

        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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