What is of this.Page.Controls.Add method in ASP.Net

Last Reply on May 08, 2013 01:49 AM By Mudassar

Posted on May 08, 2013 01:48 AM
sir what is the use of 
 this.Page.Controls.Add(SqlDataSource1);
in the following code.
 
  protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        SqlDataSource SqlDataSource1 = new SqlDataSource();
        this.Page.Controls.Add(SqlDataSource1);
        SqlDataSource1.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlDataSource1.SelectCommand = "SELECT * FROM users WHERE ElectionId ='" + this.TextBox1.Text.Trim() + "'";
        this.GridView1.DataSource = SqlDataSource1;
        this.GridView1.DataBind();
        this.GridView1.Visible = true;
    }

 

Posted on May 08, 2013 01:49 AM

In order to use any control it must be added to the Page otherwise you won't be able to use it.

Since we are adding dynamic SQL DataSource hence it is required