从在asp.net中动态创建文本框获取文本 [英] Get text from dynamically created textbox in asp.net

查看:229
本文介绍了从在asp.net中动态创建文本框获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在敲打我的头这一切上午,所以希望我能得到一些帮助。从本质上讲,我有问题的一些文本框控件,我在.NET 4中动态创建得到的值。

I've been banging my head against this all morning, so hopefully I can get some help. Essentially I'm having issues getting values from some textbox controls I'm creating dynamically in .net 4.

下面是应用程序的所需的流量。

Here's the desired flow of the application.

1)。用户从下拉菜单是用于一字母的模板的html文件。这个HTML文件的格式为$ VARIABLENAME $将被替换为正确值的标签。

1). User selects a html document from a dropdown menu that is a template for a letter. This html document has tags of the format $VARIABLENAME$ that will be replaced with the correct values.

2)。该程序运行,虽然模板,并翻出格式为$ STRING $的所有字符串,并将其存储在一个列表中。

2). The program runs though the template and pulls out all strings of the format $STRING$ and stores them in a list.

3)。对于此列表中的每个条目,该程序生成一个asp:标签和一个asp:文本框与基于原来的$ VARIABLENAME $字段唯一的ID

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

4)。用户输入替代值,并点击提交。

4). User enters replacement values, and hits submit.

5)。计划取代所有$ STRING $的用替换值,并输出结果。

5). Program replaces all $STRING$'s with the replacement values and outputs the result.

一切都运行良好了的地方,我需要从文本框中获取值的点。我敢肯定这是一个问题,在页面的生命周期,但由于没有生成的文本框,直到使用选择从下拉列表中选择所需的模板,我不知道如何让他们通过持续的回传,所以我可以参考他们。

Everything works well up to the point where I need to get values from the text boxes. I'm quite sure it's an issue with the page lifecycle, but because the textboxes are not being generated until the use selects the desired template from the dropdown, I'm not sure how to make them persist through postbacks so I can reference them.

我要对这个都错了?如何访问从下拉事件创建的文本字段回发弗罗马提交按钮事件后发生?

Am I going about this all wrong? How do I access the text fields created from a dropdown event after a postback froma submitbutton event occurs?

编辑: 这里的大多数相关的code。

Here's the most of the 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"]);
        }
    }

后来,我试图从这些文本框添加值到字典中。参数是字典的名称。密钥字段是$字符串$,结果是在文本框中什么用户输入

Later on, I'm trying to add the values from these textboxes to a dictionary. Parameters is the name of the dictionary. The key field is the $STRING$, result is what the user entered in the text box.

   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 code。

Here's the .aspx code.

<%@ 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>

编辑:我把这个评论时,我理解了它,但我想我应该把它移动到这个问题,因此更加明显:

I put this in a comment when I figured it out, but I figured I should move it into the question so it is more visible:

想我应该更新这个。我觉得自己像一个有点白痴,但我还是设法得到这个工作。基本上我被分配控制的ID等于替换令牌(因此n =$ FIRSTNAME $例如)。它甚至没有曙光,我是那样的$会引起任何问题。当我刚换的格式ID =名字,它完美的作品。感谢您所有的帮助!

Thought I should update this. I feel like a bit of an idiot, but I did manage to get this working. Basically I was assigning the controls an ID equal to the replacement tokens (So ID="$FIRSTNAME$" for example). It didn't even dawn on me that the $'s would cause any issues. When I just changed to the format ID="Firstname" it works perfectly. Thank you for all of the help!

推荐答案

你是对的,这是所有关于页的生命周期。动态创建的控件必须重新创建在 Page_Init 阶段,为了生存的的视图状态结合的阶段。这意味着,将有以某种方式(使用会话,也许)店有多少文本框您已经在$创建p $ pvious处理,重新创建。提醒要使用相同的ID ,然后将它们添加到您的控件树(中继器或者你正在使用别的东西)。

You're right, it's all about the page lifecycle. Dynamically created controls must be re-created at the Page_Init stage, in order to exist before the viewstate binding stage. This means that will have to somehow (using the Session, maybe) store how many textboxes you have created on the previous processing to recreate them. Remind to use the same ID and to add them to your control tree (a repeater or something else that you're using).

更新

让我给你一个建议:  1.声明类型的类属性名单,其中,文本框&GT; (姑且称之为 CreatedTextBoxes

Let me give you a suggestion: 1. Declare a class attribute of type List<TextBox> (let's call it CreatedTextBoxes)

  1. 声明接收任何它需要创建文本框的方法。此方法不能读取它的适用范围之外的任何内容。它只是接受一些的args,创建文本框,并将它们添加到另一个控件(如转发)。添加创建 CreatedTextBoxes

在下拉菜单更改事件,阅读选项,将它保存到会话键,调用此方法

At the dropdown change event, read the option, save it to the Session and call this method

Page_Init ,确认在会话的对象。如果它是null或空,什么都不干。如果有一个值,调用同样的方法,将相同的args

At Page_Init, verify that object at the Session. If it's null or empty, don't do anything. If it has a value, call that same method, passing the same args

这篇关于从在asp.net中动态创建文本框获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