C # ASP.NET Binding Controls via a generic method

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 ?

  • Dave
+3
2

DataSource DataBind() . T .

0

, 3 , , . "Bindable", , .

0

Source: https://habr.com/ru/post/1743568/


All Articles