views:

23

answers:

0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;

if the execution of the code is allowed at post back, then for each post back extra column are created.

yes i know the row binded event is called only when the grid rows bind, which happen only if the completing code runs, but i want prevent the running of code on post back and to keep the controls created on run time.

public partial class Default3 : System.Web.UI.Page
{
    const int ColumnSelect_Boundfield = 1;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {//if starts
            //HINT
            //THIS CODE can be replaced by sql binding to datatable
            //Get real data here.
            DataTable datatble = new DataTable();
            datatble.Clear();
            datatble.Columns.Add("Record");
            datatble.Rows.Add(datatble.NewRow());
            datatble.Rows.Add(datatble.NewRow());
            ///////////dt.Rows[0][0] = "5";
            datatble.Rows[0]["Record"] = "the name 0";
            datatble.Rows[1]["Record"] = "the name 1";


            //HINT
            //this must keep, its never shown by any helper. reason not to show is unknown
            GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_RowDataBound);

            TemplateField Templatefld = new TemplateField();
            Templatefld.ShowHeader = true;
            Templatefld.HeaderText = "TEMPLATE FIELD";
            //GridView1.Columns.Add(new TemplateField());
            GridView1.Columns.Add(Templatefld);

            BoundField b = new BoundField();
            b.ShowHeader = true;
            b.HeaderText = "CHECKBOXES[boundfield]";
            GridView1.Columns.Add(b);


            //HINT
            //Boundfield does not need the column name        
            //b.DataField = "Record";

            //HINT
            //gridview will be DATABIND at the end ALWAYS
            GridView1.DataSource = datatble;
            GridView1.DataBind();

            Button1.Click += new EventHandler(Checkbx_Click);



        }//if ends

        else
        {
            Response.Write("<br /> This is postback ");



        }

    }


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.Header)
        {
            //HINTS
            //only a conrol can come in the ()
            e.Row.Cells[ColumnSelect_Boundfield].Controls.Add(new CheckBox());
        }
    }


    protected void Checkbx_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            //Could also use (CheckBox)row.Cells[ColumnSelect].FindControl if you give the checkboxes IDs when generating them.
            CheckBox cb = (CheckBox)row.Cells[ColumnSelect_Boundfield].Controls[0];

            if (cb.Checked)
            {
                Response.Write("<br /> The index of row checked" + row.RowIndex.ToString());
            }


            else
            {
                Response.Write("<br /> The index of row not checked :" + row.RowIndex.ToString());

            }
        }
    }


}