Using a user control as EditItemTemplate in a DataList

H

Hans Merkl

Hi,

I am trying to use a user control as EditItemTemplate in a DataList. It
loads fine but I can't figure out how to bind to the data of the DataList.
Here is what I have got so far:

//Page_load of my user control
protected void Page_Load(object sender, EventArgs e)
{
IDataItemContainer DataContainer = this.Parent as
IDataItemContainer;
if (DataContainer != null)
{
DataList dl = this.Parent.Parent as DataList;
mIsNew = false;
int ItemIndex = DataContainer.DataItemIndex;
object oKey = dl.DataKeys[ItemIndex];


This kind of works but I think it's not very clean. Does anybody know what
the proper way is?

Thanks
 
S

Steven Cheng[MSFT]

Hi Hans,

Welcome to ASPNET newsgroup.
Regarding on the question of make the ascx usercontrol access the
databinding context of its container control when being put in other
Template databound control (e.g datalist, datagrid....), here are some of
my understanding and suggestion:

1. AS for ascx UserControl, itself is a template based control (we
directly define html in ascx file...), so we can directly put databinding
expressions in the Ascx file , and then at runtime, the Usercontrol's
databinding expression will be dynamically compiled and be able to access
the databinding context of its Container control (datagrid, datalist ...)'s
binding objects... Fo example, we have a page which contains the following
datalist code:

=====================in main aspx page===============
<asp:DataList ID="DataList1" runat="server" DataKeyField="EmployeeID"
DataSourceID="SqlDataSource1">
<ItemTemplate>
EmployeeID:
<asp:Label ID="EmployeeIDLabel" runat="server" Text='<%#
Eval("EmployeeID") %>'></asp:Label><br />

<uc1:MYUC ID="MYUC1" runat="server" />
</ItemTemplate>
</asp:DataList>
</form>
===============

we can find that there is a ascx usercontrol in the "itemTemplate" of the
DataList, then in the "MYUC" usercontrol, we can define the ascx template
as below:

==============MYUC.ascx=============
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MYUC.ascx.cs"
Inherits="UC_MYUC" %>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("EmployeeID")
%>'></asp:Label><br />
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("LastName")
%>'></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text='<%# Eval("FirstName") %>' />
<asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Eval("Title")
%>'>LinkButton</asp:LinkButton>
=============

At runtime, the Databinding expression can correctly reference the
databinding context and DataItem in the container (DataList)

2. Also, if you'd like to do programmatic databinding , e.g in DataList's
"ItemDataBound" event, we can consider define some public Properties which
associated with the usercontrol's certain inner Controls , so that we can
dynamically set value to the usercontrol's inner control in the DataList
(or other template databound control)'s ItemDataBound" event.....

Just some of my opinions. Hope helps.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


--------------------
| From: Hans Merkl <[email protected]>
| Subject: Using a user control as EditItemTemplate in a DataList
| User-Agent: 40tude_Dialog/2.0.14.1
| MIME-Version: 1.0
| Content-Type: text/plain; charset="us-ascii"
| Content-Transfer-Encoding: 7bit
| Sender: (e-mail address removed)
| Reply-To: (e-mail address removed)
| Organization: RHM Media, LLC
| Date: Mon, 14 Nov 2005 18:45:58 -0500
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: pcp0011547978pcs.anapol01.md.comcast.net 68.54.166.32
| Lines: 1
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:358108
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Hi,
|
| I am trying to use a user control as EditItemTemplate in a DataList. It
| loads fine but I can't figure out how to bind to the data of the DataList.
| Here is what I have got so far:
|
| //Page_load of my user control
| protected void Page_Load(object sender, EventArgs e)
| {
| IDataItemContainer DataContainer = this.Parent as
| IDataItemContainer;
| if (DataContainer != null)
| {
| DataList dl = this.Parent.Parent as DataList;
| mIsNew = false;
| int ItemIndex = DataContainer.DataItemIndex;
| object oKey = dl.DataKeys[ItemIndex];
|
|
| This kind of works but I think it's not very clean. Does anybody know what
| the proper way is?
|
| Thanks
|
 
S

Steven Cheng[MSFT]

Hi Hans,

How are you doing on this post? Does the suggestions in my last reply helps
a little? If there're anything else we can help, please feel free to post
here. Thanks,

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
| X-Tomcat-ID: 194467493
| References: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: (e-mail address removed) (Steven Cheng[MSFT])
| Organization: Microsoft
| Date: Tue, 15 Nov 2005 10:06:49 GMT
| Subject: RE: Using a user control as EditItemTemplate in a DataList
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Lines: 108
| Path: TK2MSFTNGXA02.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:358174
| NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
|
| Hi Hans,
|
| Welcome to ASPNET newsgroup.
| Regarding on the question of make the ascx usercontrol access the
| databinding context of its container control when being put in other
| Template databound control (e.g datalist, datagrid....), here are some of
| my understanding and suggestion:
|
| 1. AS for ascx UserControl, itself is a template based control (we
| directly define html in ascx file...), so we can directly put databinding
| expressions in the Ascx file , and then at runtime, the Usercontrol's
| databinding expression will be dynamically compiled and be able to access
| the databinding context of its Container control (datagrid, datalist
...)'s
| binding objects... Fo example, we have a page which contains the
following
| datalist code:
|
| =====================in main aspx page===============
| <asp:DataList ID="DataList1" runat="server" DataKeyField="EmployeeID"
| DataSourceID="SqlDataSource1">
| <ItemTemplate>
| EmployeeID:
| <asp:Label ID="EmployeeIDLabel" runat="server" Text='<%#
| Eval("EmployeeID") %>'></asp:Label><br />
|
| <uc1:MYUC ID="MYUC1" runat="server" />
| </ItemTemplate>
| </asp:DataList>
| </form>
| ===============
|
| we can find that there is a ascx usercontrol in the "itemTemplate" of the
| DataList, then in the "MYUC" usercontrol, we can define the ascx template
| as below:
|
| ==============MYUC.ascx=============
| <%@ Control Language="C#" AutoEventWireup="true" CodeFile="MYUC.ascx.cs"
| Inherits="UC_MYUC" %>
| <asp:Label ID="Label1" runat="server" Text='<%# Eval("EmployeeID")
| %>'></asp:Label><br />
| <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("LastName")
| %>'></asp:TextBox><br />
| <asp:Button ID="Button1" runat="server" Text='<%# Eval("FirstName") %>' />
| <asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Eval("Title")
| %>'>LinkButton</asp:LinkButton>
| =============
|
| At runtime, the Databinding expression can correctly reference the
| databinding context and DataItem in the container (DataList)
|
| 2. Also, if you'd like to do programmatic databinding , e.g in DataList's
| "ItemDataBound" event, we can consider define some public Properties
which
| associated with the usercontrol's certain inner Controls , so that we can
| dynamically set value to the usercontrol's inner control in the
DataList
| (or other template databound control)'s ItemDataBound" event.....
|
| Just some of my opinions. Hope helps.
|
| Thanks,
|
| Steven Cheng
| Microsoft Online Support
|
| Get Secure! www.microsoft.com/security
| (This posting is provided "AS IS", with no warranties, and confers no
| rights.)
|
|
| --------------------
| | From: Hans Merkl <[email protected]>
| | Subject: Using a user control as EditItemTemplate in a DataList
| | User-Agent: 40tude_Dialog/2.0.14.1
| | MIME-Version: 1.0
| | Content-Type: text/plain; charset="us-ascii"
| | Content-Transfer-Encoding: 7bit
| | Sender: (e-mail address removed)
| | Reply-To: (e-mail address removed)
| | Organization: RHM Media, LLC
| | Date: Mon, 14 Nov 2005 18:45:58 -0500
| | Message-ID: <[email protected]>
| | Newsgroups: microsoft.public.dotnet.framework.aspnet
| | NNTP-Posting-Host: pcp0011547978pcs.anapol01.md.comcast.net 68.54.166.32
| | Lines: 1
| | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
| | Xref: TK2MSFTNGXA02.phx.gbl
| microsoft.public.dotnet.framework.aspnet:358108
| | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| |
| | Hi,
| |
| | I am trying to use a user control as EditItemTemplate in a DataList. It
| | loads fine but I can't figure out how to bind to the data of the
DataList.
| | Here is what I have got so far:
| |
| | //Page_load of my user control
| | protected void Page_Load(object sender, EventArgs e)
| | {
| | IDataItemContainer DataContainer = this.Parent as
| | IDataItemContainer;
| | if (DataContainer != null)
| | {
| | DataList dl = this.Parent.Parent as DataList;
| | mIsNew = false;
| | int ItemIndex = DataContainer.DataItemIndex;
| | object oKey = dl.DataKeys[ItemIndex];
| |
| |
| | This kind of works but I think it's not very clean. Does anybody know
what
| | the proper way is?
| |
| | Thanks
| |
|
|
 

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

Top