473,807 Members | 2,766 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.HeaderTe xt = "Template Column"
column.ItemStyl e.Width = Unit.Pixel(widt h)
column.HeaderSt yle.Width = Unit.Pixel(widt h)
column.ItemTemp late = Page.LoadTempla te("testTEmplat eColumn2.ascx")
' column.OnClick= "Resume_Sub mit"

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

The .ascx file is:

<%@ Language="VB" %>
<asp:LinkButt on 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.testTempla teColumn2_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.Tem plateColumn'.

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 2308
On Aug 30, 12:02 am, "tshad" <t...@home.comw rote:
If I added:

OnClick="Resume _Submit"

I get the error:

'resume_submit' is not a member of 'ASP.testTempla teColumn2_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.Tem plateColumn'.
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.LoadTempla te then you shoud assign the onclick method in
testTEmplateCol umn2.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******** *************@y 42g2000hsy.goog legroups.com...
On Aug 30, 12:02 am, "tshad" <t...@home.comw rote:
>If I added:

OnClick="Resum e_Submit"

I get the error:

'resume_submit ' is not a member of 'ASP.testTempla teColumn2_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:LinkButt on 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.Tem plateColumn'.

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.LoadTempla te then you shoud assign the onclick method in
testTEmplateCol umn2.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.onIte mCreated = "ItemCreate d"

This will give you an error:

'System.Web.UI. WebControls.Dat aGrid.Protected Overridable Sub
OnItemCreated(e As System.Web.UI.W ebControls.Data GridItemEventAr gs)' 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.comw rote:
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.goog legroups.com...
On Aug 30, 1:27 am, "tshad" <t...@home.comw rote:
>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,Hy perLinkColumn,T emplateColumn). 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.comw rote:
"Alexey Smirnov" <alexey.smir... @gmail.comwrote in message

news:11******** **************@ 50g2000hsm.goog legroups.com...
On Aug 30, 1:27 am, "tshad" <t...@home.comw rote:
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,Hy perLinkColumn,T emplateColumn). 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 TemplateColumnW ithLink
Implements System.Web.UI.I Template

Public Sub InstantiateIn(B yVal container As System.Web.UI.C ontrol)
Implements System.Web.UI.I Template.Instan tiateIn
Dim lb As LinkButton
lb = New LinkButton
lb.ID = "LinkButton 1"
lb.Text = "Test"
AddHandler lb.Click, AddressOf LinkClicked
container.Contr ols.Add(lb)
End Sub

Public Sub LinkClicked(ByV al sender As Object, ByVal e As EventArgs)
CType(sender, LinkButton).Pag e.Response.Writ e("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.EventArg s) Handles Me.Load
BindGrid()
End Sub

Protected Sub BindGrid()

Dim colTem As TemplateColumn
Dim b As BoundColumn
colTem = New TemplateColumn
colTem.ItemTemp late = New TemplateColumnW ithLink
colTem.HeaderTe xt = "Links"
DataGrid1.Colum ns.Add(colTem)

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

da.Fill(ds)

DataGrid1.DataS ource = ds.Tables(0)
DataGrid1.DataB ind()

....

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
6679
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"; TemplateBuilder templateBuilder = new TemplateBuilder(); templateBuilder.AppendLiteralString("text" ); templateColumn.ItemTemplate = templateBuilder; DataGrid1.Columns.Add(templateColumn);
3
2638
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, the equilavent of this html: <asp:TemplateColumn> <HeaderTemplate>
1
990
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 the correct why when it comes to using templatecolumns can someone please HELP! Thanks! ...:: CODE Dim pageID As Integer = CInt(e.Item.Cells(0).Text)
2
4574
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 easy, but 1) How do I set about dynamically creating user controls (like TextBox, TextArea) --- simply Declare and initialised (new) the user controls?? How do I "place" it graphically on the form. Ideally, I want them to lay out in a table, one...
6
6101
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 Dynamically creating a survey on a page. I have read some MS articles on dynamic creation of web forms and have been able to create one question on a page. I am not sure how to create a variable amount of different types(radio button lists,...
5
12555
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 buttons in IE. It does not even throw any errors. I have searched over the net but could not find anyhelp. Hope some experts here could help me. Here is part my code that dynamically generates the radio buttons, I cannot paste the entire code as it is...
10
2586
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 is that it's not destroyed, but I'm not sure. I have the following program: #include<iostream>
0
1570
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 populate it dynamically by choosing from a dropdown that I populate dynamically from the database sys.tables table. When I choose a table from my dropdown, poof, the gridview is beautifully loaded with all the columns and rows, and the edit and delete...
4
2979
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 while and it seems it's not possible because the row that gets added with each click won't get recreated after a post back. After all the reading it seems that any dynamically created controls must be created in the Init event to be recreated after...
0
9719
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9599
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10624
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5684
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.