【问题标题】:Dynamically add TextBoxes to dynamically added Panels动态添加文本框到动态添加的面板
【发布时间】:2015-10-16 14:31:32
【问题描述】:

我正在以编程方式将面板(我称此面板为块以便更好地理解)添加到另一个面板。这些块中的每一个都包含一个标题、一个向块中添加文本框的按钮和一个注视文本框。

这是我用来添加文本框的事件:

/// <summary>
/// Adds a text box to the button's parent
/// </summary>
protected void AddLabel_Click(object sender, EventArgs e)
{
    Button senderButton = (Button)sender;
    string parentId = senderButton.ID.Replace("_button","");
    Panel parent = (Panel)FindControl(update_panel, parentId);

    parent.Controls.Add(new TextBox
    {
        CssClass = "form-control canvas-label",
        ID = parent.ID + "_label" + parent.Controls.OfType<TextBox>().Count<TextBox>()
    });
}

但是,每次我添加一个文本框时,我刚刚创建的文本框都会被删除

编辑这就是我最终解决它的方式(感谢 Don):

1) 保留文本框列表

Dictionary<string, List<string>> BlocksLabels
{
    get
    {
        if (ViewState["BlockLabels"] == null)
            ViewState["BlockLabels"] = new Dictionary<string, List<string>>();

        return ViewState["BlockLabels"] as Dictionary<string, List<string>>;
    }
    set { ViewState["BlockLabels"] = value; }
}

2) 在创建块的方法中(从 Page_Load 调用):

if (BlocksLabels.ContainsKey(block.ID))
{
    foreach (string label in BlocksLabels[block.ID])
        block.Controls.Add(new TextBox { ID = labelId });
}
else
{
    // Add one empty canvas label by default
    string labelId = block.ID + "_label0";
    BlocksLabels[block.ID] = new List<string>();
    BlocksLabels[block.ID].Add(labelId);
    block.Controls.Add(new TextBox { ID = labelId });
}

3) 最后,在添加新文本框的情况下

Button senderButton = (Button)sender;
string parentId = senderButton.ID.Replace("_button", "");
Panel targetBlock = (Panel)FindControl(update_panel, parentId);

string labelId = targetBlock.ID + "_label" + BlocksLabels[targetBlock.ID].Count;
BlocksLabels[targetBlock.ID].Add(labelId);
targetBlock.Controls.Add(new TextBox { ID = labelId });

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    在 ASP.NET 中,如果您以编程方式创建控件,则需要在回发期间再次重新创建该控件。

    因此,在您的情况下,解决方案是存储创建的控件的 Id 列表并在回发期间重新创建它们(最好在页面加载事件中)。

    这对于页面控件树与存储的视图状态对齐是必要的。

    【讨论】:

    • 这是使用动态控件时真正的痛苦。
    • 您好,感谢您的回答。存储它的正确/最佳方式是什么?
    • 我会将它存储在列表或数组中,然后将其存储在 ViewState 中。这样列表/数组在回发期间可用
    【解决方案2】:

    请看下面的代码:

    <asp:HiddenField runat="server" ID="hdnTbCnt" Value="0" />
    <asp:Panel runat="server" ID="panel1">
    </asp:Panel>
    <asp:Button Text="Add TextBox" runat="server" OnClick="AddTextBox_Click" />
    

    代码隐藏:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            int tbCnt = Convert.ToInt32(hdnTbCnt.Value);
    
            for (int i = 1; i <= tbCnt; i++)
            {
                var tb = new TextBox()
                {
                    ID = string.Format("txt{0}", i)
                };
    
                panel1.Controls.Add(tb);
            }
        }
    }
    
    protected void AddTextBox_Click(object sender, EventArgs e)
    {
        int tbCount = Convert.ToInt32(hdnTbCnt.Value);
    
        var tb = new TextBox()
        {
            ID = string.Format("txt{0}", tbCount + 1)
        };
    
        panel1.Controls.Add(tb);
    
        hdnTbCnt.Value = (tbCount + 1).ToString();
    }
    

    【讨论】:

    • 你在 aspx 中有 HiddenField 和 Panel,但我需要动态生成它们
    • 如果我们想要一个 removetextbox 事件怎么办......你能不能也发布那个代码部分......我们想看看你是如何执行的......比如你是否会减少 tbcount 值..如何删除特定的文本框...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 2015-07-17
    相关资源
    最近更新 更多