0

My code generates an TextBox on the fly in C# (page_load function). Can I access it in the code later? It does give me compilation error and does not seem to work. Can someone verify ?

Code for additonal problem

aContent += "<table>";
aContent += "<tr><td>lablel </td><td style='bla blah'><input type='textbox' id='col-1' name='col-1'/></td></tr> ... 10 such rows here
</table>"

spanMap.InnerHtml = aContent;

The contents are rendered OK but recusrive iteration does not return the textbox. I am calling it like this

 TextBox txt = (TextBox)this.FindControlRecursive(spanMap, "col-1");
 // txt = (TextBox) spanMapping.FindControl("col-1"); this does not work too
 if (txt != null)
 {
      txt.Text = "A";
 }
TheTechGuy
  • 16,560
  • 16
  • 115
  • 136

2 Answers2

2

Assuming that you're persisting it correctly, you should be able to access it in code-behind using the FindControl method. Depending on where the control is, you may have to search recursively through the control hierarchy:

private Control FindControlRecursive(Control root, string id)  
{  
    if (root.ID == id) 
    {  
        return root;  
    }  

    foreach (Control c in root.Controls)  
    {  
        Control t = FindControlRecursive(c, id);  
        if (t != null)  
        {  
            return t;  
        }  
    }  

    return null;  
} 

Using FindControlRecursive:

TextBox txt = this.FindControlRecursive(Page.Form, "TextBox1") as TextBox;
if (txt != null)
{
    string text = txt.Text;
}

If you still can't find it using the above method, make sure that you're creating the control during after every postback, somwhere before Page_Load, like OnInit.

EDIT

I think you need to change the way you're adding content to the container. Instead of using a <span>, I would use a Panel, and instead of building markup, simply add controls to the panel in code-behind:

TextBox txt = new TextBox();
txt.ID = String.Format("txt_{0}", Panel1.Controls.Count);
Panel1.Controls.Add(txt);    
James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • So I guess FindControl is my only way to acess it. I can not directly access it. @sylence below did mention adding a reference but that is not very clear. – TheTechGuy Nov 08 '11 at 17:03
  • Yes, you definitely need `FindControl` to access a dynamic control. You can't access it directly. – James Johnson Nov 08 '11 at 17:06
  • I tried this code but it did not work. My html is actually inside a span. `spanMap.InnerHtml = aContent;`. Inside there is a table and inside table is my textbox. Although you need to change `as Textbox` to `TextBox( ...)`, in the second block of code. – TheTechGuy Nov 08 '11 at 18:41
  • I'm confused... what's the problem? The above method shouldn't have any problem traversing the tree. – James Johnson Nov 08 '11 at 18:43
  • I have included additional code in the question. It just does not find any child element except one literalcontrol. Also I am using masterpage, if that makes any difference. I used it before though and it did work (not this particular example). – TheTechGuy Nov 08 '11 at 18:58
  • Oh, so you're not using an ASP.NET TextBox? Either use an ASP.NET TextBox or add `runat="server"` to the input element, and you should be able to find it. – James Johnson Nov 08 '11 at 19:02
  • I see, thanks for clearing this up! That's a big help. I am still work on it though. – TheTechGuy Nov 08 '11 at 19:12
  • I just provided a simple example, but you should be able to recreate your markup using the `Table`, `TableRow`, and `TableCell` controls, and obviously the `DropDownList` control. – James Johnson Nov 08 '11 at 19:15
  • Thank you a bunch for the help. While it still did not work, possible culprit is span. But I now know what is wrong. I was going to change the design anyways, there was no need to create this particular form on the fly. Thank you again – TheTechGuy Nov 08 '11 at 20:10
  • You're very welcome. Just let me know if you need any more help ;) – James Johnson Nov 08 '11 at 20:11
1

Here's an example:

<%@ Page Language="C#" %>
<script type="text/C#" runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        var textBox = new TextBox();
        textBox.ID = "myTextBox";
        textBox.Text = "hello";
        Form1.Controls.Add(textBox);
    }

    protected void BtnTestClick(object sender, EventArgs e)
    {
        var textBox = (TextBox)Form1.FindControl("myTextBox");
        lblTest.Text = textBox.Text;
    }
</script>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <form id="Form1" runat="server">
        <asp:LinkButton ID="btnTest" runat="server" Text="Click me" OnClick="BtnTestClick" />
        <asp:Label ID="lblTest" runat="server" />
    </form>
</body>
</html>
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928