Dec 22, 2010

create and Add Dyanmically image,label controll in asp.net

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
//public SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MSKEMSSqlConnectionString"].ConnectionString);

protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = GetData("Select * from sample");

//create control
System.Web.UI.WebControls.Image img1; //Image
Label lbl1;//label for blank spaces

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
lbl1 = new Label();
lbl1.Text = " ";
img1 = new System.Web.UI.WebControls.Image();
img1.ImageUrl = ds.Tables[0].Rows[i].ItemArray[1].ToString().Trim();// give the img path
img1.Height = Unit.Pixel(169);
img1.Width = Unit.Pixel(202);
img1.BorderStyle = BorderStyle.Solid;
img1.BorderWidth = Unit.Pixel(3);
img1.BorderColor = System.Drawing.Color.White;
//add control to place holder
PlaceHolder1.Controls.Add(img1);
PlaceHolder1.Controls.Add(lbl1);
}
}

public DataSet GetData(String strSql)
{
DataSet ds = new DataSet();

SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=tempdb;Persist Security Info=True;User ID=sa;Password=sa;");
SqlDataAdapter da = new SqlDataAdapter(strSql, con);

if (con.State == ConnectionState.Open)
{
con.Close();
}
con.Open();
da.Fill(ds);
con.Close();
return ds;
}



}

No comments:

Post a Comment

What is the use of n-tier architecture and 3-tier architecture?

how to implement 3-tier architecture in asp.net using c#. 3-Tier architecture is also called layered architecture. Some people called it ...