1

I create textbox dynamically, but can't retrieve a value from created textbox. Anyone can explain to me what am I doing wrong?

HtmlGenericControl testes = new HtmlGenericControl("DIV");
    testes.ID = "Div_Cabos_Rede";
    testes.Attributes.Add("class", "col-md-12 letra");
    testes.InnerHtml = "Cabos de rede";
    TextBox Cabos_de_rede = new TextBox();
    Cabos_de_rede.ID = "Txt_Cabos_Rede";
    Cabos_de_rede.Attributes.Add("class", "col-md-12 form-control");
    testes.InnerHtml = "Cabos de rede";
    Body.Controls.Add(testes);
    Body.Controls.Add(Cabos_de_rede);

This works fine almost fine (minor unrelated css problems), but when later I try to retrieve data from dynamically created textbox I get NULL value.

Here is my code to retrieve value:

 TextBox testar = (TextBox)Body.FindControl("Txt_Cabos_Rede");
 ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('" + testar + "');", true);
8
  • 1
    This seems like a very odd way of doing this in the first place. Why are you dynamically creating this control? I suspect there's a more standard way to implement whatever you're implementing. As for getting the resulting value, perhaps you don't want to use FindControl() but instead just inspect the raw form data in the Request object? (Side note: You're also trying to output the TextBox object itself, not it's .Text property.) Commented Mar 7, 2017 at 16:26
  • I need to create the control dynamically because I have an XML with different number of rows and I want to create bootstrap modal dynamically Commented Mar 7, 2017 at 16:29
  • Well, I still suspect there's a better way, but I'll defer to the design if that's what you find appropriate. In any event, if FindControl() isn't working, can you debug into the control hierarchy in Body and see if your dynamically created controls are there at all? If this is a post-back they might not be. However, any form values should still be in that HTML POST request. Are the key/value pairs you're looking for in Request.Form? It's a more manual way to get the values, but manual seems to be the approach here. Commented Mar 7, 2017 at 16:32
  • My dynamically created controls appear fine, I can see them when I run my application, I suspect that the problem is with the "runat=server" tag , but i am not sure Commented Mar 7, 2017 at 16:36
  • because those controls should be created on pre-render event (at leats what the google say) and i create them on button click Commented Mar 7, 2017 at 16:37

1 Answer 1

1

The main problem dealing with dynamically created control is you need to reload them back in either Page Init or Page Load event.

FYI: We normally use either Panel or PlaceHolder to load controls instead of Body tag, so that we can style them easily.

ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DemoWebForm.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:PlaceHolder runat="server" ID="PlaceHolder1" />
        <asp:Button runat="server" ID="SubmitButton" Text="Submit" OnClick="SubmitButton_Click" />
        <br />
        Posted Value:
        <asp:Label runat="server" ID="ResultLabel" />
    </form>
</body>
</html>

Code Behind

public partial class Default : System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        HtmlGenericControl testes = new HtmlGenericControl("DIV");
        testes.ID = "Div_Cabos_Rede";
        testes.Attributes.Add("class", "col-md-12 letra");
        testes.InnerHtml = "Cabos de rede";
        TextBox Cabos_de_rede = new TextBox();
        Cabos_de_rede.ID = "Txt_Cabos_Rede";
        Cabos_de_rede.Attributes.Add("class", "col-md-12 form-control");
        testes.InnerHtml = "Cabos de rede";
        PlaceHolder1.Controls.Add(testes);
        PlaceHolder1.Controls.Add(Cabos_de_rede);
    }

    protected void SubmitButton_Click(object sender, EventArgs e)
    {
        TextBox testar = FindControlRecursive(PlaceHolder1, "Txt_Cabos_Rede") as TextBox;
        ResultLabel.Text = testar.Text;
    }

    // Custom method to search a control recursively 
    // in case it is nested inside other control. 
    // You can create it as an extension method if you would like.
    public static Control FindControlRecursive(Control root, string id)
    {
        if (root.ID == id)
            return root;

        return root.Controls.Cast<Control>()
            .Select(c => FindControlRecursive(c, id))
            .FirstOrDefault(c => c != null);
    }
}

I know you have a lot of questions. Before commenting on this question, please create a new project, and make this very simple code to work.

Sign up to request clarification or add additional context in comments.

1 Comment

This worked fine thank you very much. not quite understood how the FindControlRecursive method works, but I got the idea. Thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.