|
 |
aspx_beginners thread: RE: Why can't I find the control?
Message #1 by "Jon Maz" <jonmaz@s...> on Tue, 23 Jul 2002 16:32:54
|
|
Hi All,
In case anyone else is having the same problems as me, here is a code
example of finding controls in dynamic item templates that actually works!
The key is the line:
Dim tbBob as TextBox = e.Item.Cells(1).Controls(0).FindControl
("tbItemText")
Cells(1) corresponds to the second column in the template (0 based),
Controls(0) corresponds to the template itself (that's the crucial bit),
and "tbItemText" is the TextBox within the template.
Thanks to Mike McIntyre, who solved this problem for me long after I had
given up all hope.
Long live the spirit of the newsgroups,
JON
*****************************************
++++++++++
template.ascx
++++++++++
<%@ Control Language="VB" %>
<asp:TextBox id="tbItemText" runat="server" Textmode="MultiLine"
Columns="50" Rows="20" />
++++++++++
main page.aspx
++++++++++
<%@ Page Language="VB" Debug="true" Trace="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Oledb" %>
<script language="VB" runat="server">
Sub Page_Init(sender as object, e as eventargs)
Dim tc1 As New TemplateColumn()
tc1.HeaderTemplate = New DataGridTemplate(ListItemType.Header, "Column1")
tc1.ItemTemplate = New DataGridTemplate(ListItemType.Item, "Column1")
'*** ADD ASCX FILE AS TEMPLATE ***
tc1.EditItemTemplate = Page.LoadTemplate("template.ascx")
tc1.FooterTemplate = New DataGridTemplate(ListItemType.Footer, "Column1")
dg.Columns.Add(tc1)
End sub
Sub Page_Load(sender as object, e as eventargs)
'*** dummy HashTable just to give the dg a shape!
Dim myHT As New Hashtable()
myHT.Add("Some", "Bollocks")
dg.DataSource = myHT
If Not Page.IsPostBack Then
dg.Databind()
End If
End Sub
sub dg_edit(sender as object, e as DataGridCommandEventArgs)
dg.edititemindex = e.item.itemindex
dg.databind()
end sub
sub dg_cancel(sender as object, e as DataGridCommandEventArgs)
dg.edititemindex = -1
dg.databind()
end sub
sub dg_update(sender as object, e as DataGridCommandEventArgs)
'put sth here
end sub
Sub dg_ItemDataBound(sender As Object, e As DataGridItemEventArgs)
If (e.Item.ItemType = ListItemType.EditItem) then
Dim tbBob as TextBox = e.Item.Cells(1).Controls(0).FindControl
("tbItemText")
tbBob.Text = "pink elephants"
end if
End sub
Private Class DataGridTemplate
Implements ITemplate
Dim templateType As ListItemType
Dim columnName As String
Sub New(ByVal type As ListItemType, ByVal ColName As String)
templateType = type
columnName = ColName
End Sub
Sub InstantiateIn(ByVal container As Control) Implements
ITemplate.InstantiateIn
Dim lc As New Literal()
Select Case templateType
Case ListItemType.Header
lc.Text = "<I><b>Header</b></I>"
container.Controls.Add(lc)
Case ListItemType.Item
lc.Text = "Item " & columnName
container.Controls.Add(lc)
Case ListItemType.EditItem
'*** This is taken care of with the LoadTemplate method above
Dim tb As New TextBox()
tb.Text = ""
container.Controls.Add(tb)
Case ListItemType.Footer
lc.Text = "<I><b>Footer</b></I>"
container.Controls.Add(lc)
End Select
End Sub
End Class
</script>
<html>
<head>
<link rel="STYLESHEET" type="text/css" href="Forum.css">
</head>
<body>
<form runat="server" name="form1" id="form1">
<asp:DataGrid id="dg" runat="server"
Bordercolor="black"
gridlines="vertical"
font-names="Arial"
font-size="10pt"
cellpadding="4"
cellspacing="0"
width="100%"
ShowFooter="True"
HeaderStyle-BackColor="#cccc99"
FooterStyle-BackColor="#cccc99"
ItemStyle-BackColor="#ffffff"
AlternatingItemStyle-Backcolor="#cccccc"
AutoGenerateColumns="False"
OnEditCommand="dg_edit"
OnCancelCommand="dg_cancel"
OnUpdateCommand="dg_update"
OnItemDataBound="dg_ItemDataBound"
>
<Columns>
<asp:editcommandcolumn HeaderText="EDIT" edittext="Edit"
CancelText="Cancel" UpdateText="Save" />
</Columns>
</asp:dataGrid>
</form>
</body></html>
Message #2 by "Jon Maz" <jonmaz@s...> on Tue, 23 Jul 2002 16:41:58
|
|
Hi,
In case anyone is wondering what I posted this message for, it is the
answer to a previously unanswered posting of mine from March this year
(!). The aforementioned Mike has just solved it for me - four months
after the original posting - and since several other people have got in
touch with me during those four months to ask if I had a solution, I
thought I'd post up the final answer for all to see.
However - probably because the original posting is so old - my message has
also come up as an apparently "new" posting. So now you know why!
The original can still be seen at:
http://p2p.wrox.com/view.asp?list=aspx_beginners&id=159686
JON
Message #3 by Pavankumar T <PavanKumar.T@k...> on Wed, 31 Jul 2002 09:52:16 +0530
|
|
Declare your textbox
as
Dim tbbob as new texbox()
and try !!!!
-----Original Message-----
From: Jon Maz [mailto:jonmaz@s...]
Sent: Tuesday, July 23, 2002 10:03 PM
To: aspx_beginners
Subject: [aspx_beginners] RE: Why can't I find the control?
Hi All,
In case anyone else is having the same problems as me, here is a code
example of finding controls in dynamic item templates that actually works!
The key is the line:
Dim tbBob as TextBox = e.Item.Cells(1).Controls(0).FindControl
("tbItemText")
Cells(1) corresponds to the second column in the template (0 based),
Controls(0) corresponds to the template itself (that's the crucial bit),
and "tbItemText" is the TextBox within the template.
Thanks to Mike McIntyre, who solved this problem for me long after I had
given up all hope.
Long live the spirit of the newsgroups,
JON
*****************************************
++++++++++
template.ascx
++++++++++
<%@ Control Language="VB" %>
<asp:TextBox id="tbItemText" runat="server" Textmode="MultiLine"
Columns="50" Rows="20" />
++++++++++
main page.aspx
++++++++++
<%@ Page Language="VB" Debug="true" Trace="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Oledb" %>
<script language="VB" runat="server">
Sub Page_Init(sender as object, e as eventargs)
Dim tc1 As New TemplateColumn()
tc1.HeaderTemplate = New DataGridTemplate(ListItemType.Header, "Column1")
tc1.ItemTemplate = New DataGridTemplate(ListItemType.Item, "Column1")
'*** ADD ASCX FILE AS TEMPLATE ***
tc1.EditItemTemplate = Page.LoadTemplate("template.ascx")
tc1.FooterTemplate = New DataGridTemplate(ListItemType.Footer, "Column1")
dg.Columns.Add(tc1)
End sub
Sub Page_Load(sender as object, e as eventargs)
'*** dummy HashTable just to give the dg a shape!
Dim myHT As New Hashtable()
myHT.Add("Some", "Bollocks")
dg.DataSource = myHT
If Not Page.IsPostBack Then
dg.Databind()
End If
End Sub
sub dg_edit(sender as object, e as DataGridCommandEventArgs)
dg.edititemindex = e.item.itemindex
dg.databind()
end sub
sub dg_cancel(sender as object, e as DataGridCommandEventArgs)
dg.edititemindex = -1
dg.databind()
end sub
sub dg_update(sender as object, e as DataGridCommandEventArgs)
'put sth here
end sub
Sub dg_ItemDataBound(sender As Object, e As DataGridItemEventArgs)
If (e.Item.ItemType = ListItemType.EditItem) then
Dim tbBob as TextBox = e.Item.Cells(1).Controls(0).FindControl
("tbItemText")
tbBob.Text = "pink elephants"
end if
End sub
Private Class DataGridTemplate
Implements ITemplate
Dim templateType As ListItemType
Dim columnName As String
Sub New(ByVal type As ListItemType, ByVal ColName As String)
templateType = type
columnName = ColName
End Sub
Sub InstantiateIn(ByVal container As Control) Implements
ITemplate.InstantiateIn
Dim lc As New Literal()
Select Case templateType
Case ListItemType.Header
lc.Text = "<I><b>Header</b></I>"
container.Controls.Add(lc)
Case ListItemType.Item
lc.Text = "Item " & columnName
container.Controls.Add(lc)
Case ListItemType.EditItem
'*** This is taken care of with the LoadTemplate method above
Dim tb As New TextBox()
tb.Text = ""
container.Controls.Add(tb)
Case ListItemType.Footer
lc.Text = "<I><b>Footer</b></I>"
container.Controls.Add(lc)
End Select
End Sub
End Class
</script>
<html>
<head>
<link rel="STYLESHEET" type="text/css" href="Forum.css">
</head>
<body>
<form runat="server" name="form1" id="form1">
<asp:DataGrid id="dg" runat="server"
Bordercolor="black"
gridlines="vertical"
font-names="Arial"
font-size="10pt"
cellpadding="4"
cellspacing="0"
width="100%"
ShowFooter="True"
HeaderStyle-BackColor="#cccc99"
FooterStyle-BackColor="#cccc99"
ItemStyle-BackColor="#ffffff"
AlternatingItemStyle-Backcolor="#cccccc"
AutoGenerateColumns="False"
OnEditCommand="dg_edit"
OnCancelCommand="dg_cancel"
OnUpdateCommand="dg_update"
OnItemDataBound="dg_ItemDataBound"
>
<Columns>
<asp:editcommandcolumn HeaderText="EDIT" edittext="Edit"
CancelText="Cancel" UpdateText="Save" />
</Columns>
</asp:dataGrid>
</form>
</body></html>
|
|
 |