?
Solved

Findcontrol for Dynamically added WebControls

Posted on 2009-04-15
3
Medium Priority
?
467 Views
Last Modified: 2012-05-06
I am building a dynamic page with various controls. I have tried various different was, using the Onit to add the controls as well as page load and prerender. My issue is while the controls are added fine, I need to be able to pull the inputted data from said dynamically added text boxes and fckeditors and add them to either a viewstate or something when my linkbutton is clicked. I keep getting a null reference any place I move these controls around to. So I figured I would see if anyone has any other ideas short of using the designer to make the controls first. My code is below.

thanks in advance.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.IO;
using System.Text;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using FredCK.FCKeditorV2;
 
public partial class AdminEditCRECalendar : AdminPage
{
 
    protected override void OnInit(EventArgs e)
    {
        String editID = Request.QueryString["edit"].ToString();
        String actID = Request.QueryString["act"].ToString();
		if (actID == "1")
                {
                    cal1.Visible = false;
                    //TO DO: Delete line below
                    editEventPanel.Visible = false;
                    String tbn = "cnTextbox";
                    String fckn = "fckEditor";
 
                    TextBox tb = new TextBox();
                    FCKeditor fck = new FCKeditor();
 
                    tb = (System.Web.UI.WebControls.TextBox)pageAddPanel.FindControl(tbn);
                    fck = (FredCK.FCKeditorV2.FCKeditor)pageAddPanel.FindControl(fckn);
                    if (tb != null)
                    {
                        ViewState["EventName"] = tb.Text;
                        ViewState["EventDetails"] = fck.Value;
                    }
                }
            
 
    }
    private Control Literal(string p)
    {
        Literal rv = new Literal();
        rv.Text = p;
        return rv;
    }
    protected new void Page_Load(object sender, EventArgs e)
    {
            this.PreRender += new EventHandler(AdminEditCRECalendar_PreRender);
                          
            
    }
 
 
 
    static string StripHTML(string text)
    {
        StringBuilder b = new StringBuilder(text.Length);
        bool inside = false;
        for (int i = 0; i < text.Length; i++)
        {
            char let = text[i];
            if (let == '<')
            {
                inside = true;
                continue;
            }
            if (let == '>')
            {
                inside = false;
                continue;
            }
            if (inside == false)
            {
                b.Append(let);
            }
        }
        return b.ToString();
    }
 
