程序要求:从testa.aspx传过来的值经过解拆后把得到的值赋给动态生成的label,并且,该动态生成的行列中还另外包含有一个textbox,一个checkbox,该页面还包含一个保存按钮和一个删除按钮,删除按钮的功能是删除checkbox选中的行,保存按钮的功能是获取所有生成行的几个列值。我的代码如下:(testa.aspx传过来的值为:1$A$a|2$B$b|3$C$c|4$D$d|5$E$e)testb.aspx
HTML内容:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testb.aspx.cs" Inherits="share_testb" %><!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>
      <table>
        <tr>
          <td>
            <asp:Table id="Table1" style="Z-INDEX: 101; LEFT: 256px; POSITION: absolute; TOP: 160px" runat="server"
Width="224px" Height="104px"></asp:Table>
<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 264px; POSITION: absolute; TOP: 64px" runat="server"
Width="64px" Text="save"></asp:Button>
<asp:Button id="Button2" style="Z-INDEX: 103; LEFT: 416px; POSITION: absolute; TOP: 64px" runat="server"
Text="delete" OnClick="Button2_Click"></asp:Button>
          </td>
        </tr>
      </table>
    </div>
    </form>
</body>
</html>CS内容:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class share_testb : System.Web.UI.Page
{
    string[] getsel;
    string[] getinfo = new string[3];        protected void Page_Load(object sender, EventArgs e)
    {
        ArrayList al = new ArrayList();
        if (!this.IsPostBack)
        {
            string s = "";
            if (Request.QueryString["s"] != null)
            {
                s = Request.QueryString["s"];
            }
            getsel = s.Split('|');            for (int j = 0; j < getsel.Length; j++)
            {
                al.Add(getsel[j]);
            }
            Session["al"] = al;
        }
        this.BindTableRow();
    }    private void BindTableRow()
    {
        this.Table1.Rows.Clear();
        ArrayList al = (ArrayList)Session["al"];        TableRow tempRow = new TableRow();
        TableCell[] tempCell = new TableCell[5];
        for (int k = 0; k < 5; k++)
        {
            tempCell[k] = new TableCell();
            switch (k)
            {
                case 0:
                    tempCell[k].Text = "列A";
                    tempCell[k].Attributes.Add("align", "center");
                    tempCell[k].Style.Value = "width:20%; border-right: #0066ff thin solid; border-top: #0066ff thin solid; border-left: #0066ff thin solid; border-bottom: #0066ff thin solid; font-weight: bold; font-size: 10pt; color: #000000; font-family: 宋体;";
                    break;
                case 1:
                    tempCell[k].Text = "列B";
                    tempCell[k].Attributes.Add("align", "center");
                    tempCell[k].Style.Value = "width:20%; border-right: #0066ff thin solid; border-top: #0066ff thin solid; border-left: #0066ff thin solid; border-bottom: #0066ff thin solid; font-weight: bold; font-size: 10pt; color: #000000; font-family: 宋体;";
                    break;
                case 2:
                    tempCell[k].Text = "列C";
                    tempCell[k].Attributes.Add("align", "center");
                    tempCell[k].Style.Value = "width:20%; border-right: #0066ff thin solid; border-top: #0066ff thin solid; border-left: #0066ff thin solid; border-bottom: #0066ff thin solid; font-weight: bold; font-size: 10pt; color: #000000; font-family: 宋体;";
                    break;
                case 3:
                    tempCell[k].Text = "数   量";
                    tempCell[k].Attributes.Add("align", "center");
                    tempCell[k].Style.Value = "width:20%; border-right: #0066ff thin solid; border-top: #0066ff thin solid; border-left: #0066ff thin solid; border-bottom: #0066ff thin solid; font-weight: bold; font-size: 10pt; color: #000000; font-family: 宋体;";
                    break;
                case 4:
                    tempCell[k].Text = "操   作";
                    tempCell[k].Attributes.Add("align", "center");
                    tempCell[k].Style.Value = "width:20%; border-right: #0066ff thin solid; border-top: #0066ff thin solid; border-left: #0066ff thin solid; border-bottom: #0066ff thin solid; font-weight: bold; font-size: 10pt; color: #000000; font-family: 宋体;";
                    break;
            }
            tempRow.Cells.Add(tempCell[k]);
        }
        this.Table1.Rows.Add(tempRow);                for (int i = 0; i < al.Count; i++)
        {
            getinfo = System.Text.RegularExpressions.Regex.Split(al[i].ToString(), @"[$]+");            TableRow tr = new TableRow();
            tr.ID = "row" + i.ToString();            TableCell[] cell = new TableCell[5];            //添加label列
            cell[0] = new TableCell();
            Label lba = new Label();
            lba.ID = "lba" + i.ToString();
            lba.Text = getinfo[0].ToString();
            cell[0].Controls.Add(lba);
            tr.Cells.Add(cell[0]);            //添加label列
            cell[1] = new TableCell();
            Label lbb = new Label();
            lbb.ID = "lbb" + i.ToString();
            lbb.Text = getinfo[1].ToString();
            cell[1].Controls.Add(lbb);
            tr.Cells.Add(cell[1]);            //添加label列
            cell[2] = new TableCell();
            Label lbc = new Label();
            lbc.ID = "lbc" + i.ToString();
            lbc.Text = getinfo[2].ToString();
            cell[2].Controls.Add(lbc);
            tr.Cells.Add(cell[2]);            //添加textbox列
            cell[3] = new TableCell();
            TextBox tb = new TextBox();
            tb.ID = "tb" + i.ToString();
            cell[3].Controls.Add(tb);
            tr.Cells.Add(cell[3]);            //添加checkbox列
            cell[4] = new TableCell();
            CheckBox cb = new CheckBox();
            cb.ID = "cb" + i.ToString();
            cell[4].Controls.Add(cb);
            tr.Cells.Add(cell[4]);            this.Table1.Rows.Add(tr);
        }
    }    protected void Button2_Click(object sender, EventArgs e)
    {
        ArrayList al = (ArrayList)Session["al"];
        int count = this.Table1.Rows.Count;
        int l = 0;        for (int j = 0; j < count; j++)
        {
            if (j > 0)
            {
                l = j - 1;
                if ((this.FindControl("cb" + l.ToString()) as CheckBox).Checked == true)
                {
                    al.Remove((this.FindControl("lba" + l.ToString()) as Label).Text);
                    al.Remove((this.FindControl("lbb" + l.ToString()) as Label).Text);
                    al.Remove((this.FindControl("lbc" + l.ToString()) as Label).Text);
                    this.Table1.Rows.Remove((this.FindControl("row" + l.ToString()) as TableRow));
                }
            }
        }
        Session["al"] = al;
        this.BindTableRow();
    }
}现在出现的问题是:按了删除按钮后,没有删掉任何行!

