Hi, i'm new to this and I've been stuck for a while on this one.
I would like to retreive the data key (au_id) when the rowType = DataRow, so I can do a Sub query with the datakey Id.
thank you for your help.
I'm using this Microsoft Example:
<%@ Page language="C#" %> <script runat="server">
// Create a template class to represent a dynamic template column. publicclass GridViewTemplate : ITemplate
{ private DataControlRowType templateType; privatestring columnName;
publicvoid InstantiateIn(System.Web.UI.Control container)
{ // Create the content for the different row types. switch(templateType)
{ case DataControlRowType.Header: // Create the controls to put in the header // section and set their properties.
Literal lc = new Literal();
lc.Text = "<B>" + columnName +
"</B>";
// Add the controls to the Controls collection // of the container.
container.Controls.Add(lc); break; case DataControlRowType.DataRow:
// GET au_id DataKey Here
// Create the controls to put in a data row // section and set their properties.
Label firstName = new Label();
Label lastName = new Label();
Literal spacer = new Literal();
spacer.Text = " ";
// To support data binding, register the event-handling methods // to perform the data binding. Each control needs its own event // handler.
firstName.DataBinding += new EventHandler(this.FirstName_DataBinding);
lastName.DataBinding += new EventHandler(this.LastName_DataBinding);
// Add the controls to the Controls collection // of the container.
container.Controls.Add(firstName);
container.Controls.Add (spacer);
container.Controls.Add(lastName); break;
// Insert cases to create the content for the other
// row types, if desired.
privatevoid FirstName_DataBinding(Object sender, EventArgs e)
{ // Get the Label control to bind the value. The Label control // is contained in the object that raised the DataBinding
// event (the sender parameter).
Label l = (Label)sender;
// Get the GridViewRow object that contains the Label control.
GridViewRow row = (GridViewRow)l.NamingContainer;
// Get the field value from the GridViewRow object and
// assign it to the Text property of the Label control.
l.Text = DataBinder.Eval(row.DataItem, "au_fname").ToString();
}
privatevoid LastName_DataBinding(Object sender, EventArgs e)
{ // Get the Label control to bind the value. The Label control // is contained in the object that raised the DataBinding
// event (the sender parameter).
Label l = (Label)sender;
// Get the GridViewRow object that contains the Label control.
GridViewRow row = (GridViewRow)l.NamingContainer;
// Get the field value from the GridViewRow object and
// assign it to the Text property of the Label control.
l.Text = DataBinder.Eval(row.DataItem, "au_lname").ToString();
}
}
void Page_Load(Object sender, EventArgs e)
{
// The field columns need to be created only when the page is // first loaded. if (!IsPostBack)
{ // Dynamically create field columns to display the desired // fields from the data source. Create a TemplateField object
// to display an author's first and last name.
TemplateField customField = new TemplateField();
// Create the dynamic templates and assign them to
// the appropriate template property.
customField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow,
"Author Name");
customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header,
"Author Name");
// Add the field column to the Columns collection of the // GridView control.
AuthorsGridView.Columns.Add(customField);
}
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Pubs sample database. -->
<asp:sqldatasource id="AuthorsSqlDataSource"
selectcommand="SELECT [au_id], [au_fname], [au_lname] FROM [authors]"
connectionstring="server=localhost;database=pubs;integrated security=SSPI"
runat="server">
</asp:sqldatasource>
I don't think you can get the DataItem there in that InstantiateIn Method. You have to add DataBinding Handler for the control and get the value from there
public void InstantiateIn(System.Web.UI.Control container)
{
DataControlRowType templateType = DataControlRowType.DataRow;
// Create the content for the different row types.
switch (templateType)
{
case DataControlRowType.Header:
// Create the controls to put in the header
break;
case DataControlRowType.DataRow:
// Create the controls to put in a data row
Label lblName = new Label();
lblName.DataBinding += new EventHandler(lblName_DataBinding);
container.Controls.Add(lblName);
break;
case DataControlRowType.Footer:
default:
// Insert code to handle unexpected values.
break;
}
}
void lblName_DataBinding(object sender, EventArgs e)
{
Label lblName = (Label)sender;
GridViewRow container = (GridViewRow)lblName.NamingContainer;
lblName.Text = DataBinder.Eval(container.DataItem, "my column name here").ToString();
}
Hi Santhosh,
i've added your code and now i'm getting this error
InvalidCastException was unhandled by user code (Unable to cast an objet of type 'System.Web.UI.WebControls.DataControlFieldCell' to a type 'System.Web.UI.WebControls.GridViewRow'.)
I would like to retreive the data key (au_id) when the rowType = DataRow, so I can do a Sub query with the datakey Id.
You want to do your query in the RowDataBound event of the GridView instead (OR, do a join in the "original" query for the GridView itself). The code you're using simply creates
a templatefield (a couple of labels making up the "Author Name" column) and ensures data for those controls gets bound (the comments there do explain).
Hi Peter,
your right, at the moment my code is basic.
here's what i'm trying to do, I have a gridview with 1 column called name, that will never change. I'm using the 'GridViewTemplate' to add dynamic column based on a query. Inside each DataRow for the dynamic column I would like to run subqueries based on
the DataKey.
I don't know if there's an other way of doing this.
heyyou
0 Points
5 Posts
How do I get the datakey for a DataControlRowType.DataRow when a TemplateField is created in code...
Dec 12, 2010 04:28 PM|LINK
Hi, i'm new to this and I've been stuck for a while on this one.
I would like to retreive the data key (au_id) when the rowType = DataRow, so I can do a Sub query with the datakey Id.
thank you for your help.
I'm using this Microsoft Example:
<%@ Page language="C#" %> <script runat="server">
// Create a template class to represent a dynamic template column.
public class GridViewTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnName;
public GridViewTemplate(DataControlRowType type, string colname)
{
templateType = type;
columnName = colname;
}
public void InstantiateIn(System.Web.UI.Control container)
{
// Create the content for the different row types.
switch(templateType)
{
case DataControlRowType.Header:
// Create the controls to put in the header
// section and set their properties.
Literal lc = new Literal();
lc.Text = "<B>" + columnName + "</B>";
// Add the controls to the Controls collection
// of the container.
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
// GET au_id DataKey Here
// Create the controls to put in a data row
// section and set their properties.
Label firstName = new Label();
Label lastName = new Label();
Literal spacer = new Literal();
spacer.Text = " ";
// To support data binding, register the event-handling methods
// to perform the data binding. Each control needs its own event
// handler.
firstName.DataBinding += new EventHandler(this.FirstName_DataBinding);
lastName.DataBinding += new EventHandler(this.LastName_DataBinding);
// Add the controls to the Controls collection
// of the container.
container.Controls.Add(firstName);
container.Controls.Add (spacer);
container.Controls.Add(lastName);
break;
// Insert cases to create the content for the other
// row types, if desired.
default:
// Insert code to handle unexpected values.
break;
}
}
private void FirstName_DataBinding(Object sender, EventArgs e)
{
// Get the Label control to bind the value. The Label control
// is contained in the object that raised the DataBinding
// event (the sender parameter).
Label l = (Label)sender;
// Get the GridViewRow object that contains the Label control.
GridViewRow row = (GridViewRow)l.NamingContainer;
// Get the field value from the GridViewRow object and
// assign it to the Text property of the Label control.
l.Text = DataBinder.Eval(row.DataItem, "au_fname").ToString();
}
private void LastName_DataBinding(Object sender, EventArgs e)
{
// Get the Label control to bind the value. The Label control
// is contained in the object that raised the DataBinding
// event (the sender parameter).
Label l = (Label)sender;
// Get the GridViewRow object that contains the Label control.
GridViewRow row = (GridViewRow)l.NamingContainer;
// Get the field value from the GridViewRow object and
// assign it to the Text property of the Label control.
l.Text = DataBinder.Eval(row.DataItem, "au_lname").ToString();
}
}
void Page_Load(Object sender, EventArgs e)
{
// The field columns need to be created only when the page is
// first loaded.
if (!IsPostBack)
{
// Dynamically create field columns to display the desired
// fields from the data source. Create a TemplateField object
// to display an author's first and last name.
TemplateField customField = new TemplateField();
// Create the dynamic templates and assign them to
// the appropriate template property.
customField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Author Name");
customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "Author Name");
// Add the field column to the Columns collection of the
// GridView control.
AuthorsGridView.Columns.Add(customField);
}
}
</script>
<html>
<body>
<form runat="server">
<h3>TemplateField Constructor Example</h3>
<asp:gridview id="AuthorsGridView"
datasourceid="AuthorsSqlDataSource"
autogeneratecolumns="False"
datakeynames="au_id"
runat="server">
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Pubs sample database. -->
<asp:sqldatasource id="AuthorsSqlDataSource"
selectcommand="SELECT [au_id], [au_fname], [au_lname] FROM [authors]"
connectionstring="server=localhost;database=pubs;integrated security=SSPI"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
Item Template <C#> GridView TemplateField <asp.net>
sansan
All-Star
53942 Points
8147 Posts
Re: How do I get the datakey for a DataControlRowType.DataRow when a TemplateField is created in ...
Dec 12, 2010 06:42 PM|LINK
You can get DataKey as
You may get the row index as row.RowIndex in your code.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How do I get the datakey for a DataControlRowType.DataRow when a TemplateField is created in ...
Dec 14, 2010 02:38 AM|LINK
Yes, please don't forget to set DataKeyNames property for your GridView.
heyyou
0 Points
5 Posts
Re: How do I get the datakey for a DataControlRowType.DataRow when a TemplateField is created in ...
Dec 14, 2010 10:52 PM|LINK
Hi Decker dong,
I have set the dataeynames for the gridview,
but I can't retreive the datakey in the 'case DataControlRowType.DataRow:'
sansan
All-Star
53942 Points
8147 Posts
Re: How do I get the datakey for a DataControlRowType.DataRow when a TemplateField is created in ...
Dec 14, 2010 11:05 PM|LINK
Get the value from the DataItem Directly
heyyou
0 Points
5 Posts
Re: How do I get the datakey for a DataControlRowType.DataRow when a TemplateField is created in ...
Dec 14, 2010 11:35 PM|LINK
Hi Santhosh,
thank you for your reply.
here's what i did, but i'm getting this error (the name 'e' does not exist in the current context)
public void InstantiateIn(System.Web.UI.Control container)
{
// Create the content for the different row types.
switch (templateType)
{
case DataControlRowType.Header:
// Create the controls to put in the header
break;
case DataControlRowType.DataRow:
// Create the controls to put in a data row
string dataKeyValue = DataBinder.Eval(e.Row.DataItem, "au_id").ToString();
break;
case DataControlRowType.Footer:
default:
// Insert code to handle unexpected values.
break;
}
}
sansan
All-Star
53942 Points
8147 Posts
Re: How do I get the datakey for a DataControlRowType.DataRow when a TemplateField is created in ...
Dec 14, 2010 11:47 PM|LINK
I don't think you can get the DataItem there in that InstantiateIn Method. You have to add DataBinding Handler for the control and get the value from there
public void InstantiateIn(System.Web.UI.Control container) { DataControlRowType templateType = DataControlRowType.DataRow; // Create the content for the different row types. switch (templateType) { case DataControlRowType.Header: // Create the controls to put in the header break; case DataControlRowType.DataRow: // Create the controls to put in a data row Label lblName = new Label(); lblName.DataBinding += new EventHandler(lblName_DataBinding); container.Controls.Add(lblName); break; case DataControlRowType.Footer: default: // Insert code to handle unexpected values. break; } } void lblName_DataBinding(object sender, EventArgs e) { Label lblName = (Label)sender; GridViewRow container = (GridViewRow)lblName.NamingContainer; lblName.Text = DataBinder.Eval(container.DataItem, "my column name here").ToString(); }Probably, you can try
heyyou
0 Points
5 Posts
Re: How do I get the datakey for a DataControlRowType.DataRow when a TemplateField is created in ...
Dec 15, 2010 12:06 AM|LINK
Hi Santhosh,
i've added your code and now i'm getting this error
InvalidCastException was unhandled by user code (Unable to cast an objet of type 'System.Web.UI.WebControls.DataControlFieldCell' to a type 'System.Web.UI.WebControls.GridViewRow'.)
Alain
PeteNet
All-Star
81342 Points
11398 Posts
Re: How do I get the datakey for a DataControlRowType.DataRow when a TemplateField is created in ...
Dec 15, 2010 12:19 AM|LINK
You want to do your query in the RowDataBound event of the GridView instead (OR, do a join in the "original" query for the GridView itself). The code you're using simply creates a templatefield (a couple of labels making up the "Author Name" column) and ensures data for those controls gets bound (the comments there do explain).
Peter
heyyou
0 Points
5 Posts
Re: How do I get the datakey for a DataControlRowType.DataRow when a TemplateField is created in ...
Dec 15, 2010 01:00 AM|LINK
Hi Peter,
your right, at the moment my code is basic.
here's what i'm trying to do, I have a gridview with 1 column called name, that will never change. I'm using the 'GridViewTemplate' to add dynamic column based on a query. Inside each DataRow for the dynamic column I would like to run subqueries based on the DataKey.
I don't know if there's an other way of doing this.
Alain