开发者

ASP.NET Two-Way Binding with GridView and Dynamic Templates

I'm trying to get two-way binding working with a simple GridView with Dynamic Template columns. The code for the Template is as follows:

public class GridViewCheckboxTemplate : ITemplate
{
    private DataControlRowType dcrType;
    private string strColumnName;
    private string strDataType;

    public GridViewCheckboxTemplate(DataControlRowType dcrType, string strColumnName, string strDataType)
    {
        this.dcrType = dcrType;
        this.strColumnName = strColumnName;
        this.strDataType = strDataType;
    }

    public void InstantiateIn(System.Web.UI.Control cContainer)
    {
        DataControlFieldCell dcfCell = null;

        switch(dcrType)
        {
            case DataControlRowType.Header:
                cContainer.Controls.Add(new Literal()
                {
                    Text ="开发者_开发百科<b>" + strColumnName + "</b>"
                });
                break;
            case DataControlRowType.DataRow:

                switch(strDataType)
                {
                    case "String":
                        Label lData = new Label();
                        lData.DataBinding += new EventHandler(lData_DataBinding);
                        cContainer.Controls.Add(lData);
                        break;
                    case "Boolean":
                        CheckBox cbData = new CheckBox();
                        cbData.DataBinding += new EventHandler(cbData_DataBinding);
                        cContainer.Controls.Add(cbData);
                        break;
                }
                break;
            default:
                break;
        }
    }

    private void lData_DataBinding(object sender, EventArgs e)
    {
        Label lData = (Label)sender;
        GridViewRow gvRow = (GridViewRow)lData.NamingContainer;

        lData.Text = DataBinder.Eval(gvRow.DataItem, strColumnName).ToString();
    }

    private void cbData_DataBinding(object sender, EventArgs e)
    {
        CheckBox cbData = (CheckBox)sender;
        GridViewRow gvRow = (GridViewRow)cbData.NamingContainer;

        cbData.Checked = Boolean.TrueString.Equals(DataBinder.Eval(gvRow.DataItem, strColumnName).ToString(), StringComparison.CurrentCultureIgnoreCase);
    }
}

The code for creating the DataTable is as follows:

DataTable dtData = new DataTable();
        dtData.Columns.Add(new DataColumn("id", typeof(Int32)) { AutoIncrement = true });
        dtData.Columns.Add("name", typeof(string));

        for(int i = 0; i < 5; i++)
        {
            dtData.Columns.Add(new DataColumn("day-" + i, typeof(Boolean)));
        }

        foreach(Person pPerson in PersonManager.Instance.GetList())
        {
            DataRow drRow = dtData.NewRow();

            drRow["name"] = pPerson.Name;
            drRow["day-0"] = true;
            drRow["day-1"] = true;
            drRow["day-2"] = false;
            drRow["day-3"] = false;
            drRow["day-4"] = false;

            dtData.Rows.Add(drRow);
        }

        for(int i = 0; i < dtData.Columns.Count; i++)
        {
            if(dtData.Columns[i].ColumnName != "id")
            {
                TemplateField tfField = new TemplateField();
                // create the data rows
                tfField.ItemTemplate = new GridViewCheckboxTemplate(DataControlRowType.DataRow, dtData.Columns[i].ColumnName, dtData.Columns[i].DataType.Name);
                // create the header
                tfField.HeaderTemplate = new GridViewCheckboxTemplate(DataControlRowType.Header, dtData.Columns[i].ColumnName, dtData.Columns[i].DataType.Name);
                // add to the GridView
                this.gvSessions.Columns.Add(tfField);
            }
        }

        this.gvSessions.DataSource = dtData;
        this.gvSessions.DataBind();

The GridView is defined in the asp page as:

<asp:GridView ID="gvSessions" AutoGenerateColumns="false" BorderWidth="0" BorderStyle="None" Width="100%" runat="server">
                    </asp:GridView>

This works fine and presents a GridView with a row per Person with 5 checkboxes in the desired state. The problem is that I need to know about any changes that are made when the user click a submit button. Is there anyway I can get the changes put back into the DataTable?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