Thursday, January 13, 2011

dynaic html in main page and also in same page using method using HTML generic Control

 private void getTopSolutions()
    {
        ContentPlaceHolder mpContentPlaceHolder;

        mpContentPlaceHolder =
          (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
        string query;

        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
      
        if (BlogType == "NULL")
        {
             query = "select top 6 * from blog order by bid";
        }
        else
        {
            query = "select top 6 * from blog where type='" + BlogType + "' order by bid";
        }
        SqlCommand cmd = new SqlCommand(query, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            int iii = i + 1;
            HtmlGenericControl dv = (HtmlGenericControl)mpContentPlaceHolder.FindControl("newsdiv" + iii);
            dv.InnerText = ds.Tables[0].Rows[i]["header"].ToString();
        }

        ////newsdiv1.InnerText = "1 )" + ds.Tables[0].Rows[0]["header"].ToString();
        ////newsdiv2.InnerText = "2 )" + ds.Tables[0].Rows[1]["header"].ToString();
        ////newsdiv3.InnerText = "3 )" + ds.Tables[0].Rows[2]["header"].ToString();
        ////newsdiv4.InnerText = "4 )" + ds.Tables[0].Rows[3]["header"].ToString();
        ////newsdiv5.InnerText = "5 )" + ds.Tables[0].Rows[4]["header"].ToString();
        ////newsdiv6.InnerText = "6 )" + ds.Tables[0].Rows[5]["header"].ToString();

    }

    private void getretedSolutions()
    {
        string query;

        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();

        if (BlogType == "NULL")
        {
            query = "select top 6 * from blog order by bid";
        }
        else
        {
            query = "select top 6 * from blog where type='" + BlogType + "' order by bid";
        }
        SqlCommand cmd = new SqlCommand(query, con);
        SqlDataReader da = cmd.ExecuteReader();

    
      while (da.Read())
        {
            HtmlGenericControl dv = new HtmlGenericControl("div");
            dv.Attributes.Add("class", "cs_article");

            HtmlGenericControl h2 = new HtmlGenericControl("h2");

            HtmlAnchor a1 = new HtmlAnchor();
            a1.InnerText = da["header"].ToString();
            a1.HRef = "Solution.aspx?TopicId=" + da["bid"].ToString();
            h2.Controls.Add(a1);

            HtmlGenericControl p = new HtmlGenericControl("p");
            p.InnerText = da["matter"].ToString();


            HtmlAnchor a11 = new HtmlAnchor();
            a11.InnerText = "read more";
            a11.HRef = "Solution.aspx?TopicId=" + da["bid"].ToString();
        

            dv.Controls.Add(h2);
            dv.Controls.Add(p);
            dv.Controls.Add(a11);
            bddv.Controls.Add(dv);

No comments:

SQL Optimization

  SQL Optimization  1. Add where on your query  2. If you remove some data after the data return then remove the remove condition in the sel...