本文介绍了动态表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
1)如何使动态表的每一行都成为后面代码中的超链接?
2)如何在后面的代码中向动态表的每一行添加图像?
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.
推荐答案
这篇关于动态表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!