473,516 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3421
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
4537
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 different word like "PIZZAAAA" this new word is not available in the Update_Command following :
0
1707
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 can't. How can i do this? <Code> <script language="javascript"> document.getElementById('<%=txt1.ClientID %>').value='test';...
1
4573
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 web user control, but I found that it is in the namedcontrols. How can I find the web user control using foreach in the datalist Thanks.
2
5030
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" oneditcommand="GLRulesGrid_Edit"
0
961
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" Text='<%#
1
1286
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"), DropDownList) But the place that I am trying to access it from right now is not an event of the DataList, so it does not have e as one of the parameters....
2
1770
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 there's an error that says: "Syntax error in UPDATE statement." I wonder what's wrong with my code and how to correct this one? Below are my codes:...
2
1392
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 imfamous complexity and undocumented vagaries of user controls and MasterPages which are also a type of user control.
1
8816
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, ByVal e As System.EventArgs) Handles Button1.Click Dim dli As DataListItem For Each dli In DataList1.Items Dim theValue...
0
7276
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7408
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7581
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7548
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5714
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4773
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3267
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
1624
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.