Get text from dynamically generated text field in asp.net

I kept banging my head about this, so I hope I can help. Essentially, I'm having trouble getting values ​​from some text field controls that I dynamically create in .net 4.

Here is the desired application stream.

1). The user selects an html document from the drop-down menu, which is a template for the letter. This html document has $ VARIABLENAME $ format tags that will be replaced with the correct values.

2). The program starts according to the template and pulls out all lines of the format $ STRING $ and saves them in the list.

3). For each entry in this list, the program generates asp: label and asp: textbox with a unique identifier based on the original $ VARIABLENAME $ field.

4). The user enters substitution values ​​and sends requests.

5). The program replaces all $ STRING $ with replacement values ​​and prints the result.

Everything works well until the moment when I need to get values ​​from text fields. I am sure this is a problem with the page life cycle, but since text fields are not generated until use selects the desired template from the drop-down list, I am not sure how to make them persist through callbacks so that I can reference them.

Am I really doing all this wrong? How to access text fields created from dropdown event after postback froma submitbutton event?

EDIT: Here's the most relevant code.

protected void createTextBoxes(List<string> results)
    {
        if (results != null)
        {
            foreach (string result in results)
            {
                string formattedResult = result.Substring(1, result.Length - 2);
                formattedResult = formattedResult.ToLower();
                formattedResult = char.ToUpper(formattedResult[0]) + formattedResult.Substring(1);


                var label = new Label();
                label.ID = formattedResult;
                label.Text = formattedResult + ": ";
                templateFormPlaceholder.Controls.Add(label);

                var textBox = new TextBox();
                textBox.ID = result;
                templateFormPlaceholder.Controls.Add(textBox);
                templateFormPlaceholder.Controls.Add(new LiteralControl("<br />"));

                previewBtn.Visible = true;
            }
        }
    }

protected void templateDD_SelectedIndexChanged(object sender, EventArgs e)
    {
        var templatePath = "";
        if (templateDD.SelectedIndex == 0)
        {
            previewBtn.Visible = false;
        }

        if (templateDD.SelectedIndex == 1)
        {
            templatePath = employeePath;
        }
        else if (templateDD.SelectedIndex == 2)
        {
            templatePath = managerPath;
        }
        List<string> regMatches = FindMatches(templatePath);
        Session["regMatches"] = regMatches;
        createTextBoxes(regMatches);
    }

protected void Page_Init(object sender, EventArgs e)
    {
        if (Session["regMatches"] != null)
        {
            createTextBoxes((List<string>)Session["regMatches"]);
        }
    }

. - . $STRING $, , .

   protected void previewBtn_Click(object sender, EventArgs e)
    {
        List<string> matchResults = (List<string>)Session["regMatches"];
        Dictionary<string, string> parameters = new Dictionary<string, string>();
        foreach (string result in matchResults)
        {
            TextBox tb = (TextBox)templateFormPlaceholder.FindControl(result);
            parameters.Add(result, tb.Text);
        }

        var template = ReplaceKeys(parameters);
        outputLBL.Text = template;

.aspx.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
    <p>
        Which template would you like to use?
    </p>
    <asp:DropDownList ID="templateDD" runat="server" OnSelectedIndexChanged="templateDD_SelectedIndexChanged"
        AutoPostBack="true">
        <asp:ListItem></asp:ListItem>
        <asp:ListItem Value="1">Employee</asp:ListItem>
        <asp:ListItem Value="2">Manager</asp:ListItem>
    </asp:DropDownList>
    <br />
    <asp:PlaceHolder ID="templateFormPlaceholder" runat="server" />
    <div>
        <asp:Button ID="previewBtn" runat="server" Text="Preview" Visible="false" OnClick="previewBtn_Click" />
    </div>
    <div>
        <asp:Label ID="outputLBL" runat="server"></asp:Label>
    </div>
    <br />
</div>
</form>
</body>
</html>

EDIT: , , , , :

, . , . , (, ID = "$ FIRSTNAME $" ). , $ - . ID = "", . !

+5
3

, . Page_Init, . , - ( Session, ) textboxes, , . , ( - , ).


UPDATE

:  1. List<TextBox> ( CreatedTextBoxes)

  • , . . , (, Repeater). , CreatedTextBoxes

  • , Session

  • Page_Init Session. , . , , args

  • , CreatedTextBoxes, FindControls()
+7

TextBox templateFormPlaceholder.Controls, form1.FindControl, . FindControl , - http://msdn.microsoft.com/en-us/library/486wc64h.aspx, templateFormPlaceholder.FindControl.

+1

Create dynamic text fields and add them to the asp panel so you can easily access it.

Here are the ASP.NET design elements.

<div class="form-group">
<asp:Panel ID="panel" runat="server" CssClass="form-group">

</asp:Panel>
</div>

C # code for generating dynamic text fields

 protected void create_dynamic_text(object sender, EventArgs e)
{
    int num = 5; // you can give the number here
    for (int i = 0; i < num;i++ )
    {
        TextBox tb = new TextBox();
        tb.ID = "txt_box_name" + i.ToString();
        tb.CssClass = "add classes if you need";
        tb.Width = 400; //Manage width and height 
        panel.Controls.Add(tb); //panel is my ASP.Panel object. Look above for the design code of ASP panel
    }
}

C # code for accepting values

 protected void reade_values(object sender, EventArgs e)
{
   int num=5; // your count goes here
    TextBox tb = new TextBox();
        for (int i = 0; i < num; i++)
        {
           tb=(TextBox)panel.FindControl("txt_box_name"+i.ToString());
           string value = tb.Text; //You have the data now
        }
    }
}
+1
source

All Articles