一个保存视图状态的小例子

using System;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        TableMethod();
    }

    private void TableMethod()
    {
        Random random = new Random();
        int rowCount;//行数
        int colCount;//列数
        rowCount = int.Parse(TB_row.Text);
        colCount = int.Parse(TB_col.Text);
        ViewState["rowCount"] = rowCount;
        ViewState["colCount"] = colCount;
        int CurrentRow;//当前行
        int CurrentCol;//当前列
        for (CurrentRow = 1; CurrentRow <= rowCount; CurrentRow++)
        {
            TableRow tableRow = new TableRow();
            Tb_text.Rows.Add(tableRow);//在 Tb_text中添加行
            for (CurrentCol = 1; CurrentCol <= colCount; CurrentCol++)
            {
                TableCell tableCell = new TableCell();
                Image image = new Image();
                image.ImageUrl = string.Format("死神{0}.jpg", random.Next(3) + 1);
                tableCell.Controls.Add(image);
                tableRow.Cells.Add(tableCell);
            }
            this.Tb_text.Rows.Add(tableRow);
        }
    }
    protected void LB_died_SelectedIndexChanged(object sender, EventArgs e)
    {
      
        if (this.LB_died.SelectedIndex >= 0)
        {
            this.Image_died.ImageUrl = string.Format("死神{0}.jpg", this.LB_died.SelectedIndex + 1);
        }

        if (ViewState["rowCount"] != null)
        {

            this.TB_row.Text = ViewState["rowCount"].ToString();
            this.TB_col.Text = ViewState["colCount"].ToString();
            TableMethod();
        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值