473,387 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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,
"STR_TRAN_DT")).ToString().Trim().Substring(1, 10) %>'></asp:Literal>--
>
<asp:Literal ID="Literal1" runat="server"
Text='<%# Convert.ToDateTime(DataBinder.Eval(Container.DataI tem,
"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.DataI tem, \"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;
}
}

--------------------------------

Can anyone please help me!.

Apr 27 '07 #1
0 5386

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

Similar topics

3
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum....
1
by: Miguel Dias Moura | last post by:
Hello, I have a GridView in my page which is created in runtime. It works fine. My page has 2 Asp Buttons: - The HIDE button makes GridView.Visible = False; - The SHOW button makes...
0
by: Innova | last post by:
Hi, We are working on a gridview inside the gridview (parent-child) scenario. The data of child grid will depend on the data of parent. Objectives: 1.Add new row in parent grid after each row...
1
by: ram_kri | last post by:
Hi Guys, Here is a uncommon scenario that I had to work on.. I have a GridView which is associated to a DataSource. But I have a couple of columns (item-templates while the rest of the columns...
5
by: NKaufman | last post by:
<asp:GridView ID="Basic" runat="server" DataKeyNames="QuestionID,isHeading" AutoGenerateColumns="false" AllowPaging="true" PageSize="100"> <Columns> <asp:BoundField Visible="false"...
3
by: RobertTheProgrammer | last post by:
Hi folks, I've got another problem. Basically, I'm trying to use a nested GridView, however the nexted GridView displays no values (even though in debug I'm getting valid values into my DataSet. ...
2
by: Danielle | last post by:
All - Thanks in advance for any help you can provide. I've been working with a GridView in Visual Basic for a long time trying to get a list of contacts and to be able to edit and delete the...
13
by: SAL | last post by:
Hello, I'm trying to include a popup in the ItemTemplate of a gridview row. The ItemTemplate for the field contains a textbox and when the user clicks in the textbox I want a popup panel to show...
0
by: dotnetrookie | last post by:
Hi This is my 1st post.I have two checkbox columns in gridview binded through item template. The checkboxes are Category and Subcategory. I have actually removed the duplicate values in the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...

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.