Asp:table from database

Hello, how are you all doing?

I have some information in my database.
I tried to put them on my page using a GridView or a DataList but the layout is not what I want. I tried to fix it but I need something totally different.

I made an asp:table

I want to put in the table something like: for each … <td></>

So I do not want everything out the database but only certain things. Plus I do not want to write a query for each td.

If you’re using WebForms drag the table from the server explorer onto the design view, then click the little action tab, click “Edit Columns” and then “Convert this field into a TemplateField” for each column you wish to edit and set up the layout any way you’d like. I usually make everything a “TemplateField” so that I have better control over the markup.

Or you can just use a Repeater or ListView for this. It will be all your own markup.

Sweet. I didn’t know of such a control. Learn something everyday…

I usually prefer ListView for everything else but if you’re rendering a table a GridView is usually the least work for the same results. :slight_smile:

Also, this would be the only time I ever use the design view and that’s only so that I can get the action tab to make the items into TemplateFields.

Yea, I agree. For custom content i use ListView or Repeater. But it is definitely better to use the GridView control. As it will already output the table for you.

What exactly are you trying to achieve that cannot be done with the GridView?

If you want to do it by yourself using <asp:table> then try this…

#region LoadTableWithRec
public void LoadTableWithRec(string strPageNo)
{

		tblRecordset.Rows.Clear();
		

		if(strPageNo=="1")
		{
			//((LinkButton)sender).ForeColor=System.Drawing.Color.Blue;
			iCurrentPage=(Convert.ToInt32(strPageNo)-1)*Constant.iInitialRecDisplay+1;
			//Response.Write("step1");
		}
		else
		{
			iCurrentPage=(Convert.ToInt32(strPageNo)-1)*Constant.iInitialRecDisplay+1;
			//Response.Write("step2");
		}
	
		trRec=new TableRow();
		
		TblHeader1("10&#37;","Deal Id");
		TblHeader1("15%","Entity Ref #");
		tblRecordset.Rows.Add(trRec);
		TableCell tcRec;

		for(int iRow=(iCurrentPage-1);iRow&lt;(iCurrentPage+Constant.iInitialRecDisplay-1) && iRow&lt;iTotalRec;iRow++)
		{
			{
				trRec=new TableRow();
				for(int iCol=0;iCol&lt;2;iCol++)
				{
					if(iCol==0)
					{
						LinkButton _liDetails=new LinkButton();
						_liDetails.ID=Convert.ToString("RecordDetail"+iRow);
						tcRec=new TableCell();
						tcRec.BorderWidth=new Unit("1px");
						//tcRec.Text=tblRec.Rows[iRow][iCol].ToString();
						_liDetails.Text=tblRec.Rows[iRow][iCol].ToString();
						tcRec.Controls.Add(_liDetails);
						_liDetails.Click+=new EventHandler(LinkToDetailsRec);
						
						trRec.Cells.Add(tcRec);

					}
					else
					{
						
						tcRec=new TableCell();
						tcRec.BorderWidth=new Unit("1px");
						tcRec.Text=tblRec.Rows[iRow][iCol].ToString();
						trRec.Cells.Add(tcRec);
				
					}
				}
				tblRecordset.Rows.Add(trRec);
			
			}
		 }
	}
	#endregion

	#region Link to Detail Record
	public void LinkToDetailsRec(object sender ,EventArgs e)
	{
			string strTemp=((LinkButton)sender).ID;
			
		string strOpenWin="&lt;script&gt;";
		
	//	strOpenWin+="alert('"+strTemp+"')";
		strOpenWin+="alert('"+strTemp+"');window.open('Basic.aspx','Asset','width=350,height=400,left=270,top=180,scrollbars=yes');";
		
		strOpenWin+="&lt;/script&gt;";
		RegisterClientScriptBlock("str",strOpenWin);	
	
		//	((LinkButton)sender).Attributes.Add("onclick","return OpenNewWin();");

		
	}
	#endregion

	#region TblHeader1
	public void  TblHeader1(string width,string HeaderName)
	{
		TableCell tc;
		tc=new TableCell();
		
		tc.Text="&lt;b&gt;"+HeaderName+"&lt;/b&gt;";
		//tc.Width.Value;
		tc.Width = new Unit(width);
		tc.BorderWidth=new Unit("1px");
		tc.CssClass="lightgray";
		trRec.Cells.Add(tc);
		
	}
	#endregion