您的位置:首页 > 编程语言 > ASP

C#(asp.net web) 从后台生成提示框/控件并添加方法(实例如下)

2020-07-16 05:33 1076 查看

@[TOC](C#(asp.net web) 从后台生成提示框/控件并添加方法(实例如下)
)

C#(asp.net web) 从后台生成多个提示框/控件并添加方法(实例如下)

实现功能页面如下(页面的内容都是从后台生成的):

Q.aspx具体代码如下:

<form id="form1" runat="server">

Q.aspx.cs具体代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

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

/// <summary>
/// 构造提示框
/// </summary>
/// <param name="btn">关闭按钮</param>
/// <param name="itemName">项目名称</param>
/// <param name="itemTime">项目日期</param>
/// <param name="conText">诊断内容</param>
/// <returns>返回提示框</returns>
public void S() {
Table[] tb1= new Table[2];
HtmlInputButton[] btn = new HtmlInputButton[2];
for (int i = 0; i < 2; i++)
{
btn[i]=new HtmlInputButton();
btn[i].ID = "btn" + i;

tb1[i] = S1(btn[i], i + "AAA","2020/4/30", "1、辅导书发放进度表对方的做开发的发电机组\n2、搞对象贵绳股份了国防大学联发科多个翻到了");
tb1[i].ID = "tbName" + i;
BB1.Controls.Add(tb1[i]);
}
}

public Table S1(HtmlInputButton btn, string itemName, string itemTime,string conText)
{
#region 表格样式设置
Table table = new Table();
table.BorderWidth = 1;
table.BorderColor = System.Drawing.Color.Black;
table.CellSpacing = 0;
table.CellPadding = 1;
table.Width = 230;
#endregion

#region 标题//提示框
TableRow headerRow = new TableHeaderRow();
headerRow.BackColor = System.Drawing.Color.Silver;

var cell1 = new TableCell();
headerRow.TableSection = TableRowSection.TableHeader;

Label lb=new Label();
lb.Text="智能提醒";
lb.Width=205;
cell1.Controls.Add(lb);

btn.Value = "X";//设置按钮值
btn.ServerClick += new EventHandler(btn_ServerClick);
cell1.Controls.Add(btn); //将此按钮搁到table格中

headerRow.Cells.Add(cell1);
table.Rows.Add(headerRow);
#endregion

#region
TableRow row2 = new TableRow();
var cell2 = new TableCell();
//cell12.ColumnSpan = 2;   //合并单元格
cell2.Text = "项目:" + itemName;
row2.Cells.Add(cell2);
//设置背景颜色
row2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(235)))), ((int)(((byte)(235)))), ((int)(((byte)(228)))));
table.Rows.Add(row2);

TableRow row3 = new TableFooterRow();
var cell3 = new TableCell();
cell3.Text = "时间:" + itemTime;
cell3.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(235)))), ((int)(((byte)(235)))), ((int)(((byte)(228)))));
row3.Cells.Add(cell3);
//row3.TableSection = TableRowSection.TableFooter;
table.Rows.Add(row3);

TableRow row4 = new TableRow();
var cell4 = new TableCell();
cell4.Text = "";
cell4.Height = 10;
cell4.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(235)))), ((int)(((byte)(235)))), ((int)(((byte)(228)))));
row4.Cells.Add(cell4);
table.Rows.Add(row4);
#endregion

#region 内容
TableRow row5 = new TableRow();
var cell5 = new TableCell();
cell5.Text = "内容:";
cell5.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(235)))), ((int)(((byte)(235)))), ((int)(((byte)(228)))));
row5.Cells.Add(cell5);
table.Rows.Add(row5);

TableRow row6 = new TableRow();
var cell61 = new TableCell();

HtmlTextArea txtArea = new HtmlTextArea();
txtArea.Value = conText;
txtArea.Cols = 30;
txtArea.Rows = 10;
txtArea.Disabled = true;

cell61.Controls.Add(txtArea);
row6.Cells.Add(cell61);
table.Rows.Add(row6);
#endregion

return table;
}

/// <summary>
/// 关闭按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void btn_ServerClick(object sender, EventArgs e)
{
HtmlInputButton cb = (HtmlInputButton)sender; //sender:获取当前对象
string tbName = GetParentID(cb);
foreach (System.Web.UI.Control s in BB1.Controls)
{
if (s is Table && s.ID == tbName)
(s as Table).Visible = false;
}

// 如果Panel中没有看见则隐藏
if (GetTableCount(BB1) == 0)
BB1.Visible = false;
}

/// <summary>
/// 获取某种控件在页面中的数量(可以直接替换 Table 求其他控件数量)
/// </summary>
/// <param name="parentCountol"></param>
/// <returns>所求控件数量</returns>
public int GetTableCount(System.Web.UI.Control parentCountol)//获取某种控件在页面中的数量
{
int Count = 0;
foreach (System.Web.UI.Control c in parentCountol.Controls)
{
if (c is System.Web.UI.WebControls.Table && c.Visible)
{
Count++;
}
Count += GetTableCount(c);
}
return Count;
}

/// <summary>
/// 根据控件对象获取当前控件所在的父控件
/// </summary>
/// <param name="child">控件对象</param>
/// <returns>父容器ID值</returns>
public string GetParentID(Control child)
{
if (child.Parent == null)
{
throw new Exception("There is no such a parent.");
}
if (child.Parent is Table) //Table:父容器类型,根据需要改变
{
return child.Parent.ID;
}
return GetParentID(child.Parent);
}

/// <summary>
/// 处理数据
/// </summary>
public void ProcessingData(ref List<string> listName, ref List<string> listContext)
{
List<string> tempName = listName;
getNonRepetitiveData(ref tempName);
List<string> tempContext = new List<string>();
for (int i = 0; i < tempName.Count; i++)
{
int num = 1;
string str="";
for (int j = 0; j < listName.Count; j++)
{
if (listName[j]==tempName[i])
{
str += num + "、" + listContext[j] + "\n";
num++;
}
}
tempContext.Add(str);
}

listName = tempName;
listContext = tempContext;
}
/// <summary>
/// 获取listname中非重复的数据
/// </summary>
/// <param name="listName">非重复数据</param>
public void getNonRepetitiveData(ref List<string> listName)
{
List<string> tempName = new List<string>();
for (int i = 0; i < listName.Count; i++)
{
if (tempName.Count == 0)
tempName.Add(listName[i]);
else if (!tempName.Contains(listName[i]))
tempName.Add(listName[i]);
}

listName = tempName;
}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: