Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use querystring in webpart?

Hi I am currently developing a webpart 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

like image 492
Daniel Avatar asked Aug 08 '26 20:08

Daniel


1 Answers

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.

like image 61
Alexandre Machado Avatar answered Aug 11 '26 15:08

Alexandre Machado