Loading Different User Controls into a Single DataGrid

Discussion in 'ASP .Net Datagrid Control' started by Mark, Dec 8, 2003.

  1. Mark

    Mark Guest

    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
    Mark, Dec 8, 2003
    #1
    1. Advertising

  2. Mark

    lostinet Guest

    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") %>
    lostinet, Dec 9, 2003
    #2
    1. Advertising

  3. Mark

    Mark Guest

    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" <> wrote in message
    news:e5qZ$...
    > 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") %>
    >
    >
    >
    >
    Mark, Dec 9, 2003
    #3
  4. Mark

    lostinet Guest

    ok

    here is the sample code (zipped 4kb)

    "Mark" <> дÈëÓʼþ
    news:%...
    > 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" <> wrote in message
    > news:e5qZ$...
    > > 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") %>
    > >
    > >
    > >
    > >

    >
    >
    lostinet, Dec 10, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?V291dGVy?=
    Replies:
    1
    Views:
    683
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    Feb 25, 2006
  2. Eric Layman
    Replies:
    3
    Views:
    616
    Rad [Visual C# MVP]
    Apr 14, 2007
  3. jim
    Replies:
    1
    Views:
    728
    Wizfrog
    Jul 24, 2007
  4. =?Utf-8?B?QU5lZWxpbWE=?=

    Loading custom user controls into asp.net 2.0 wizard help

    =?Utf-8?B?QU5lZWxpbWE=?=, Oct 2, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    699
    Mike Placentra II
    Oct 3, 2007
  5. ela
    Replies:
    12
    Views:
    334
    Uri Guttman
    Apr 6, 2009
Loading...

Share This Page