    void AdminEditCRECalendar_PreRender(object sender, EventArgs e)
    {
        //throw new Exception("The method or operation is not implemented.");
        String editID = Request.QueryString["edit"].ToString();
                String actID = Request.QueryString["act"].ToString();
 
                if (!IsPostBack)
                {
                    //editID 0 is base page
                    if (editID == "0")
                    {
                        AddNewEvent.Text = "Add a New Event";
                        AddNewEvent.NavigateUrl = string.Format("AdminEditCRECalendar.aspx?act=0&edit=1");
                        if (Request.QueryString["ReturnDate"] != null)
                        {
                            cal1.SelectedDate = DateTime.Parse(Request.QueryString["ReturnDate"]);
                        }
                        else
                        {
                            cal1.SelectedDate = DateTime.Now;
                        }
                        cal1.VisibleDate = cal1.SelectedDate;
 
                        DateTime startDate = new DateTime
                            (cal1.VisibleDate.Year, cal1.VisibleDate.Month, 1).AddDays(-7);
                        DateTime endDate = new DateTime
                            (cal1.VisibleDate.Date.AddMonths(1).Year,
                            cal1.VisibleDate.Date.AddMonths(1).Month, 1).AddDays(7);
                        cal1.DataSource = GetEventData(startDate, endDate);
                    }
                    //editID 1 is for new event creation
                    else if (editID == "1")
                    {
                        //name and details
                        if (actID == "0")
                        {
                            cal1.Visible = false;
                            //TO DO: Delete line below
                            editEventPanel.Visible = false;
                            Label cnL = new Label();
                            Label cdL = new Label();
                            TextBox cnTb = new TextBox();
                            FCKeditor fck = new FCKeditor();
                            HyperLink hlN = new HyperLink();
                            HyperLink hlC = new HyperLink();
                            CREHeaderLabel.Text = "New Event Creator";
                            pageAddPanel.Controls.Add(Literal("<HR style='COLOR: #21558c' />"));
 
                            //label editing
                            cnL.ID = "cnLabel";
                            cnL.Text = "Event Name: ";
                            cnL.CssClass = "AdminEditorLabel";
                            cdL.ID = "cdLabel";
                            cdL.Text = "Details: ";
                            cdL.CssClass = "AdminEditorLabel";
 
                            //textbox editing
                            cnTb.ID = "cnTextbox";
                            cnTb.Width = System.Web.UI.WebControls.Unit.Pixel(400);
 
                            //fck editor editing
                            fck.ID = "fckEditor";
                            fck.ToolbarSet = "Mytoolbar";
                            fck.Width = System.Web.UI.WebControls.Unit.Pixel(400);
                            fck.Height = System.Web.UI.WebControls.Unit.Pixel(250);
 
                            //hyperlink editing
                            hlN.ID = "eventeditorNext";
                            hlN.Text = "Next";
                            hlN.CssClass = "AdminEditorLink";
                            hlN.NavigateUrl = "AdminEditCRECalendar.aspx?act=1&edit=1";
                            hlC.ID = "cancelHL";
                            hlC.Text = "Cancel";
                            hlC.CssClass = "AdminEditorLink";
                            hlC.NavigateUrl = "AdminHome.aspx";
 
                            pageAddPanel.Controls.Add(Literal
                                ("<table width='100%' height='85%'><tbody><tr><td style='width:85px' vAlign='top' align='left'>"));
                            pageAddPanel.Controls.Add(cnL);
                            pageAddPanel.Controls.Add(Literal("</td><td vAlign='top' align='left'>"));
                            pageAddPanel.Controls.Add(cnTb);
                            pageAddPanel.Controls.Add(Literal("<tr><td style='width:85px' vAlign='top' align='left'>"));
                            pageAddPanel.Controls.Add(cdL);
                            pageAddPanel.Controls.Add(Literal("</td><td vAlign='top' align='left'>"));
                            pageAddPanel.Controls.Add(fck);
                            pageAddPanel.Controls.Add(Literal("</td></tr></tbody></table>"));
                            pageAddPanel.Controls.Add(Literal("<HR style='COLOR: #21558c' />"));
                            pageAddPanel.Controls.Add(hlC);
                            pageAddPanel.Controls.Add(Literal("&nbsp;"));
                            pageAddPanel.Controls.Add(hlN);
                            pageAddPanel.Visible = true;
                        }
                        //dates
                        else if (actID == "1")
                        {
                            cal1.Visible = false;
                            //TO DO: Delete line below
                            editEventPanel.Visible = false;
                            String tbn = "cnTextbox";
                            String fckn = "fckEditor";
 
                            TextBox tb = new TextBox();
                            FCKeditor fck = new FCKeditor();
 
                            tb = (System.Web.UI.WebControls.TextBox)pageAddPanel.FindControl(tbn);
                            fck = (FredCK.FCKeditorV2.FCKeditor)pageAddPanel.FindControl(fckn);
                            if (tb != null)
                            {
                                ViewState["EventName"] = tb.Text;
                                ViewState["EventDetails"] = fck.Value;
                            }
                        }
 
                    }
                    //editID 2 is for new class creation
                    else if (editID == "2")
                    {
                        if (Request.QueryString["act"].ToString() == "0")
                        {
                        }
                        else if (Request.QueryString["act"].ToString() == "1")
                        {
                        }
                        else if (Request.QueryString["act"].ToString() == "2")
                        {
                        }
                        else if (Request.QueryString["act"].ToString() == "3")
                        {
                        }
                    }
                    //editID 3 is for editing events
                    else if (editID == "3")
                    {
                        if (Request.QueryString["act"].ToString() == "0")
                        {
                        }
                        //add/edit classes visible -- driven to after adding new event
                        else if (Request.QueryString["act"].ToString() == "1")
                        {
                        }
                        else if (Request.QueryString["act"].ToString() == "2")
                        {
                        }
                    }
                    //editID 4 is for editing classes
                    else if (editID == "4")
                    {
                    }
                    //editID 5 is for registering for events/also is the details view
                    else if (editID == "5")
                    {
                    }
                    //editID 6 is for registering for classes/also is the details view
                    else if (editID == "6")
                    {
                    }
                }
    }
 
 
 
}

Open in new window

0
Comment
Question by:GLIanimal
  • 2
3 Comments
 

Author Comment

by:GLIanimal
ID: 24151488
Side Note: I also tried adding the controls at the top of the onit on every cycle, making them visible only when needed, however while I was able to find the controls, the value's were always blank that way.
0
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 1500 total points
ID: 24152494
0
 

Author Closing Comment

by:GLIanimal
ID: 31570634
Thanks
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Screencast - Getting to Know the Pipeline
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question