Loading Different User Controls into a Single DataGrid

M

Mark

I have a situation where I am going to create 5 similar, but different user
controls that all need to be bound into one datagrid.
Basically, depending on the data that I bind to the datagrid, I want to load
a usercontrol for that row.
Does anyone know of any examples of where this is done or how I might go
about it?
I've done a grid where I created a class that implemented ITemplate and
coded the InstantiateIn to create a linkbutton dynamically and then coded a
databinding event to add data to that linkbutton. But in this case, I
almost need the data in the InstantiateIn method in order to know what type
of control to create. See what I'm saying?

Please help
Mark
 
L

lostinet

1.
add all usercontrols on ItemCreated
set the Visible of usercontrols on ItemDataBound
2.
use a usercontrol for proxy :
filelist:
WebForm1.aspx
UProxy.ascx
Canada.ascx
NoneCA.ascx

Core Content:

WebForm1.aspx:
<asp:DataGrid id="DataGrid1" runat="server">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<uc1:UProxy id="UProxy1" runat="server"></uc1:UProxy>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

the datagrid bind the data : sqlserver.pubs.authors

UProxy.ascx:

<script runat=server>
public DataGridItem Item
{
get
{
if(__item==null)
{
for(Control c=this;c!=null;c=c.Parent)
{
if(c is DataGridItem)
{
__item=(DataGridItem)c;
break;
}
}
}
return __item;
}
}
DataGridItem __item;

protected void FillHolder()
{
holder.Controls.Clear();
holder.Controls.Add(LoadStateControl());
}

override protected void LoadViewState(object obj)
{
base.LoadViewState(obj);
FillHolder();
}

protected Control LoadStateControl()//load a control from ViewSate
{
object o=ViewState["state"];
if(o==null)return new LiteralControl("#");//not found . not binding yet
string s=o.ToString();
//determine which to load here
if(s=="CA")
return LoadControl("Canada.ascx");
else
return LoadControl("NoneCA.ascx");
}

protected override void OnDataBinding(EventArgs e)
{
base.OnDataBinding(e);
ViewState["state"]=DataBinder.Eval(Item.DataItem,"state","{0}");//record
value
FillHolder();
}

</script>
<asp:placeHolder ID=holder Runat=server>
</asp:placeHolder>


Canada.ascx:
<script runat=server>
public DataGridItem Item
{
get
{
if(__item==null)
{
for(Control c=this;c!=null;c=c.Parent)
{
if(c is DataGridItem)
{
__item=(DataGridItem)c;
break;
}
}
}
return __item;
}
}
DataGridItem __item;
</script>
Canada - <%# DataBinder.Eval(Item.DataItem,"address") %>

NoneCA.ascx:
<script runat=server>
public DataGridItem Item
{
get
{
if(__item==null)
{
for(Control c=this;c!=null;c=c.Parent)
{
if(c is DataGridItem)
{
__item=(DataGridItem)c;
break;
}
}
}
return __item;
}
}
DataGridItem __item;
</script>
NoneCA - <%# DataBinder.Eval(Item.DataItem,"address") %>
 
M

Mark

Umm, thanks for the code. It's a little hard to understand what exactly is
being done here though. I understand the basic concept I think. Do you know
of any articles that would explain this better. Or possibly be able to email
me a working example?

thx
 
L

lostinet

ok

here is the sample code (zipped 4kb)

Mark said:
Umm, thanks for the code. It's a little hard to understand what exactly is
being done here though. I understand the basic concept I think. Do you know
of any articles that would explain this better. Or possibly be able to email
me a working example?

thx
lostinet said:
1.
add all usercontrols on ItemCreated
set the Visible of usercontrols on ItemDataBound
2.
use a usercontrol for proxy :
filelist:
WebForm1.aspx
UProxy.ascx
Canada.ascx
NoneCA.ascx

Core Content:

WebForm1.aspx:
<asp:DataGrid id="DataGrid1" runat="server">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<uc1:UProxy id="UProxy1" runat="server"></uc1:UProxy>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

the datagrid bind the data : sqlserver.pubs.authors

UProxy.ascx:

<script runat=server>
public DataGridItem Item
{
get
{
if(__item==null)
{
for(Control c=this;c!=null;c=c.Parent)
{
if(c is DataGridItem)
{
__item=(DataGridItem)c;
break;
}
}
}
return __item;
}
}
DataGridItem __item;

protected void FillHolder()
{
holder.Controls.Clear();
holder.Controls.Add(LoadStateControl());
}

override protected void LoadViewState(object obj)
{
base.LoadViewState(obj);
FillHolder();
}

protected Control LoadStateControl()//load a control from ViewSate
{
object o=ViewState["state"];
if(o==null)return new LiteralControl("#");//not found . not binding yet
string s=o.ToString();
//determine which to load here
if(s=="CA")
return LoadControl("Canada.ascx");
else
return LoadControl("NoneCA.ascx");
}

protected override void OnDataBinding(EventArgs e)
{
base.OnDataBinding(e);
ViewState["state"]=DataBinder.Eval(Item.DataItem,"state","{0}");//record
value
FillHolder();
}

</script>
<asp:placeHolder ID=holder Runat=server>
</asp:placeHolder>


Canada.ascx:
<script runat=server>
public DataGridItem Item
{
get
{
if(__item==null)
{
for(Control c=this;c!=null;c=c.Parent)
{
if(c is DataGridItem)
{
__item=(DataGridItem)c;
break;
}
}
}
return __item;
}
}
DataGridItem __item;
</script>
Canada - <%# DataBinder.Eval(Item.DataItem,"address") %>

NoneCA.ascx:
<script runat=server>
public DataGridItem Item
{
get
{
if(__item==null)
{
for(Control c=this;c!=null;c=c.Parent)
{
if(c is DataGridItem)
{
__item=(DataGridItem)c;
break;
}
}
}
return __item;
}
}
DataGridItem __item;
</script>
NoneCA - <%# DataBinder.Eval(Item.DataItem,"address") %>
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top