aspnetlogo

Problem:
How to pass values into a usercontrol as querystrings in asp.net.
More References to the problem as as follows:

Solution:

  1. One Can definitely use Session Variables to give the functionality. Set the session variable value in the page and then retrieve the value in the user control.
  2. Another way is to set the Public Property for the userControl.
  3. And one solution is described below which i implemented successfully. It also works for dynamically loaded usercontrol.

The Alternative New Method:

Method is simple:

  1. Add Invisible buttons(or label) to the user control on the page where it will be rendered.
  2. Set the text property of the buttons to the value of the querystring.

    UserControl mycontrol = new UserControl();
    mycontrol = (UserControl)Page.LoadControl(“./UserControls/CustomCallDetailsChart.ascx”);

    Button btnName=new Button();
    btnName.Visible = false;
    btnName.Text=this.Name;
    mycontrol.Controls.AddAt(0, btnName);

    this.Controls.Add(mycontrol);

  3. In the page load event of the usercontrol find the control and typecast the text value to the required datatype.
    protected void Page_Load(object sender, EventArgs e)
    {
    Button btnName = (Button)this.Controls[0];
    this.ChartName = btnName.Text.ToString();
    }

kick it on DotNetKicks.com

Anky Goyal

I am MCPD certified in .NET and working on .NET technologies for past 3yrs. I am very passionate about the new features which get introduced in the technology. I am here to share the new features i get to know in the world of Microsoft. Follow me on twitter @ankygoyal LinkedIN@Ankit Goyal

More Posts - Website

  • Larry Swarts

    This is fantastic!!!

    I’ve been struggle with a problem that this solves for at least a couple days now.

    I’ve run it and it works great.

    When I get the final product up and running I’ll let you know.

    A thousand thousands thanks,
    Larry