首页 > 学院 > 开发设计 > 正文

asp.net开发--细节总结3【后台动态创建控件】

2019-11-17 01:43:44
字体:
来源:转载
供稿:网友

asp.net开发--细节总结3【后台动态创建控件】

先看效果图,点击【添加】则新增一条数据,点击相应的删除按钮,则删除该条记录;

代码如下:

PRotected void Page_Load(object sender, EventArgs e)

{

CreateTable();

}

/// <summary>

/// 新增一条收据

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

protected void btn_shouju_add_Click(object sender, EventArgs e)

{

if(string.IsNullOrEmpty(txt_shouju_number.Text))

{

lb_msg_shouju.Text = "收据号码不能为空!";

return;

}

if (ViewState["shoujuCount"] == null || ViewState["shoujuCount"].ToString() == "0")

{

ViewState["shoujuCount"] = 1;

ViewState["shoujuNumbers"] = txt_shouju_number.Text;

ViewState["shoujuStates"] = ddl_shouju_state.SelectedItem.Text;

}

else

{

string shoujuNumber = ViewState["shoujuNumbers"].ToString()+"|";

if(shoujuNumber.Contains(txt_shouju_number.Text+"|"))

{

lb_msg_shouju.Text = "已经存在该收据,请重新添加!";

return;

}

ViewState["shoujuCount"] = int.Parse(ViewState["shoujuCount"].ToString()) + 1;

ViewState["shoujuNumbers"] += "|" + txt_shouju_number.Text;

ViewState["shoujuStates"] += "|" + ddl_shouju_state.SelectedItem.Text;

}

CreateTable();

}

/// <summary>

/// 创建收据列表

/// </summary>

void CreateTable()

{

int count = ViewState["shoujuCount"] == null ? 0 : Convert.ToInt32(ViewState["shoujuCount"]);

List<string> numbers = ViewState["shoujuNumbers"] != null ? strsToList(ViewState["shoujuNumbers"].ToString()) : null;

List<string> states = ViewState["shoujuStates"] != null ? strsToList(ViewState["shoujuStates"].ToString()) : null;

tb_shouju_list.Controls.Clear();

if (count > 0)

{

#region 创建表头

TableHeaderRow head_row = new TableHeaderRow();

TableHeaderCell head_cell_number = new TableHeaderCell();

TableHeaderCell head_cell_state = new TableHeaderCell();

TableHeaderCell head_cell_delete = new TableHeaderCell();

Literal head_lateral_number = new Literal();

head_lateral_number.Text = "收据号码";

Literal head_lateral_state = new Literal();

head_lateral_state.Text = "收据状态";

Literal head_lateral_delete = new Literal();

head_lateral_delete.Text = "操作";

head_cell_number.Controls.Add(head_lateral_number);

head_cell_state.Controls.Add(head_lateral_state);

head_cell_delete.Controls.Add(head_lateral_delete);

head_row.Controls.Add(head_cell_number);

head_row.Controls.Add(head_cell_state);

head_row.Controls.Add(head_cell_delete);

tb_shouju_list.Rows.Add(head_row);

#endregion

for (int i = 0; i < count; i++)

{

#region 循环遍历创建表主体

TableRow row = new TableRow();

row.ID = "tr_" + i;

TableCell cell_number = new TableCell();

TableCell cell_state = new TableCell();

TableCell cell_delete = new TableCell();

Literal literal_number = new Literal();

literal_number.Text = numbers[i];

Literal literal_state = new Literal();

literal_state.Text = states[i];

Button btn_temp = new Button();

btn_temp.Text = "删除";

btn_temp.Attributes["delID"] = i.ToString();

btn_temp.Click += new EventHandler(Delete_shoujuRow);

cell_number.Controls.Add(literal_number);

cell_state.Controls.Add(literal_state);

cell_delete.Controls.Add(btn_temp);

row.Controls.Add(cell_number);

row.Controls.Add(cell_state);

row.Controls.Add(cell_delete);

tb_shouju_list.Rows.Add(row);

#endregion

}

}

}

List<string> strsToList(string str)

{

if (!string.IsNullOrEmpty(str.Trim()))

{

string[] strs = str.Split('|');

List<string> list = new List<string>();

list.AddRange(strs);

return list;

}

return null;

}

/// <summary>

/// 删除--添加的行

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

void Delete_shoujuRow(object sender, EventArgs e)

{

Button btn = (Button)sender;

int id = Convert.ToInt32(btn.Attributes["delID"]);

int count = ViewState["shoujuCount"] == null ? 0 : Convert.ToInt32(ViewState["shoujuCount"]);

List<string> numbers = ViewState["shoujuNumbers"] != null ? strsToList(ViewState["shoujuNumbers"].ToString()) : null;

List<string> states = ViewState["shoujuStates"] != null ? strsToList(ViewState["shoujuStates"].ToString()) : null;

if (count > 0)

{

numbers.RemoveAt(id);

states.RemoveAt(id);

ViewState["shoujuNumbers"] = string.Join("|", numbers);

ViewState["shoujuStates"] = string.Join("|", states);

ViewState["shoujuCount"] = count - 1;

}

CreateTable();

}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表