473,395 Members | 1,763 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,395 software developers and data experts.

Creating TemplateColumns dynamically

I found I can create Template columns dynamically - as long as I don't use
objects that need onclick events, such as a LinkButton. Textboxes and
Labels work fine.

I create the Template columns like so:

Dim column as TemplateColumn = new TemplateColumn()
column.HeaderText = "Template Column"
column.ItemStyle.Width = Unit.Pixel(width)
column.HeaderStyle.Width = Unit.Pixel(width)
column.ItemTemplate = Page.LoadTemplate("testTEmplateColumn2.ascx")
' column.OnClick="Resume_Submit"

This seems to work fine (notice I have the OnClick commented out)

The .ascx file is:

<%@ Language="VB" %>
<asp:LinkButton id="ResumeDate"
text="test"
visible="true"
width="80px"
runat="server"/>

As it is it works fine - but doesn't do anything.

If I added:

OnClick="Resume_Submit"

I get the error:

'resume_submit' is not a member of 'ASP.testTemplateColumn2_ascx'.

So I tried to do it where I create the TemplateColumn (the line that is
commented out). If I uncomment it I get:

'OnClick' is not a member of 'System.Web.UI.WebControls.TemplateColumn'.

This makes sense as the ID is on the LinkButton and not the TemplateColumn -
How would I access it from the TemplateColumn to set it?

Thanks,

Tom
Aug 29 '07 #1
5 2280
On Aug 30, 12:02 am, "tshad" <t...@home.comwrote:
If I added:

OnClick="Resume_Submit"

I get the error:

'resume_submit' is not a member of 'ASP.testTemplateColumn2_ascx'.
You should create it, I guess

>
So I tried to do it where I create the TemplateColumn (the line that is
commented out). If I uncomment it I get:

'OnClick' is not a member of 'System.Web.UI.WebControls.TemplateColumn'.
It's true, there is no 'OnClick' method

How would I access it from the TemplateColumn to set it?
First of all you should decide what you need to have. "OnClick" for
the LinkButton control is not the same with "OnClick" for a whole
column of the grid(?) control. When you want to load control with
Page.LoadTemplate then you shoud assign the onclick method in
testTEmplateColumn2.ascx.

Aug 29 '07 #2
Actually, I am thinking about giving up trying to create the DataGrid
Dynamically. The template seems more trouble than it is worth. I can
create the template fine, but you have to use an ascx file to get it to
work.

"Alexey Smirnov" <al************@gmail.comwrote in message
news:11*********************@y42g2000hsy.googlegro ups.com...
On Aug 30, 12:02 am, "tshad" <t...@home.comwrote:
>If I added:

OnClick="Resume_Submit"

I get the error:

'resume_submit' is not a member of 'ASP.testTemplateColumn2_ascx'.

You should create it, I guess
The problem is that the ascx file is nothing more than the Template code
inside the TemplateColumn tags:

<%@ Language="VB" %>
<asp:LinkButton id="ResumeDate"
text="test"
visible="true"
width="80px"
runat="server"/>

There is no code here.

This works fine but does nothing, really.
>
>>
So I tried to do it where I create the TemplateColumn (the line that is
commented out). If I uncomment it I get:

'OnClick' is not a member of 'System.Web.UI.WebControls.TemplateColumn'.

It's true, there is no 'OnClick' method
Right. For the TemplateColumn there isn't one and the problem is trying to
get access to the LinkButton I just created.
>
>How would I access it from the TemplateColumn to set it?

First of all you should decide what you need to have. "OnClick" for
the LinkButton control is not the same with "OnClick" for a whole
column of the grid(?) control. When you want to load control with
Page.LoadTemplate then you shoud assign the onclick method in
testTEmplateColumn2.ascx.
Can't assign it in the .ascx file.

Espisito mentions that you can't do this. You need to get to the access to
the LinkButton directly. You have to grab it as it is being created on the
ItemCreated event. The problem is you can't do:

DataGrid1.onItemCreated = "ItemCreated"

This will give you an error:

'System.Web.UI.WebControls.DataGrid.Protected Overridable Sub
OnItemCreated(e As System.Web.UI.WebControls.DataGridItemEventArgs)' is not
accessible in this context

Rock in a hard place problem.

I realized looking at Espisitos article, he was creating the DataGrid in the
..aspx page and then adding the Controls later. So his article didn't really
help me much.

He also doesn't mention that you need to create the DataGrid in the
Page_Init event because on the PostBack it won't be there. Apparently, the
viewstate keeps the data for the columns but not the columns themselves, so
you have to recreate columns before the page gets the data from the
viewstate to populate the DataGrid. If you don't you have nothing where the
DataGrid is supposed to be.

The problem is that I am building the datagrid from my Sql Statement that I
fill my DataTable with and using the DataTables Columns to build the
DataGrids columns. So to recreate the DataGrid I would need to re-read my
data from Sql on each Post Back. I think it is probably easier to just
build the DataGrid with all its columns and TemplateColumns and then just
move the columns where I want in the Page_Init event.

Been an experiance.

Thanks,

Tom
Aug 29 '07 #3
On Aug 30, 1:27 am, "tshad" <t...@home.comwrote:
Actually, I am thinking about giving up trying to create the DataGrid
Dynamically. The template seems more trouble than it is worth. I can
create the template fine, but you have to use an ascx file to get it to
work.
Why do you need to use an ascx file? You can add a linkbutton
dynamically

