Solved

Get value from a programatically created textbox

Posted on 2014-02-27
6
387 Views
Last Modified: 2014-04-29
Hello,

I am creating a textbox and attaching it to a table using this code...

            if (controlType == "textbox")
            {
                HtmlTableRow row = new HtmlTableRow();
                HtmlTableCell cell1 = new HtmlTableCell();
                HtmlTableCell cell2 = new HtmlTableCell();

                System.Web.UI.WebControls.Label label = new System.Web.UI.WebControls.Label();
                System.Web.UI.WebControls.TextBox textbox = new System.Web.UI.WebControls.TextBox();
                label.ID = "Label" + rowCount.ToString();
                label.Text = r["ControlText"].ToString();
                textbox.ID = r["ControlName"].ToString();

                cell1.Width = "200px";
                cell1.Controls.Add(label);
                cell2.Controls.Add(textbox);
                row.Cells.Add(cell1);
                row.Cells.Add(cell2);

                tableControls.Rows.Add(row);
            }

Open in new window


I am trying to get the value of the textbox using this code, but the TextBox I create always has a null value.

TextBox t = this.FindControl(controlName) as TextBox;

Open in new window


Any ideas?
0
Comment
Question by:tatton777
  • 4
6 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility
I tried this and working fine for me
check and let us know if any issue

 protected void Page_Load(object sender, EventArgs e)
    {
        HtmlTableRow row = new HtmlTableRow();
        HtmlTableCell cell1 = new HtmlTableCell();
        HtmlTableCell cell2 = new HtmlTableCell();

        System.Web.UI.WebControls.Label label = new System.Web.UI.WebControls.Label();
        System.Web.UI.WebControls.TextBox textbox = new System.Web.UI.WebControls.TextBox();
        label.ID = "Label1";
        label.Text = "Label1";
        textbox.ID = "Text1";

        cell1.Width = "200px";
        cell1.Controls.Add(label);
        cell2.Controls.Add(textbox);
        row.Cells.Add(cell1);
        row.Cells.Add(cell2);
        tableControls.Rows.Add(row);
     

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox t = this.FindControl("Text1") as TextBox; 

        Response.Write(t.Text);
    }

Open in new window

0
 
LVL 1

Author Comment

by:tatton777
Comment Utility
I am not having luck.

The control is embedded in a RadAjaxPanel (like an updatepanel).

I tried using the RadAjaxPanel.FindControl but that didn't work either. Any more ideas?
0
 
LVL 19

Expert Comment

by:Daniel Van Der Werken
Comment Utility
I've had problems in the past with controls located on Ajax update panels.

http://msdn.microsoft.com/en-us/library/bb386454.aspx

What I've found is that a control that is in an update panel won't register properly with a control that isn't. So, they either need to be in the same update panel or they need ot be both in separate update panels with UpdateMode-"Conditional" in the ASPX page for the UpdatePanel and do a .Update() in the code-behind.

Either that, or use JQuery if possible. That get the value regardless.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:tatton777
Comment Utility
Hello,

I removed the updatepanel and the controls still aren't found.

The controls are added programatically in the page_load. I'm reading that since they are loaded in the page_load event that they may have to be recreated. Is that true?
0
 
LVL 1

Accepted Solution

by:
tatton777 earned 0 total points
Comment Utility
I ended up getting help elsewhere. You have to load the table with the controls on EVERY POSTBACK once the set of controls is instantiated. You can do this on the Page_Load or the Page_Init, it doesn't matter.

    //==================================================
    // Page_Load()
    //==================================================
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {

        }
        if(sv.FillPdfPageFormControls != null)
            LoadTableWithPdfControls(sv.FillPdfPageFormControls);
    }

    //==================================================
    // LoadTableWithPdfControls()
    //==================================================
    protected void LoadTableWithPdfControls(DataTable formContols)
    {
        int rowCount = 1;

        foreach (DataRow r in formContols.Rows)
        {
            string controlType = r["ControlType"].ToString().ToLower().Trim();

            if (controlType == "spacer")
            {
                TableRow row = new TableRow();
                TableCell cell = new TableCell();
                cell.ColumnSpan = 2;
                cell.Text = " ";
                row.Cells.Add(cell);                
                
                TableControls.Rows.Add(row);
                TableControls.Rows.Add(row);
            }

            if (controlType == "label")
            {
                TableRow row = new TableRow();
                TableCell cell = new TableCell();

                System.Web.UI.WebControls.Label label = new System.Web.UI.WebControls.Label();
                label.Text = r["ControlText"].ToString();
                label.ID = r["ControlName"].ToString();
                cell.ColumnSpan = 2;
                cell.Controls.Add(label);
                row.Cells.Add(cell);
                TableControls.Rows.Add(row);
            }

            if (controlType == "textbox")
            {
                TableRow row = new TableRow();
                TableCell cell1 = new TableCell();
                TableCell cell2 = new TableCell();

                System.Web.UI.WebControls.Label label = new System.Web.UI.WebControls.Label();
                System.Web.UI.WebControls.TextBox textbox = new System.Web.UI.WebControls.TextBox();
                label.ID = "Label" + rowCount.ToString();
                label.Text = r["ControlText"].ToString();

                //TextBox wk = (TextBox)this.TableControls.FindControl(r["controlName"].ToString());

                textbox.ID = r["ControlName"].ToString();

                cell1.Width = 200;
                cell1.Controls.Add(label);
                cell2.Controls.Add(textbox);
                row.Cells.Add(cell1);
                row.Cells.Add(cell2);

                TableControls.Rows.Add(row);
            }

            if (controlType == "checkbox")
            {
                TableRow row = new TableRow();
                TableCell cell1 = new TableCell();
                TableCell cell2 = new TableCell();

                System.Web.UI.WebControls.CheckBox checkbox = new System.Web.UI.WebControls.CheckBox();

                checkbox.Text = r["ControlText"].ToString();
                checkbox.ID = r["ControlName"].ToString();

                cell2.Controls.Add(checkbox);
                row.Cells.Add(cell1);
                row.Cells.Add(cell2);
                TableControls.Rows.Add(row);
            }
            rowCount += 1;
        }
    }

Open in new window

0
 
LVL 1

Author Closing Comment

by:tatton777
Comment Utility
Because I solved my own problem. Why else would I be choosing myself as the provider of the solution? I hired two programmers to help me with this. One failed utterly, the other helped me get this solved. I decided that I would share the fix with the experts-exchange community.

Take care.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now