468,257 Members | 1,391 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,257 developers. It's quick & easy.

Using a user control as EditItemTemplate in a DataList

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
Nov 19 '05 #1
2 3141
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 <rh*****@newsgroups.nospam>
| 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: rh*****@newsgroups.nospam
| Reply-To: rh*****@newsgroups.nospam
| Organization: RHM Media, LLC
| Date: Mon, 14 Nov 2005 18:45:58 -0500
| Message-ID: <c8*****************************@40tude.net>
| 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!TK2MSFT NGP15.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
|

Nov 19 '05 #2
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: <c8*****************************@40tude.net>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: st*****@online.microsoft.com (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: <Kj**************@TK2MSFTNGXA02.phx.gbl>
| 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 <rh*****@newsgroups.nospam>
| | 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: rh*****@newsgroups.nospam
| | Reply-To: rh*****@newsgroups.nospam
| | Organization: RHM Media, LLC
| | Date: Mon, 14 Nov 2005 18:45:58 -0500
| | Message-ID: <c8*****************************@40tude.net>
| | 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!TK2MSFT NGP15.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
| |
|
|

Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by caldera | last post: by
2 posts views Thread by zambizzi | last post: by
reply views Thread by RAM | last post: by
1 post views Thread by Nathan Sokalski | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.