向Table控件添加行时出现问题 [英] Problem in adding rows to Table control

查看:76
本文介绍了向Table控件添加行时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我的asp表有两个单元格。一个是文本框,另一个是图像(+或 - )。

当我点击+时,我想要添加一个新行,文本框和图像' - '。

点击' - '图像时,相应的行将被删除。



当我点击'+'时,正在添加新行'第一次。但是当我第二次点击+时,它正在替换现有的行。



我的.cs代码:

Hi,

My asp table has two cells. One is textbox, another one is an image (+ or -).
When I click '+', I want a new row to be added with a textbox and an image '-'.
While clicking on that '-' Image, the corresponding row will be deleted.

A new row is being added, when I click '+' on first time. But when I click '+' on second time, it 's replacing the existing row.

My .cs code:

TableRow row = new TableRow();
protected void CreateTextBox(object sender, ImageClickEventArgs e)
        {
            TextBox MyTextBox = new TextBox();
            //Assigning the textbox ID name 
            ImageButton img = sender as ImageButton;
            TableCell cell = new TableCell();
            TableCell cell1 = new TableCell();
            ImageButton minus = new ImageButton();

            cell.ID = "tblCell" + ++system;
            cell1.ID = "tblCell1" + system;
            row.ID = "tblRow" + system;

            minus.ID = "imgSystem" + system;
            minus.ImageUrl = "~/Images/minus.png";

            MyTextBox.ID = "txtSystem" + system;
            MyTextBox.Width = 142;
            MyTextBox.Height = 17;
            MyTextBox.TextMode = TextBoxMode.SingleLine;

            cell.Controls.Add(MyTextBox);
            cell1.Controls.Add(minus);

            row.Cells.Add(cell);
            row.Cells.Add(cell1);
            tbl.Rows.Add(row);
            minus.Click += new System.Web.UI.ImageClickEventHandler(deleteRow);
       }
     protected void deleteRow(object sender, System.Web.UI.ImageClickEventArgs e)
      {
            TableRow row = new TableRow();
            ImageButton img = sender as ImageButton;
            string val = img.ID;
            string id = val.Substring(val.Length - 1, 1);
            if (val.Contains("System"))
            {
                TableRow rw = (TableRow)row.FindControl("tblRow" + id);
                rw.Visible = false;
            }
      }





我的aspx代码:



My aspx code:

<asp:Table ID="tbl" runat="server">
        <asp:TableRow runat="server">
           <asp:TableCell runat="server">
                 <asp:TextBox ID="txtSystem1" runat="server"></asp:TextBox>
           </asp:TableCell>
        <asp:TableCell runat="server">
            <asp:ImageButton ID="imgSystem" runat="server" AlternateText="System"      ImageUrl="~/Images/plus1.png" OnClick="CreateTextBox" />
        </asp:TableCell>
        </asp:TableRow>
</asp:Table>





请帮忙。在此先感谢



Please help. Thanks in advance

推荐答案

在CreateTextBox事件中创建新的行实例

create new instance of row inside CreateTextBox event
protected void CreateTextBox(object sender, ImageClickEventArgs e)
        {
            ...
            TableRow row = new TableRow(); 
            row.Cells.Add(cell);
            row.Cells.Add(cell1);
            tbl.Rows.Add(row);
            minus.Click += new System.Web.UI.ImageClickEventHandler(deleteRow);
       }



快乐编码!

:)


Happy Coding!
:)


Hi Mohan ...

问题在于回发...

看看这个链接....它清楚地解释了如何实现相同的

在按钮上的ASP表中动态添加行点击事件
Hi Mohan...
the issue is with the postback...
Have a look at this link.... it clearly explains how to achieve the same
Dynamically Adding Rows in ASP Table on Button Click event


这篇关于向Table控件添加行时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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