Click here to Skip to main content
14,883,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
1)How to make each row of a dynamic table as hyperlink in code behind ?
2)How to add image to each row of a dynamic table in code behind ?
protected void BindDataInTable()
        {
            try
            {
                string strCountryName = "'India'";
                SqlCommand objSqlCommand = new SqlCommand("select * from TBL_COUNTRYINFO where CountryName=" + strCountryName, objConnection);
                SqlDataAdapter objSqlDataAdapter = new SqlDataAdapter();
                DataSet objDataSet = new DataSet();
                objSqlDataAdapter.SelectCommand = objSqlCommand;
                objSqlDataAdapter.Fill(objDataSet, "TBL_COUNTRYINFO");
                DataTable objDataTable = new DataTable();
                objDataTable = objDataSet.Tables["TBL_COUNTRYINFO"];
                Image objImage = new Image();
                Image objArrowImage = new Image();
                //HtmlGenericControl spnImage = new HtmlGenericControl();

                #region Add Header
                TableHeaderRow trHeader = new TableHeaderRow();
                TableHeaderCell trHCell1 = new TableHeaderCell();
                trHeader.Cells.Add(trHCell1);
                TableHeaderCell trHCell2 = new TableHeaderCell();
                trHeader.Cells.Add(trHCell2);
                TableHeaderCell trHCell3 = new TableHeaderCell();
                trHeader.Cells.Add(trHCell3);
                TableHeaderCell trHCell4 = new TableHeaderCell();
                trHeader.Cells.Add(trHCell4);
                #endregion 

                #region Add Rows
                TableRow trDataRow = null;
                TableRow imgRow = null;
                TableCell trCellArrowImage = null;
                TableCell trCellLocalOffice = null;
                TableCell trCellAddress = null;
                TableCell trCellContact = null;
                TableCell trCellImage = null;
                if (objDataTable != null)
                {
                    
                    foreach (DataRow dr in objDataTable.Rows)
                    {
                        HyperLink hlLocalOffice = new HyperLink();
                        HyperLink hlAddress = new HyperLink();
                        HyperLink hlContact = new HyperLink();
                        trDataRow = new TableRow();
                        //cell1
                        trCellArrowImage = new TableCell();
                        //trCellArrowImage.Width = 10;                       
                        objArrowImage.ImageUrl = "arrow.JPG";
                        trCellArrowImage.Controls.Add(objArrowImage);
                        trDataRow.Cells.Add(trCellArrowImage);                        
                        //cell 2                       
                        trCellLocalOffice = new TableCell();
                        hlLocalOffice.Text = (dr["LocalOffice"].ToString());
                        hlLocalOffice.NavigateUrl = "";
                        trCellLocalOffice.Controls.Add(hlLocalOffice);
                        trDataRow.Cells.Add(trCellLocalOffice);
                        //cell 3                       
                        trCellAddress = new TableCell();
                        hlAddress.Text = (dr["Address"].ToString());
                        hlAddress.NavigateUrl = "";
                        trCellAddress.Controls.Add(hlAddress);
                        trDataRow.Cells.Add(trCellAddress);
                        //cell 4                      
                        trCellContact = new TableCell();
                        hlContact.Text = (dr["Contact"].ToString());
                        hlContact.NavigateUrl = "";
                        trCellContact.Controls.Add(hlContact);
                        trDataRow.Cells.Add(trCellContact);                        
                        //new row for bind image line                     
                        imgRow = new TableRow();
                        //spnImage.InnerHtml = "<image src='fade line-1.JPG';alt='imageline'/>";
                        trCellImage = new TableCell();                   
                        objImage.ImageUrl = "fade line-1.JPG";
                        trCellImage.Controls.Add(objImage);
                        trCellImage.ColumnSpan = 3;
                        //trDataRow.Controls.Add(spnImage);
                        imgRow.Cells.Add(trCellImage);
                        this.tblCountryInfo.Rows.Add(trDataRow);
                        this.tblCountryInfo.Rows.Add(imgRow);
                     }
                  }
                #endregion
            }
            catch (Exception)
            {
                throw;
            }           
        }

Here in the code i need to add image and hyperlink.
Posted
Updated 20-Jan-11 20:33pm
v2
Comments
m@dhu 21-Jan-11 2:33am
   
wrapped the code.
OriginalGriff 21-Jan-11 3:01am
   
Just a style thing: Don't use #region directives to break up a methods code: if you need to break it up into smaller chunks, then extract the code blocks to methods. This makes it more readable, and easier to document and follow.

1 solution

Hi,
Why don't use a GridView and make some template columns to add hyperlinks...
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900