472,787 Members | 1,362 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,787 software developers and data experts.

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 3355
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: etantonio | last post by:
Good Morning, I still havent solved my problem with a textbox inside an editItemTemplate of a datalist, in fact now it is filled with the word "CIAOOOOOO" , if a runtime in the form I put a...
0
by: Renato Neves | last post by:
My problem is simple, i have a datalist control with some controls inside of EditTemplate (the problem is the same in the DataGrid). I want to have access to this controls, using javascript, but i...
1
by: caldera | last post by:
Hi, I want to find the web user control in the datalist. I iterate the datalist using foreach statement in the DataListItem and in thi DataListItem I iterate all Controls objects but I can't find...
2
by: zambizzi | last post by:
....I can't seem to get my hands on a control I'm loading in an editable datagrid. Here's my datagrid control: <asp:datagrid id="GLRulesGrid" runat="server" autogeneratecolumns="False"...
0
by: RAM | last post by:
Hi, Could you help me please? I have: <asp:DataList ID="MateriałyDataList" runat="server" OnEditCommand="Materiały_Edit"> ... <EditItemTemplate> ... <asp:Label ID="NumerEdit" runat="server"...
1
by: Nathan Sokalski | last post by:
I am trying to access a control in the EditItemTemplate of a DataList in a usercontrol of mine. In the ItemDataBound event I can simply use CType(e.Item.FindControl("ddlEditSection"),...
2
tjc0ol
by: tjc0ol | last post by:
Hi guys, Im just wondering that I cannot update my database throught datagrid datalist control, and the only thing works is that I can delete, cancel, edit but when I clicked update link...
2
by: Hillbilly | last post by:
Anybody have any sage advice on this frustrating "feature" of ASP.NET? I have a TextBox in the EditItemTemplate of a DataList I can't seem to find for some reason that is I believe related to the...
1
by: bradleyhogan | last post by:
I've tried various ways to get data value(s) from a DataList control (with same results for ListView and FormView). These are my attempts: Protected Sub Button1_Click(ByVal sender As Object,...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.