Dim lb As LinkButton = new LinkButton
....

Aug 30 '07 #4
"Alexey Smirnov" <al************@gmail.comwrote in message
news:11**********************@50g2000hsm.googlegro ups.com...
On Aug 30, 1:27 am, "tshad" <t...@home.comwrote:
>Actually, I am thinking about giving up trying to create the DataGrid
Dynamically. The template seems more trouble than it is worth. I can
create the template fine, but you have to use an ascx file to get it to
work.

Why do you need to use an ascx file? You can add a linkbutton
dynamically

Dim lb As LinkButton = new LinkButton
This is a column in the DataGrid. You need a column type of object
(BoundColumn,HyperLinkColumn,TemplateColumn). A LinkButton can't converted
to a DataGridColumn. You don't have attributes such as HeaderText for a
LinkButton. If you want to add Textboxes, dropdowns, linkboxes, etc, you
need to surround them with a TemplateColumn.

You can't use a Hyperlink in a Datagrid but you can use a HyperLinkColumn.

Thanks,

Tom
...

Aug 30 '07 #5
On Aug 30, 5:46 pm, "tshad" <t...@home.comwrote:
"Alexey Smirnov" <alexey.smir...@gmail.comwrote in message

news:11**********************@50g2000hsm.googlegro ups.com...
On Aug 30, 1:27 am, "tshad" <t...@home.comwrote:
Actually, I am thinking about giving up trying to create the DataGrid
Dynamically. The template seems more trouble than it is worth. I can
create the template fine, but you have to use an ascx file to get it to
work.
Why do you need to use an ascx file? You can add a linkbutton
dynamically
Dim lb As LinkButton = new LinkButton

This is a column in the DataGrid. You need a column type of object
(BoundColumn,HyperLinkColumn,TemplateColumn). A LinkButton can't converted
to a DataGridColumn. You don't have attributes such as HeaderText for a
LinkButton. If you want to add Textboxes, dropdowns, linkboxes, etc, you
need to surround them with a TemplateColumn.

You can't use a Hyperlink in a Datagrid but you can use a HyperLinkColumn.
Hi Tom

you don't need to convert LinkButton into DataGridColumn (it's not
possible), you need to implement ITemplate Interface

Look at my example

Public Class TemplateColumnWithLink
Implements System.Web.UI.ITemplate

Public Sub InstantiateIn(ByVal container As System.Web.UI.Control)
Implements System.Web.UI.ITemplate.InstantiateIn
Dim lb As LinkButton
lb = New LinkButton
lb.ID = "LinkButton1"
lb.Text = "Test"
AddHandler lb.Click, AddressOf LinkClicked
container.Controls.Add(lb)
End Sub

Public Sub LinkClicked(ByVal sender As Object, ByVal e As EventArgs)
CType(sender, LinkButton).Page.Response.Write("Hello, world")
End Sub

End Class

That class has to be added in to your webform class, so you can use it
when you bound your columns, e.g.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
BindGrid()
End Sub

Protected Sub BindGrid()

Dim colTem As TemplateColumn
Dim b As BoundColumn
colTem = New TemplateColumn
colTem.ItemTemplate = New TemplateColumnWithLink
colTem.HeaderText = "Links"
DataGrid1.Columns.Add(colTem)

b = New BoundColumn
b.DataField = "..."
b.HeaderText = "..."
DataGrid1.Columns.Add(b)

da.Fill(ds)

DataGrid1.DataSource = ds.Tables(0)
DataGrid1.DataBind()

....

End Sub

Hope this helps

Aug 31 '07 #6

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

Similar topics

3
by: Krzysztof Karnicki | last post by:
Hi, I need to create DataGrid with TemplateColumns in Codebehind so I do something like that: TemplateColumn templateColumn = new TemplateColumn(); templateColumn.HeaderText = "header title";...
3
by: Jeremy Chapman | last post by:
At run time I've added a TemplateColumn to a DataGrid. Now I'm trying to add a Table control to the TemplateColumns's HeaderTemplate and ItemTemplate. In essence, I'm trying to do in code,...
1
by: Tim::.. | last post by:
Hi can someone tell me how I get the values from a datagrid using template columns??? I tried like this but I keep getting Input string was not in a correct format. I don't think I'm doing this...
2
by: Patrick | last post by:
I want to define a set of web-form templates in XML and render the equivalent web-form with ASP.NET, then process any input server controls on the form. Reading the XML file from Page_load is...
6
by: Tex | last post by:
I am writting a survey system web application. I am using ASP.Net 2, C# and MS SQL 2005. I am able to store surveys and questions associated to the surveys just fine. The problem I am having is...
5
by: sam | last post by:
Hi all, I am dynamically creating a table rows and inerting radio buttons which are also dynamically created. Everything works fine in Firefox as expected. But I am not able to select radio...
10
by: Jess | last post by:
Hello, If I create a temporary object using a dynamically created object's pointer, then when the temporary object is destroyed, will the dynamically created object be destroyed too? My guess...
0
by: lianaent | last post by:
Hi All, I'm brand new to asp.net 2.0, and have a simple task of just creating a quick and dirty data entry form with SQL Server 2005 on the back end. I added a gridview to my form, and I can...
4
by: mohaaron | last post by:
I can think of a lot of reasons why this might need to be done but as far as I can tell it's not possible. I've been looking for a way to add HtmlTableRows to a table using a button click for a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...
0
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...

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.