I have several web applications that I support, and very often I write the same block of code over and over again to associate a GridView with a data source. I am trying to create a universal method for processing data binding, but I am having problems with its work with relays and data lists.
Here is the general method that I still have:
public void BindControl<T>(T control, SqlCommand sql) where T : System.Web.UI.WebControls.BaseDataBoundControl
{
cmd = sql;
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
control.DataSource = dr;
control.DataBind();
}
dr.Close();
cn.Close();
}
That way, I can just define my CommandText and then make a call to “BindControls (myGridView, cmd)” instead of redialing the same base block of code every time I need to snap a grid.
, . "DataSource" "DataBind" . - , , .
GridView, Datalist Repeater "DataBind" BaseDataBoundControl, BaseDataList Repeater. , ? 3 ?