开发者

When to use querystring in webpart?

开发者 https://www.devze.com 2023-02-20 18:09 出处:网络
Hi I am currently developing a w开发者_JAVA百科ebpart in which I read a Querystring variable, however when I try to read it in the CreateChildControls method (because some of the controls are created

Hi I am currently developing a w开发者_JAVA百科ebpart in which I read a Querystring variable, however when I try to read it in the CreateChildControls method (because some of the controls are created depending on this variable) it has a null value. If i read it in a postback, it works fine.

Is there a way to create the controls depending on a querystring variable?

Thanks in advance


You could declare your control outside the CreateChildControls method and override the render method to instance it.

private Label label;
protected override void Render(HtmlTextWriter writer)
{        
     if (Page.Request.QueryString["PageView"] != null)
     {
         label = new Label();
         label.Text = Page.Request.QueryString["PageView"];
         this.Controls.Add(label);
     }   
     base.Render(writer);
}

EDIT: I made some more tests and got it working with this code

    protected override void CreateChildControls()
    {
        Controls.Add(new LiteralControl(Page.Request.QueryString["PageView"]));
    }

The strange thing about it is that all of a sudden I can't reproduce your problem anymore.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号