Creating TemplateColumns dynamically

T

tshad

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
 
G

Guest

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.
 
T

tshad

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.

Anon User said:
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.
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.
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
 
G

Guest

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
....
 
T

tshad

Anon User said:
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
 
G

Guest

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
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top