473,666 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using TemplateColumn and DataGrid in code

Hi

I need to be able to handle the following ASP programming in pure C# code:

<asp:TemplateCo lumn HeaderText="Cus tomer Information">
<ItemTemplate >
<table border="0">
<tr>
<td align="right">< b>Name:</b></td>
<td><%# DataBinder.Eval (Container.Data Item, "name") %></td>
</tr>
<tr>
<td align="right">< b>ID number:</b></td>
<td><%# DataBinder.Eval (Container.Data Item, "customerID ") %></td>
</tr>
<tr>
<td align="right">< b>E-mail:</b></td>
<td><%# DataBinder.Eval (Container.Data Item, "email") %>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateCol umn>

I want to be able to create TemplateColumns that contain some HTML and be
able to insert multiple values from my datasource at runtime.

The reason for this is I want it to be part of a WebPart, where the DataGrid
is added through the CreateChildCont rols() method.
Aug 4 '06 #1
2 2448
Finally I also found a solution to this, posting it here for reference if
anyone else ever needs to do something similar. I involves using
TemplateColumn and DataGrid to output several generated rows with presence
information.

For doing this in code, like I did, this link explains a lot of the code in
using the TemplateColumn:
http://www.superdotnet.com/Article.aspx?ArticleID=116

To get the E-mail for presence detection and name for display from the
datasource, the following example can be used (C#):

public class MyColumnTemplat e : ITemplate
{
public void InstantiateIn(C ontrol container)
{
Label name = new Label();
name.DataBindin g += new EventHandler(my DatSource_DataB inding);
container.Contr ols.Add(name);
}

public void myDatSource_Dat aBinding(object sender, EventArgs e)
{
Label name = ((Label)(sender ));
DataGridItem container = ((DataGridItem) (name.NamingCon tainer));
string email =
Convert.ToStrin g(DataBinder.Ev al((((DataGridI tem)(container) )).DataItem,
"eMail"));
}
}//End class
This is a simple example to show how this would work. Here you only see the
"name" attribute as the output. The rest would just be added just like this
is.
Notice that you need to add an event handler that gets the source to make
this work, something that ASP.NET does automatically.
"MrCrool" wrote:
Hi

I need to be able to handle the following ASP programming in pure C# code:

<asp:TemplateCo lumn HeaderText="Cus tomer Information">
<ItemTemplate >
<table border="0">
<tr>
<td align="right">< b>Name:</b></td>
<td><%# DataBinder.Eval (Container.Data Item, "name") %></td>
</tr>
<tr>
<td align="right">< b>ID number:</b></td>
<td><%# DataBinder.Eval (Container.Data Item, "customerID ") %></td>
</tr>
<tr>
<td align="right">< b>E-mail:</b></td>
<td><%# DataBinder.Eval (Container.Data Item, "email") %>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateCol umn>

I want to be able to create TemplateColumns that contain some HTML and be
able to insert multiple values from my datasource at runtime.

The reason for this is I want it to be part of a WebPart, where the DataGrid
is added through the CreateChildCont rols() method.
Aug 7 '06 #2
Actually, an even better example would be like this:
public class MyColumnTemplat e : ITemplate
{
public void InstantiateIn(C ontrol container)
{
LiteralControl ouputLiteral = new LiteralControl( );
ouputLiteral.Da taBinding += new EventHandler(my Literal_DataBin ding);
container.Contr ols.Add(ouputLi teral);
}

public void myLiteral_DataB inding(object sender, EventArgs e)
{
LiteralControl myLiteral = ((LiteralContro l)(sender));
DataGridItem container = ((DataGridItem) (myLiteral.Nami ngContainer));
string email =
Convert.ToStrin g(DataBinder.Ev al((((DataGridI tem)(container) )).DataItem,
"eMail"));
string name =
Convert.ToStrin g(DataBinder.Ev al((((DataGridI tem)(container) )).DataItem,
"contactName")) ;
string idnumber =
Convert.ToStrin g(DataBinder.Ev al((((DataGridI tem)(container) )).DataItem,
"idNumber") );
string strExactOutput;

strExactOutput = "" +
"<table border=\"0\">" +
"<tr>" +
"<td align=\"right\" ><b>Name:</b></td>" +
"<td>" + name + "</td>" +
"</tr>" +
"<tr>" +
"<td align=\"right\" ><b>ID number:</b></td>" +
"<td>" + idnumber + "</td>" +
"</tr>" +
"<tr>" +
"<td align=\"right\" ><b>E-mail:</b></td>" +
"<td>" + email +
"</td>" +
"</tr>" +
"</table>";

myLiteral.Text = strExactOutput;
}
}//End class
"MrCrool" wrote:
Finally I also found a solution to this, posting it here for reference if
anyone else ever needs to do something similar. I involves using
TemplateColumn and DataGrid to output several generated rows with presence
information.

For doing this in code, like I did, this link explains a lot of the code in
using the TemplateColumn:
http://www.superdotnet.com/Article.aspx?ArticleID=116

To get the E-mail for presence detection and name for display from the
datasource, the following example can be used (C#):

public class MyColumnTemplat e : ITemplate
{
public void InstantiateIn(C ontrol container)
{
Label name = new Label();
name.DataBindin g += new EventHandler(my DatSource_DataB inding);
container.Contr ols.Add(name);
}

public void myDatSource_Dat aBinding(object sender, EventArgs e)
{
Label name = ((Label)(sender ));
DataGridItem container = ((DataGridItem) (name.NamingCon tainer));
string email =
Convert.ToStrin g(DataBinder.Ev al((((DataGridI tem)(container) )).DataItem,
"eMail"));
}
}//End class
This is a simple example to show how this would work. Here you only see the
"name" attribute as the output. The rest would just be added just like this
is.
Notice that you need to add an event handler that gets the source to make
this work, something that ASP.NET does automatically.
"MrCrool" wrote:
Hi

I need to be able to handle the following ASP programming in pure C# code:

<asp:TemplateCo lumn HeaderText="Cus tomer Information">
<ItemTemplate >
<table border="0">
<tr>
<td align="right">< b>Name:</b></td>
<td><%# DataBinder.Eval (Container.Data Item, "name") %></td>
</tr>
<tr>
<td align="right">< b>ID number:</b></td>
<td><%# DataBinder.Eval (Container.Data Item, "customerID ") %></td>
</tr>
<tr>
<td align="right">< b>E-mail:</b></td>
<td><%# DataBinder.Eval (Container.Data Item, "email") %>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateCol umn>

I want to be able to create TemplateColumns that contain some HTML and be
able to insert multiple values from my datasource at runtime.

The reason for this is I want it to be part of a WebPart, where the DataGrid
is added through the CreateChildCont rols() method.
Aug 7 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
6427
by: Michelle Keys | last post by:
I am trying to call a print function to print a string from a database using javascript. Which is RC_DATA of Varchar2(2500). This is a javascript is not being used. I have a thing that needs to be modified: if(e.CommandName =="Print") { string parsedreceipt = null; parsedreceipt = DecodeReceipt (e.Item.Cells.Text); Session = parsedreceipt;
1
1429
by: Randy | last post by:
Hello, I've got an ASP app. I have a simple datagrid. If I don't use column templates I can easily set up the datagrid to use paging by creating a PageIndexChanged event and putting this code in it... DataGrid1.CurrentPageIndex = e.NewPageIndex; DataGrid1.DataBind(); This works fine. But when I then use column styles for the datagrid using Labels in the item template slot, when I click the next page link at the bottom of the datagrid,...
2
2118
by: Ricardo | last post by:
Hi, How can I insert a TemplateColumn on a Datagrid on the fly? Basically I'm after how to create a TemplateColumn, add a button and a label to its control list and add the TemplateColumn to my Datagrid. Something like: Dim tc as New TemplateColumn
2
5035
by: zambizzi | last post by:
....I can't seem to get my hands on a control I'm loading in an editable datagrid. Here's my datagrid control: <asp:datagrid id="GLRulesGrid" runat="server" autogeneratecolumns="False" oneditcommand="GLRulesGrid_Edit"
2
6410
by: Nu2ASP.NET | last post by:
What I am trying to do is essentially 'flip' the bits, when the user clicks in the checkbox. For example, if the CheckBox appears checked, and the user un-checks it, I want the underlying data field to change from a "1" to a "0" (and vica versa). I have the bindings worked out, I just can't figure out how to do the update. Here is my source:
0
1173
by: Vagabond Software | last post by:
I am fairly new to ASP.NET, so bear with me... I have a Datagrid with a data-bound DropDownList in the TemplateColumn. Here is the HTML code: <asp:TemplateColumn HeaderText="Void"> <ItemStyle horizontalalign="Left" wrap="False"></ItemStyle> <ItemTemplate> <%#Databinder.Eval(Container.DataItem, "isVoid")%> </ItemTemplate>
1
2117
by: Brett Wesoloski | last post by:
I am new to using template columns. I am just trying to create a data grid with a bound column and another column with a imagebutton in it. What I have always done in the past was to then create a datatable and bind that to the datagrid. Where is my code to bind the grid to the datatable. DataTable dt = new DataTable(); dt.Columns.Add("FundDescription");
3
7376
by: rn5a | last post by:
A DataGrid has the following TemplateColumn: <asp:DataGrid ID="dgCart"...OnSortCommand="SortGrid" AllowSorting="true"....> <Column> <asp:TemplateColumn HeaderImageUrl="Images\Up.gif" HeaderText="ID" SortExpression="PID"> <ItemTemplate> <asp:Label ID="lblPID" Text=<%# Container.DataItem("PID") %> runat="server"/>
2
3279
by: =?Utf-8?B?RXVnZW5l?= | last post by:
Hi, I use a datagrid with asp:templatecolumn, and handles the datagrid's ItemDataBound event, whereby I assign a value to this asp:templatecolumn text. e.Item.Cells.Text = e.Item.Cells.Text + e.Item.Cells.Text; /* column index 2 and 3 are boundcolumn */ The issue that I have is that the column header (index 1) would be blank after it.
0
8443
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8550
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.