469,625 Members | 1,171 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,625 developers. It's quick & easy.

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 2098
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Krzysztof Karnicki | last post: by
3 posts views Thread by Jeremy Chapman | last post: by
1 post views Thread by Tim::.. | last post: by
6 posts views Thread by Tex | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.