如何删除我的动态添加文本? [英] How do I remove my dynamically added textbox?

查看:117
本文介绍了如何删除我的动态添加文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里

在我的codebehind我创建了一个方法来添加 ASP:文本框动态

Create按钮操作来创建文本字段

 列表<串GT; controlIdList =新的List<串GT;();
INT计数器= 0;
保护覆盖无效LoadViewState(对象savedState)
{
    base.LoadViewState(savedState);
    controlIdList =(列表<串GT;)的ViewState [controlIdList];
    的foreach(字符串ID在controlIdList)
    {
        反++;
        文本框TB =新的TextBox();
        tb.ID = ID;
        LiteralControl断行=新LiteralControl(< BR />中);
        LiteralControl openLI =新LiteralControl(<立GT;);
        LiteralControl closeLI =新LiteralControl(< /李>);        PlaceHolder1.Controls.Add(openLI);
        PlaceHolder1.Controls.Add(TB);
        PlaceHolder1.Controls.Add(closeLI);
        PlaceHolder1.Controls.Add(断行);
    }
}
保护无效btnAddTextBox_Click(对象发件人,EventArgs的发送)
{
    的foreach(在PlaceHolder1.Controls控制控制)
    {        反++;
        文本框TB =新的TextBox();
        tb.ID =文本框+计数器;        LiteralControl断行=新LiteralControl(< BR />中);
        PlaceHolder1.Controls.Add(TB);
        PlaceHolder1.Controls.Add(断行);
        controlIdList.Add(tb.ID);
        的ViewState [controlIdList] = controlIdList;
    }
}

现在,当我尝试删除按钮,所有发生的回发。控制不会被删除。以下是我曾尝试:

编辑
这工作,但现在由于某种原因,文本框不会删除我第一次点击第二个按钮,点击文本框获取删除

 保护无效btnRemoveTextBox_Click(对象发件人,EventArgs的发送)
{
    的foreach(在PlaceHolder1.Controls控制控制)
    {
        VAR TB =新的TextBox();
        tb.ID =文本框+计数器;
        如果((control.ID == tb.ID.ToString())及&放大器;!(control.ID =空))
        {
            controlIdList.Remove(tb.ID);
            的ViewState [controlIdList] = controlIdList;
        }
    }
}


解决方案

您需要让您的数据封装在一个的更新面板的,否则每一个你的页面有时间去到服务器的回发的发生。这样,当你打的逻辑服务器,您删除逻辑的确可以执行没有的回发

您也可以简单地控制可见性设置为false。

另一种可能性是通过客户端,这将是为简单.hide()做一大块这一点; 。显示(); 使用jQuery。

同时请记住你的方法会造成维护困难的视图状态的。所提出的解决方案,一个的更新面板的保存在内存中的页面,然后通过Ajax重新创建它。

另一个问题

< ASP:的UpdatePanel> 将是你需要避免什么的回发

老实说,你可以做到这一点很简单,如:

  ... btnRemoveTextbox_Click(...,...)
{
     txtDynamic.Visibility = FALSE;
}

就像我说的,但你可以做到这一点的服务器,你可能是最好的客户端的再调用,一个的服务

重要:如果这是你的事件处理程序的名字,你不应该当你有两个独立的任务设计他们两个是相同的。


更新/示例

  //前端:
< ASP:的ScriptManager ID =scmPage=服务器>< / ASP:ScriptManager的>
< ASP:的UpdatePanel ID =updDemo=服务器>
    <&的ContentTemplate GT;
        < ASP:占位符ID =plhControl=服务器>< / ASP:占位符>
        < ASP:按钮的ID =btnCreateText=服务器文本=创建的OnClick =btnCreateText_Click/>
        < ASP:按钮的ID =btnRemoveText=服务器文本=删除的OnClick =btnRemoveText_Click/>
    < /&的ContentTemplate GT;
< / ASP:的UpdatePanel>//后端:
保护无效btnCreateText_Click(对象发件人,EventArgs的发送)
{
    VAR文本=新的TextBox();
    text.ID =txtDynamic;    plhControl.Controls.Add(文本);}保护无效btnRemoveText_Click(对象发件人,EventArgs的发送)
{
    VAR文本=新的TextBox();
    text.ID =txtDynamic;
    text.Visible = FALSE;
}

和实际工作,我刚试了一下。有一点要注意在这里,你需要确保你有一个有效的身份证件和填充适当的控制。

