Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have written a class "CheckBoxTemplate" that implements ITemplate interface.

At runtime, using this class I add a column to the gridview.

Now I have "next" link and i have implemented the click eventhandler. In the eventhandler i iterate each row but I don't know how to get the "Checked" property of the CheckBoxTemplate.

Please help me in this regard. I have already wasted two days on this thing, and it is now frustrating me alot :(

Yeah I am using ASP.NET. Sorry for confusion.
would post the code that will be more helpful

Here is my CheckBoxTemplate class:

Yeah I am using ASP.NET. Sorry for confusion.
would post the code that will be more helpful

Here is my CheckBoxTemplate class:

C#
public class CheckBoxTemplate: ITemplate
{
    private string id;
    CheckBox box;
    public bool Checked
    {
        get { return box.Checked; }
        set { box.Checked = value; }
    }
    public string ID
    {
        get { return id; }
        set { id = value; }
    }
    /// <summary>
    /// The CheckBoxItem constructor
    /// </summary>
    /// <param name="editable">true if the item is to be in its editable state, false for the item to be disabled.</param>
    public CheckBoxTemplate(bool editable)
    {
        readOnly = (editable == true) ? false : true;
    }
    /// <summary>
    /// Instantiates the CheckBox that we wish to represent in this column.
    /// </summary>
    /// <param name="container">The container into which the control or controls are added.</param>
    void ITemplate.InstantiateIn(Control container)
    {
        box = new CheckBox();
        box.ID = id;
        box.DataBinding += new EventHandler(this.BindData);
        box.AutoPostBack = autoPostBack;
        box.CheckedChanged += new EventHandler(this.OnCheckChanged);
        container.Controls.Add(box);
    }
    /// <summary>
    /// Our CheckChanged event
    /// </summary>
    public event EventHandler CheckedChanged;
    /// <summary>
    /// This is a common handler for all the Checkboxes.
    /// </summary>
    /// <param name="sender">The raiser of this event a CheckBox.</param>
    /// <param name="e">A System.EventArgs that contains the event data.</param>
    private void OnCheckChanged(object sender, EventArgs e)
    {
        if (CheckedChanged != null)
        {
            CheckedChanged(sender, e);
        }
    }
    /// <summary>
    /// The internal storage for which DataField we are going to represent.
    /// </summary>
    private string dataField;
    /// <summary>
    /// Used to set the DataField that we wish to represent with this CheckBox.
    /// </summary>
    public string DataField
    {
        get
        {
            return dataField;
        }
        set
        {
            dataField = value;
        }
    }
    /// <summary>
    /// The internal storage for the AutoPostback flag.
    /// </summary>
    private bool autoPostBack = false;
    /// <summary>
    /// Set the AutoPostBack flag. If this is true then each time a CheckBox is clicked
    /// in the Column that contains this item then an event is raised on the server.
    /// </summary>
    public bool AutoPostBack
    {
        set
        {
            autoPostBack = value;
        }
        get
        {
            return autoPostBack;
        }
    }
    /// <summary>
    /// Handler for the DataBinding event where we bind the data for a specific row
    /// to the CheckBox.
    /// </summary>
    /// <param name="sender">The raiser of the event.</param>
    /// <param name="e">A System.EventArgs that contains the event data.</param>
    private void BindData(object sender, EventArgs e)
    {
        CheckBox box = (CheckBox)sender;
        GridViewRow container = (GridViewRow)box.NamingContainer;
        box.Checked = false;
        box.Enabled = (readOnly == true) ? false : true;
        string data = ((DataRowView)container.DataItem)[dataField].ToString();
        Type t = ((DataRowView)container.DataItem).DataView.Table.Columns[dataField].DataType;
        if (data.Length > 0)
        {
            switch (t.ToString())
            {
                case "System.Boolean":
                    if ((data == "True") || (data == "true"))
                    {
                        box.Checked = true;
                    }
                    break;
                default:
                    break;
            }
        }
    }
    /// <summary>
    /// Internal storage for the readOnly flag.
    /// </summary>
    private bool readOnly = true;
}


And this is how I am adding it to the gridview <TemplateField>:

C#
private void ResetGridView()
    {
        BoundField rn = new BoundField();
        rn.DataField = "ResultNumber";
        rn.HeaderText = "Result Number";

        BoundField ac = new BoundField();
        ac.DataField = "AccountNumber";
        ac.HeaderText = "Account Number";

        BoundField cc = new BoundField();
        cc.DataField = "CardNumber";
        cc.HeaderText = "Credit Card";

        CommandField cf = new CommandField();

        cf.ItemStyle.VerticalAlign = VerticalAlign.Middle;
        cf.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
        cf.ShowHeader = true;
        cf.HeaderText = "Actions";
        cf.SelectText = "Print";
        cf.DeleteText = "Email";

        cf.ShowDeleteButton = false;
        cf.ShowSelectButton = false;
        cf.ShowCancelButton = false;
        cf.ShowEditButton = false;

        BoundField ri = new BoundField();  //result index
        ri.DataField = "ResultIndex";
        ri.HeaderText = "Result Index";

        gvBoResults.Columns.Clear();

        TemplateField cbf = new TemplateField();
        CheckBoxTemplate t = new CheckBoxTemplate(true);
        t.ID = "myCheckBox";
        t.DataField = "select";
        cbf.ItemTemplate = t;
        gvBoResults.Columns.Add(cbf);

        gvBoResults.Columns.Add(rn);

        if (SearchType == SearchType.CreditCard)
            gvBoResults.Columns.Add(cc);
        else
            gvBoResults.Columns.Add(ac);

        gvBoResults.Columns.Add(cf);
        gvBoResults.Columns.Add(ri);
        
    }


And the problematic part:

private void GetCheckedRows()
    {
        foreach (GridViewRow r in gvBoResults.Rows)
        {
             //I want to get the value of check box :(
        }

    }
Posted
Updated 2-Sep-10 18:31pm
v3
Comments
Christian Graus 3-Sep-10 0:31am    
Please don't add 'answers' that are questions, edit your post, as I did.

1 solution

You're using ASP.NET, not ASP. You should post your code so we can see what you've done. you could also read the articles that do this, on Code Project, and see if they answer your question.
 
Share this answer
 
Comments
Phan7om 3-Sep-10 4:59am    
I have posted the code, can't you help me ??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900