Hello all.
Thanks in advance. It has been at least 2 years since I have implemented my own PageTemplate. And I have it all working, my only problem is I cannot remeber how to handle the events for nested run-time created Child Controls.
IE. I can't remeber how to handle the event from the server controls within a DataGrid TemplateColumn ........
MyDataGrid_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles MyDataGrid.ItemCommand
All my top level events work, but it seems when the controls are in the PageTemplate, the event bubbling doesn't work or something. For the life of me I cannot remeber how I did it a couple years ago. Maybe delegates?? HAHA.
anyway let me lay down the situation:
The MasterTemplate is a USERCONTROL:
-----------------------------------------------------------------------------------------------------
ascx code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<title>
<%= me.Title %>
</title>
<LINK href="standard.css" type="text/css" rel="stylesheet">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:literal id="litHeader" Runat="server"></asp:literal><asp:placeholder id="BodyContainer" runat="server"></asp:placeholder><asp:literal id="litFooter" Runat="server"></asp:literal>
</form>
</body>
</HTML>
-----------------------------------------------------------------------------------------------------
ascx.vb:
<ParseChildren(True)> Public MustInherit Class MainTemplate
Inherits System.Web.UI.UserControl
'Implements INamingContainer
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents BodyContainer As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents litHeader As System.Web.UI.WebControls.Literal
Protected WithEvents litFooter As System.Web.UI.WebControls.Literal
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private _main As ITemplate
Private _title As String
Public Property body() As ITemplate
Get
Return Me._main
End Get
Set(ByVal Value As ITemplate)
Me._main = Value
End Set
End Property
Public Property Title() As String
Get
Return String.Format("Amerco Intranet: {0}", Me._title)
End Get
Set(ByVal Value As String)
Me._title = Value
End Set
End Property
Public Sub LoadChildControls()
Me.EnsureChildControls()
End Sub
Protected Overrides Sub CreateChildControls()
If Not body Is Nothing Then
body.InstantiateIn(BodyContainer)
End If
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
If Request.IsAuthenticated Then
Dim UserGuid As String = context.User.Identity.Name
litHeader.Text = GetTheHeaderData(UserGuid)
litFooter.Text = GetTheFooterData(UserGuid)
Else
Response.Redirect("Login_main.aspx")
End If
End If
End Sub
End Class
-------------------------------------------------------------------------------------------------
That's the TemplateControl, pretty basic and easy. The problem resides in the pages
TESTPAGE .ASPX
--------------------------------------------------------------------------------------------------
..aspx code
<%@ Register TagPrefix="Master" TagName="Template" Src="../Controls/MainTemplate.ascx"%>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="EditDealerStandards.aspx.vb" Inherits="DealerReports.EditDealerStandards"%>
<Master:Template Title="PageTemplateTest" id="PageTemplate" runat="server">
<BODY>
<table cellSpacing="0" cellPadding="3" width="600" align="center" border="0">
<tr align="center">
<td>
<table class="actiontable" cellSpacing="0" cellPadding="3" align="center" border="0">
<tr>
<td>Select District, MCO, or Routes to Edit</td>
</tr>
<tr>
<td><asp:dropdownlist id="ddlEdit" Runat="server">
<asp:ListItem Value="District" Selected="True">District</asp:ListItem>
<asp:ListItem Value="MCO">MCO</asp:ListItem>
<asp:ListItem Value="Route">Route</asp:ListItem>
</asp:dropdownlist>
<asp:button id="cmdGetData" Runat="server" Text="Go" Height="22"></asp:button></td>
</tr>
</table>
<br>
<br>
<asp:datagrid id="dgDealers" Runat="server" AllowSorting="True" CssClass="data_table" AutoGenerateColumns="False"
PageSize="20" PagerStyle-Mode="NumericPages" AllowPaging="True">
<AlternatingItemStyle CssClass="alternating_row"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Font-Bold="True" VerticalAlign="Bottom" HorizontalAlign="Center" />
<Columns>
<asp:TemplateColumn HeaderText="New Standard">
<ItemTemplate>
<asp:TextBox ID="txtNewStandard" Runat="server" Width="50" Height="20"></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Retrieve Previous">
<ItemTemplate>
<!--PROBLEM CHILD HERE!!!!!!!!! THE EVENT WONT BUBBLE --> <asp:LinkButton ID="lcmdRetrieve" Runat="server" CommandArgument="Retrieve">Retrieve</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid><br>
<asp:button id="cmdApplyChanges" Runat="server" Text="Apply New Standards"></asp:button><br>
</td>
</tr>
</table>
</BODY>
</Master:Template>
-----------------------------------------------------------------------------------
and finally, the aspx.vb
Public Class EditDealerStandards
Inherits AdminPageBase
Protected WithEvents dgDealers As System.Web.UI.WebControls.DataGrid
Protected WithEvents ddlEdit As System.Web.UI.WebControls.DropDownList
Protected WithEvents cmdApplyChanges As System.Web.UI.WebControls.Button
Protected WithEvents cmdGetData As System.Web.UI.WebControls.Button
Protected PageTemplate As MainTemplate
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PageTemplate.LoadChildControls()
ddlEdit = DirectCast(PageTemplate.FindControl("ddlEdit"), DropDownList)
dgDealers = DirectCast(PageTemplate.FindControl("dgDealers"), DataGrid)
cmdApplyChanges = DirectCast(PageTemplate.FindControl("cmdApplychanges"), Button)
cmdGetData = DirectCast(PageTemplate.FindControl("cmdGetData"), Button)
If Not IsPostBack Then
If Not MyBase.IsAdmin Then Response.Redirect("../../../default.aspx")
BindGrid()
End If
End Sub
''Im only putting a couple methods in here all the other events are fine except:
Private Sub dgDealers_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgDealers.ItemCommand
If e.CommandArgument.ToString() = "Retrieve" Then
Dim DealerDB As DealerReportsDB
Dim RetrievePrevious As LinkButton = DirectCast(e.Item.FindControl("lcmdRetrieve"), LinkButton)
'do work .......
End If
End Sub
----------------------------------------------------------------------------------------------
I can't get this event to fire. It seems the nested controls do not even fire the JScript Page_postback correctly. Im sorry for all the code pasting, but I wanted to dictate my situation thouroughly the first time. If I remeber, this is an easy fix, but I would greatly appreciate the advice from someone who has done this more recently than I
Thanks-
Don't bother just to be better than your contemporaries or predecessors. Try to be better than yourself
-William Faulkner