troubleshooting Question

Dynamic Usercontrol and Postback

Avatar of copyPasteGhost
copyPasteGhostFlag for Canada asked on
.NET ProgrammingASP.NET
1 Comment1 Solution2675 ViewsLast Modified:
Hello Experts,

I have read everywhere that you must recreate your usercontrol on postback. I have no idea how to do this...
I have messing with this for over 4 hours and now I turn to the experts.

We will start with a sample. I have TONS of questions on this and will be opening several questions so please bear with me...

I have two files.

testmainpage.aspx
testmainusercontrol.ascx

the problem I get right now is..

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0246: The type or namespace name 'testmainusercontrol' could not be found (are you missing a using directive or an assembly reference?)

Source Error:

 

Line 36:     }
Line 37:
Line 38:     private testmainusercontrol CreateControl(int id) {
Line 39:         testmainusercontrol result = (testmainusercontrol)LoadControl("testmainusercontrol.ascx");
Line 40:         result.ID = "c_" + id;
 
Any help would be great!
thanks!



testmainpage.aspx
 
<%@ Page Language="C#" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
    public ArrayList ControlIdList {
        get {
            object v = ViewState["ControlIdList"];
            return v != null ? (ArrayList)v : null;
        }
        set {
            ViewState["ControlIdList"] = value;
        }
    }
 
    public int LastControlId {
        get {
            object v = ViewState["LastControlId"];
            return v != null ? (int)v : 0;
        }
        set {
            ViewState["LastControlId"] = value;
        }
    }
 
    private Hashtable m_controls = new Hashtable();
 
    private void CreateControls() {
        ArrayList ids = ControlIdList;
        if (ids != null) {
            foreach (int id in ids) {
                testmainusercontrol control = CreateControl(id);
                c_placeHolder.Controls.Add(control);
            }
        }
    }
 
    private testmainusercontrol CreateControl(int id) {
        testmainusercontrol result = (testmainusercontrol)LoadControl("testmainusercontrol.ascx");
        result.ID = "c_" + id;
        result.InsertAbove += InsertRowAbove;
        result.InsertBellow += InsertRowBellow;
        result.Remove += RemoveRow;
        m_controls[id] = result;
        return result;
    }
 
    protected override void OnLoad(EventArgs e) {
        base.OnLoad(e);
        if (!IsPostBack) {
            LastControlId = 1;
            ArrayList idList = new ArrayList(1);
            idList.Add(0);
            ControlIdList = idList;
            CreateControls();
        }
        DisplayResult();
    }
 
    private void DisplayResult() {
        StringBuilder sb = new StringBuilder();
        bool first = true;
        foreach (int id in ControlIdList) {
            if (!first) {
                sb.Append(", ");
            }
            else {
                first = false;
            }
            testmainusercontrol control = (testmainusercontrol)m_controls[id];
            sb.Append(control.Text);
        }
        c_resultLabel.Text = sb.ToString();
    }
 
    protected override void LoadViewState(object savedState) {
        base.LoadViewState(savedState);
        CreateControls();
    }
 
    private void RemoveRow(object sender, EventArgs e) {
        testmainusercontrol control = (testmainusercontrol)sender;
        int index = control.Parent.Controls.IndexOf(control);
        c_placeHolder.Controls.RemoveAt(index);
        ControlIdList.RemoveAt(index);
        DisplayResult();
    }
 
    private void InsertRowAbove(object sender, EventArgs e) {
        testmainusercontrol control = (testmainusercontrol)sender;
        int index = control.Parent.Controls.IndexOf(control);
        CreateControlAt(index);
        DisplayResult();
    }
 
    private void InsertRowBellow(object sender, EventArgs e) {
        testmainusercontrol control = (testmainusercontrol)sender;
        int index = control.Parent.Controls.IndexOf(control) + 1;
        CreateControlAt(index);
        DisplayResult();
    }
 
    private void CreateControlAt(int index) {
        int id = GetNewId();
        testmainusercontrol newControl = CreateControl(id);
        ControlIdList.Insert(index, id);
        c_placeHolder.Controls.AddAt(index, newControl);
    }
 
    private int GetNewId() {
        int id = LastControlId++;
        return id;
    }
 
 
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:PlaceHolder runat="server" ID="c_placeHolder"></asp:PlaceHolder>
            <br />
            <asp:Label ID="c_resultLabel" runat="server" Text="Label"></asp:Label><br />
            <asp:Button ID="c_postBackButton" runat="server" Text="Do PostBack!" /></div>
    </form>
</body>
</html>
 
testmainusercontrol.ascx
 
<%@ Control Language="C#" ClassName="testmainusercontrol" %>
 
<script runat="server">
    public event EventHandler InsertAbove;
    public event EventHandler InsertBellow;
    public event EventHandler Remove;
 
    protected void InserAboveClick(object sender, EventArgs e) {
        if (InsertAbove != null) {
            InsertAbove(this, e);
        }
    }
 
    protected void InsertBellowClick(object sender, EventArgs e) {
        if (InsertBellow != null) {
            InsertBellow(this, e);
        }
    }
 
    protected void RemoveClick(object sender, EventArgs e) {
        if (Remove != null) {
            Remove(this, e);
        }
    }
 
    public string Text {
        get {
            return c_textBox.Text;
        }
    }
    
</script>
 
<div>
    <span style="width: 200px;">
        <asp:TextBox runat="server" ID="c_textBox" />
    </span><span>
        <asp:Button runat="server" ID="c_insertAboveButton" Text="Insert Above" OnClick="InserAboveClick" />
        <asp:Button runat="server" ID="c_insertBellowButton" Text="Insert Bellow" OnClick="InsertBellowClick" />
        <asp:Button runat="server" ID="c_removeButtom" Text="Remove" OnClick="RemoveClick" />
    </span>
</div>
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 1 Comment.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros