我正在处理一个asp.net网页,该网页将/可以一次由多个用户使用。为了组织我的代码,我做了一些所谓的“ HelperClasses”。 例如:
public static class GridViewHelper
{
public static List<TableCell> GetCellsOfRow(GridViewRow gvRow, int start, int ende)
{
try
{
List<TableCell> cellList = new List<TableCell>();
for (int i = start; i < ende; i++)
{
cellList.Add(gvRow.Cells[i]);
}
return cellList;
}
catch (Exception)
{
throw;
}
}
public static void BindGridview(GridView gv, DataTable dt)
{
gv.DataSource = dt;
gv.DataBind();
}
public static void NullGridViewDataSource(params GridView[] gvList)
{
try
{
for (int i = 0; i < gvList.Length - 1; i++)
{
gvList[i].DataSource = null;
gvList[i].DataBind();
}
}
catch (Exception)
{
throw;
}
}
public static int GetColIndex(GridView gv, string colName)
{
try
{
int colIndex = -1;
foreach (DataControlField col in gv.Columns)
{
if (col.HeaderText == colName)
{
colIndex = gv.Columns.IndexOf(col);
}
}
if (colIndex == -1)
{
throw new Exception("Spalte existiert nicht!");
}
return colIndex;
}
catch (Exception)
{
throw;
}
}
}
如您所见,我只需要一些其他方法来处理GridView。 但是,由于我仍在学习并且并不真正了解所有约定,所以我有几个问题要问:
1)对于上面的示例:这样的帮助器类是一个好的解决方案吗?还是应该创建一个从常规GridView类扩展的类,并在aspx页面上注册控件?如果前一种情况适用,那么:我应该了解哪些约定?我问是因为我害怕静态关键字。据我所知,静态(全局)变量在所有用户之间共享,并且我不希望因为任何共享范围而遇到问题。
2)与SQL Connections一起使用时,关于HelperClasses有潜在的危险吗?例如,使用这样的方法来创建一个帮助器类:
public static DataTable GetData(SqlCommand cmd, string conString)
{
try
{
using (SqlConnection con = DB_Connections.getConnection(conString))
{
cmd.Connection = con;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
sda.SelectCommand.CommandType = CommandType.StoredProcedure;
DataTable dt = new DataTable();
con.Open();
sda.Fill(dt);
return dt;
}
}
}
catch (SqlException)
{
throw;
}
}
3)我想知道是否可以在没有任何主要缺点的情况下访问和更改帮助器类中的ViewState?
由于我仍在学习如何改善编码并大致理解一些概念,所以我真的希望有人可以在这里帮助我。预先感谢大家!