When to use querystring in webpart?
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.
精彩评论