Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WebUserControl dynamically instantiate within a Repeater

Status
Not open for further replies.

xdzgor

Programmer
May 17, 2010
4
DK
Hi

I have a user-control which uses the ViewState to persist some data between postbacks. But if I dynamically instantiate my control within a Repeater, it seems my control has no access to the ViewState - how do I get around this limitation?

See the below aspx, on which I place a web-user-control called WebUserControl5a.
It is placed (1) statically, (2) dynamically in a placeholder, and (3) dynamically in a placeholder in a Repeater.

WebUserControl5a uses the ViewState to contain a count, which it increments on every postback, and displays. This works fine, unless WebUserControl5a is dynamically instantiated in a Repeater. Why, and how do I work around this?

Note that I could achieve this "increment count" in other ways, but that is not the point of the exercise. I really want to persist data in the viewstate which I have access to.

Thanks!

WebForm5.aspx:
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm5.aspx.cs" Inherits="WebApplication1.WebForm5" %>

<%@ Register TagPrefix="uc" TagName="WebUserControl5a" Src="WebUserControl5a.ascx" %>
<%@ Register TagPrefix="uc" TagName="WebUserControl5b" Src="WebUserControl5b.ascx" %>

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    User control 5a directly on page:
    <br />
    <table border="1">
        <tr>
            <td>
                <uc:WebUserControl5a ID="wuc5a" runat="server" />
            </td>
        </tr>
    </table>
    <br />

    User control 5a dynamically instantiated in a placeholder on the page:
    <br />
    <table border="1">
        <tr>
            <td>
                <asp:PlaceHolder ID="placeholderFor5a" runat="server" />
            </td>
        </tr>
    </table>
    <br />

    User control 5a and 5b dynamically created in a repeater:
    <br />
    <table border="1">

        <asp:Repeater ID="repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
            <ItemTemplate>
                <tr>
                    <td>
                        Control = <asp:PlaceHolder ID="ph" runat="server" />
                    </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
    
    </table>

    <asp:Button ID="SaveBtn" runat="server" Text="save" onclick="SaveBtn_Click" />

    </div>
    </form>
</body>
</html>

WebForm5.aspx.cs:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm5 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            WebUserControl5a wuc5a = LoadControl("WebUserControl5a.ascx") as WebUserControl5a;
            placeholderFor5a.Controls.Add(wuc5a);

            if (!IsPostBack)
            {
                IList<string> data = RetrieveData();
                CurrentData = data;
            }
            else
            {
                IList<string> data = CurrentData;
                CurrentData = data;
            }
        }

        protected IList<string> CurrentData
        {
            get
            {
                return ViewState["DATA_LIST"] as IList<string>; ;
            }

            set
            {
                ViewState["DATA_LIST"] = value;

                if (value != null)
                {
                    repeater1.DataSource = value;
                    repeater1.DataBind();
                }
                else
                {
                }
            }
        }

        private IList<string> RetrieveData()
        {
            IList<string> data = new List<string>();

            data.Add("5a");
            data.Add("5b");

            return data;
        }

        protected void Repeater1_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
            RepeaterItem item = e.Item;

            if ((item.ItemType == ListItemType.Item) || (item.ItemType == ListItemType.AlternatingItem))
            {
                Control dynamicControl = null;

                string controlType = item.DataItem as string;

                if (controlType == "5a")
                {
                    WebUserControl5a wc5a = LoadControl("WebUserControl5a.ascx") as WebUserControl5a;
                    dynamicControl = wc5a;
                }
                else if (controlType == "5b")
                {
                    WebUserControl5b wc5b = LoadControl("WebUserControl5b.ascx") as WebUserControl5b;
                    dynamicControl = wc5b;
                }


                dynamicControl.ID = "dcontrol_" + item.ItemIndex;
                PlaceHolder ph = (PlaceHolder)item.FindControl("ph");
                ph.Controls.Add(dynamicControl);
            }
        }

        protected void SaveBtn_Click(object sender, EventArgs e)
        {

        }
    }
}

WebUserControl5a.ascx:
Code:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl5a.ascx.cs" Inherits="WebApplication1.WebUserControl5a" %>

This is WebUserControl5a<br />
<asp:TextBox ID="textbox5a" runat="server" /><br />
StateCount=<%= StateCount%>

WebUserControl5a.ascx.cs:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebUserControl5a : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int? count = StateCount;

            if (count == null)
            {
                count = 0;
                StateCount = count;
            }
            else
            {
                count++;
                StateCount = count;
            }
        }

        protected int? StateCount
        {
            get
            {
                return ViewState["COUNT_5A"] as int?;
            }
            set
            {
                ViewState["COUNT_5A"] = value;
            }
        }

        protected string GetDataValue()
        {
            return textbox5a.Text;
        }
    }
}

WebUserControl5b.ascx:
Code:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl5b.ascx.cs" Inherits="WebApplication1.WebUserControl5b" %>

This is WebUserControl5b<br />
<asp:TextBox ID="textbox5b" runat="server" />

WebUserControl5b.ascx.cs:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebUserControl5b : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top