解决方案 »

  1.   

    楼主调试看看点击删除按钮后session的内容变了没有
    还有this.FindControl("row" + l.ToString())可以这样用么
      

  2.   

    调试是没有报任何错的,这段代码奇怪在如果我把
    switch (k)
    {
    case 0:
    tempCell[k].Text = "列A";
    tempCell[k].Attributes.Add("align", "center");
    tempCell[k].Style.Value = "width:20%; border-right: #0066ff thin solid; border-top: #0066ff thin solid; border-left: #0066ff thin solid; border-bottom: #0066ff thin solid; font-weight: bold; font-size: 10pt; color: #000000; font-family: 宋体;";
    break;
    case 1:
    tempCell[k].Text = "列B";
    tempCell[k].Attributes.Add("align", "center");
    tempCell[k].Style.Value = "width:20%; border-right: #0066ff thin solid; border-top: #0066ff thin solid; border-left: #0066ff thin solid; border-bottom: #0066ff thin solid; font-weight: bold; font-size: 10pt; color: #000000; font-family: 宋体;";
    break;
    case 2:
    tempCell[k].Text = "列C";
    tempCell[k].Attributes.Add("align", "center");
    tempCell[k].Style.Value = "width:20%; border-right: #0066ff thin solid; border-top: #0066ff thin solid; border-left: #0066ff thin solid; border-bottom: #0066ff thin solid; font-weight: bold; font-size: 10pt; color: #000000; font-family: 宋体;";
    break;
    case 3:
    tempCell[k].Text = "数 量";
    tempCell[k].Attributes.Add("align", "center");
    tempCell[k].Style.Value = "width:20%; border-right: #0066ff thin solid; border-top: #0066ff thin solid; border-left: #0066ff thin solid; border-bottom: #0066ff thin solid; font-weight: bold; font-size: 10pt; color: #000000; font-family: 宋体;";
    break;
    case 4:
    tempCell[k].Text = "操 作";
    tempCell[k].Attributes.Add("align", "center");
    tempCell[k].Style.Value = "width:20%; border-right: #0066ff thin solid; border-top: #0066ff thin solid; border-left: #0066ff thin solid; border-bottom: #0066ff thin solid; font-weight: bold; font-size: 10pt; color: #000000; font-family: 宋体;";
    break;
    }
    tempRow.Cells.Add(tempCell[k]);
    }
    this.Table1.Rows.Add(tempRow); 
    这段动态生成表头的代码去掉,表头用html方式生成的话,整个程序就是正常的!
      

  3.   

    al.Remove((this.FindControl("lba" + l.ToString()) as Label).Text);
    al.Remove((this.FindControl("lbb" + l.ToString()) as Label).Text);
    al.Remove((this.FindControl("lbc" + l.ToString()) as Label).Text);将这三行替换成:string strValue = (this.FindControl("lba" + l.ToString()) as Label).Text + "$"
    + (this.FindControl("lbb" + l.ToString()) as Label).Text + "$"
    + (this.FindControl("lbc" + l.ToString()) as Label).Text;
    al.Remove(strValue);因为你的ArrayList中每一项的值是:1$A$a,2$B$b,....还有你的动态表头中:
    tempCell[k].Style.Value = "width:20%; border-right: #0066ff thin solid; border-top: #0066ff thin solid; border-left: #0066ff thin solid; border-bottom: #0066ff thin solid; font-weight: bold; font-size: 10pt; color: #000000; font-family: 宋体;";
    我用的是2003,编译的时候就会出错,原因是"Style中并不包含对Value的定义",我不知道2005是不是可以这样用.如果也不能直接这样用,可用如下方法逐个添加css属性值:
    tempCell[k].Style.Add("width","20%");
    ...............
      

  4.   

    seesharp(我给代码你给分):老兄,你没有仔细看我的代码,在所有lable中的值已经不再含有$符,只是因为$符是分隔符,label里面放的就是从1$A$a字符串里分离出来的1,A,a这三个值!关于Style:
    我的就是在2005下编译的,没有问题
      

  5.   

    seesharp(我给代码你给分):
    实在抱歉,你的说法是对的,我有点犯晕,把$分隔符当成|分隔符了,散分!