Re: Required multiple instances of a UserControl loaded at runtime

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hi Steven

Thanks for the information. I have another question. I am currently loading
the user control when a button onclick event fires and also in a Placeholder
Load event in the right conditions. This works fine for me. How can I access
the DataItem value from the ItemCreated method?

I think I need to store the values in viewstate for each DataItem of each
RepeaterItem so I can access these values in the Load and OnClick events
(also without rebinding).

What would be the appropriate way?
Thanks
Andrew


"Steven Cheng[MSFT]" <stcheng@xxxxxxxxxxxxxxxxxxxx> wrote in message
news:FQ%23EPz0IHHA.2080@xxxxxxxxxxxxxxxxxxxxxxxxx
Hello Andrew,

Thanks for your reply.

For your further questions, here are my understanding and suggestion:

the "e.Item.DataItem" object refer to the current item from databound
datasource, and this is only available during databinding period. And in
your usercontrol's custom event handler (or any other place durign control
lifecycle), this propety is not available, you need to persist the value
in
some certain place so that the usercontrol can access it later.

So far what I've got are the following two means to persist the value:

1. directly use databinding expression in ascx usercontrol to bind the
value to a sub control in usercontrol. e.g.

================
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="WebUserControl.ascx.cs" Inherits="uc_WebUserControl" %>
.......................
<asp:Label ID="lblData" runat="server" Text='<%# Eval("CategoryName") %>'
Visible="false"></asp:Label>
================

When you add usercontrol into any databound context(in repeater,
datalist...), it can automatically join the databinding context and get
the
value from each DataItem


2. You can also define a custom property on the usercontrol so that you
can
persist the data from DataSource Item into this property on usercontrol.
Later, the usercontrol's method or event can access this property to get
the data. e.g.

=========usercontrol code behind=========
public partial class uc_WebUserControl : System.Web.UI.UserControl
{
public string DataText
{
get
{
if (ViewState["_datatext"] != null)
{
return ViewState["_datatext"] as string;
}
return "default data text";
}

set
{
TrackViewState();
ViewState["_datatext"] = value;
}
}

.................

protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = DataText;
}
}
========================

And in repeater's databound code, you can programmatically assign this
value:

===================
protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs
e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
PlaceHolder holder = e.Item.FindControl("holder1") as
PlaceHolder;

if (holder != null)
{
Control uc = Page.LoadControl("~/uc/WebUserControl.ascx");

uc_WebUserControl wuc = uc as uc_WebUserControl;
wuc.DataText = DataBinder.Eval(e.Item.DataItem,
"CategoryID", "{0}");

holder.Controls.Add(uc);
}
}
}
===============================

#Note that since ASP.NET page/usercontrol use partial class, to reference
the actual type of the usercontrol in page, you need to explicitly
reference it in Page(through @page directive) .e.g.

================
<%@ Page ...................%>
<%@ Reference VirtualPath="~/uc/WebUserControl.ascx" %>

..............
==================

Hope this helps. If there is anything unclear, please feel free to let me
know.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no
rights.



.


Quantcast