这code是令人难以置信的粗糙,因为它适用于具有单一ID或包含验证控制它不会是明智的生产。您将需要遍历控制,确保检测到正确的ID,并试图删除之前就已存在。你需要调试code和验证。

here in my codebehind I have created a way to add asp:Textbox dynamically

Create button action to create text field

List<string> controlIdList = new List<string>();
int counter = 0;
protected override void LoadViewState(object savedState)
{
    base.LoadViewState(savedState);
    controlIdList = (List<string>)ViewState["controlIdList"];
    foreach (string Id in controlIdList)
    {
        counter++;
        TextBox tb = new TextBox();
        tb.ID = Id;
        LiteralControl linebreak = new LiteralControl("<br />");
        LiteralControl openLI = new LiteralControl("<li>");
        LiteralControl closeLI = new LiteralControl("</li>");

        PlaceHolder1.Controls.Add(openLI);
        PlaceHolder1.Controls.Add(tb);
        PlaceHolder1.Controls.Add(closeLI);
        PlaceHolder1.Controls.Add(linebreak);
    }
}


protected void btnAddTextBox_Click(object sender, EventArgs e)
{
    foreach (Control control in PlaceHolder1.Controls)
    {               

        counter++;
        TextBox tb = new TextBox();
        tb.ID = "Textbox" + counter;

        LiteralControl linebreak = new LiteralControl("<br />");
        PlaceHolder1.Controls.Add(tb);
        PlaceHolder1.Controls.Add(linebreak);
        controlIdList.Add(tb.ID);
        ViewState["controlIdList"] = controlIdList;
    }
}

Now when I try to remove the button all that happens is a postback. The control does not get removed. Here is what I have tried:

Edited This works now but for some reason the textbox does not delete the first time I click the button on the second click the textbox gets removed

protected void btnRemoveTextBox_Click(object sender, EventArgs e)
{
    foreach (Control control in PlaceHolder1.Controls)
    {
        var tb = new TextBox();
        tb.ID = "Textbox" + counter;
        if ((control.ID == tb.ID.ToString()) && (control.ID != null))
        {
            controlIdList.Remove(tb.ID);
            ViewState["controlIdList"] = controlIdList;
        }
    }
}       

解决方案

You'll need to have your data wrapped in an Update Panel, otherwise every time your page has to go to the server a Postback will occur. That way when your logic hits the server, your remove logic can indeed execute without the Postback.

You could also simply set the control visibility to false.

Another possibility would be to do a large chunk of this via the Client, which would be as simple as .hide(); or .show(); with jQuery.

Also keep in mind your approach will create difficulties in maintaining the View State. Another issue with the proposed solution, an Update Panel stores the page in memory then recreates it via Ajax.

<asp:UpdatePanel> would be what you need to avoid a Postback.

Honestly you could do it quite simply like:

... btnRemoveTextbox_Click(..., ...)
{
     txtDynamic.Visibility = false;
}

As I said, though you can do this with the server you may be better off with Client Side then call, a Service.

Important: If those are your Event Handler names, you shouldn't have both be identical when you have two separate task designed for them.


Update/Example

// Front-End:
<asp:ScriptManager ID="scmPage" runat="server" ></asp:ScriptManager>
<asp:UpdatePanel ID="updDemo" runat="server">
    <ContentTemplate>
        <asp:PlaceHolder ID="plhControl" runat="server"></asp:PlaceHolder>
        <asp:Button ID="btnCreateText" runat="server" Text="Create" OnClick="btnCreateText_Click" />
        <asp:Button ID="btnRemoveText" runat="server" Text="Remove" OnClick="btnRemoveText_Click" />
    </ContentTemplate>
</asp:UpdatePanel>

// Backend:
protected void btnCreateText_Click(object sender, EventArgs e)
{
    var text = new TextBox();
    text.ID = "txtDynamic";

    plhControl.Controls.Add(text);

}

protected void btnRemoveText_Click(object sender, EventArgs e)
{
    var text = new TextBox();
    text.ID = "txtDynamic";
    text.Visible = false;
}

And that actually works, I just tried it. One thing to note here, you need to ensure you have a valid id and populate the proper control.

This code is incredibly rough, it wouldn't be wise for production because it works for a control that has that single Id or contain validation. You would need to loop through the controls, ensure the proper Id is detected and exist before attempting to remove. You'll need to debug your code and validate.

这篇关于如何删除我的动态添加文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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