Asp.Net 2.0 GridView - Creating ItemTemplate at Run Time issue

B

bsm

Hi,

I created GridView and created "BoundColumn" at runtime. When I try to
create TemplateColumn it throws some error. But the same is working in
Design Time Setting.

Design code (Working fine)
---------------------------------------
<asp:TemplateField HeaderText="New col">
<ItemTemplate>
<!--<asp:Literal ID="LControl1" runat="server"
Text='<%# (DataBinder.Eval(Container.DataItem,
<asp:Literal ID="Literal1" runat="server"
Text='<%# Convert.ToDateTime(DataBinder.Eval(Container.DataItem,
"STR_TRAN_DT")).ToString("dd/MM/yyyy") %>'></asp:Literal>
</ItemTemplate>
</asp:TemplateField>

Code in Codebehing file (Throws error)
-------------------------------------------------------

TemplateField TF_Date = new TemplateField();
string strLtrVal = "<%#
Convert.ToDateTime(DataBinder.Eval(Container.DataItem, \"STR_TRAN_DT
\")).ToString(\"dd/MM/yyyy\") %>";
TF_Date.ItemTemplate = new
CreateItemTemplateLtrl("STR_TRAN_DT", strLtrVal, "STR_TRAN_DT");
GridView1.Columns.Add(TF_Date);

CreateTemplateLtrl class file
-----------------------------------------
public class CreateItemTemplateLtrl : ITemplate
{
string strddlName;
string strDataValueField;
string strDataTextField;

//Here, I added the constructor for creating the Literal with
value member and text member.
public CreateItemTemplateLtrl(string LtrlName, string
DataValueField, string DataTextField)
{
this.strDataValueField = DataValueField;
this.strDataTextField = DataTextField;
this.strddlName = LtrlName;
}

//Here is the code for declaring DataBinding event for the Ltrl
being added. This occurs while DataGrid is being bound.
public void InstantiateIn(Control objContainer)
{
Literal _LtrlCon = new Literal();
_LtrlCon.DataBinding += new EventHandler(Ltrl_DataBinding);
objContainer.Controls.Add(_LtrlCon);
}
//Here, you can assign all the properties and every thing else
required for the DDL.

private void Ltrl_DataBinding(object sender, EventArgs e)
{
Literal _LtrlCon = (Literal)sender;
_LtrlCon.ID = strddlName;
//_LtrlCon.Text =
(string)DataBinder.Eval("Container.DataItem", strDataValueField);
_LtrlCon.Text = strDataValueField;
//_LtrlCon.DataTextField = strDataTextField;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top