Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Get value from a programatically created textbox

Posted on 2014-02-27
6
390 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
ID: 39893904
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
ID: 39894130
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 20

Expert Comment

by:Daniel Van Der Werken
ID: 39894931
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:tatton777
ID: 39895221
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
ID: 40020941
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
ID: 40029065
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

808 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