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
| |
|
|
 
Joined
Jan 27, 2010
Messages
27
Reaction score
0
Using ASP.NET expression to bind the control properties at run time

Following are some use cases and examples for this:

a) Common use of this is to specify the connection string property of a control.


<asp:SqlDataSource ID="SqlDataSource1" Runat="server"
SelectCommand="SELECT * FROM [Employees]"
ConnectionString="<%$ ConnectionStrings:TraineeDatabaseConnectionString%>">
</asp:SqlDataSource>


Inside Web.config:


<!--A common use of expressions is to set the connection string property of a control-->

<connectionStrings>
<add name="TraineeDatabaseConnectionString"
connectionString="uid=trainee;pwd=mindfire;database=Trainee2009;server=LANSERVER;"
providerName="System.Data.SqlClient" />
</connectionStrings>


b) To display static content in our web pages we can add key-value pair in <appSettings> .
For example, Lets set a key value pair inside Web.config for the copyright message for our website.


<!--setting the site's copyright message-->

<appSettings>
<add key="copyright" value="(c)Copyright 2009 MFS"/>
</appSettings>

Now we can set the property like


<asp:Literal ID="Literal1" runat="server" Text="<%$ AppSettings: copyright %>"></asp:Literal>


We can use this key "copyright" where ever we want. We can do the same in our masterpage also, but the advantage here is the setting in a centralized place. Web.config files are easy to access and understand. It will be more helpful in case of Nested master page concept.


c) In addition to the above two, you can display values that are stored in resource (.resx or .resource) files in our project. Resource files are used to store information for a specific language or “language and culture” combination. Suppose we want that welcome message in our web page should display according to the language specified at run time, then we can use this.

Steps for using Resource file :
1. Create a App_GlobalResources folder in the root directory.
2. Create a file WelcomeMessage.resx.
3. Now create the Name Value pair as Name: Welcome, Value:Welcome to our website!!!
4. Now set it as property of control as follows:

<asp:Label id="lblResource" runat="server" text="<%$ Resources: My_Resource, Welcome %>" />

Hope this will help you.
Searching has no end points. Keep searching.
 

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

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top