Advertisement

08.11.2008 at 01:30PM PDT, ID: 23639294
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.3

Dynamic Usercontrol and Postback

Asked by copyPasteGhost in Programming for ASP.NET, Microsoft Visual C#.Net

Tags: ,

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!


Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
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>
 
 
[+][-]08.12.2008 at 02:17AM PDT, ID: 22211187

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Programming for ASP.NET, Microsoft Visual C#.Net
Tags: ASP.NET, C#
Sign Up Now!
Solution Provided By: renjurdevan
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_EXPERT_20070906