This is a migrated thread and some comments may be shown as answers.

Accessing template column of dynamically built Radgrid from code behind

2 Answers 200 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Manikandan Balasubramaniyan
Top achievements
Rank 1
Manikandan Balasubramaniyan asked on 03 May 2012, 05:56 PM
I am trying to retrieve the values of Checkbox which is inside a template column, build from the codebehind dynamically.
 
protected void Page_Init(object sender, EventArgs e)
{       
    DefineGridStructure();
private void DefineGridStructure()
{       
    GridBoundColumn boundColumn;
    boundColumn = new GridBoundColumn();
    RadGridAttendance.MasterTableView.Columns.Add(boundColumn);
    boundColumn.DataField = "Name";
    boundColumn.HeaderText = "Name";
        
        string session_id = dt.Rows[i]["web_submission_id"].ToString();          
        GridTemplateColumn templateColumn = new GridTemplateColumn();
        templateColumn.ItemTemplate = new MyTemplate(session_id,db);            
        templateColumn.UniqueName = session_id;
        RadGridAttendance.MasterTableView.Columns.Add(templateColumn);
     
}

Code for Template:

class MyTemplate : ITemplate
{
    private string colname;
    protected CheckBox cbAttended;
    private string query;
    private string session_id;   
    private DataLib_oracle db;
     
    public MyTemplate(string sessionid, DataLib_oracle dbo)
    {
        colname = sessionid;
        session_id = sessionid;
        db = dbo;
    }
 
 
    public void InstantiateIn(System.Web.UI.Control container)
    {       
        cbAttended = new CheckBox();
        cbAttended.Attributes["session_id"] = session_id;
        container.Controls.Add(cbAttended);
        cbAttended.DataBinding += new EventHandler(cbAttended_DataBinding);
        cbAttended.ID = session_id;       
        //cbAttended.Checked = true;
        //cbAttended.Enabled = false;       
    }
 
 
    void cbAttended_DataBinding(object sender, EventArgs e)
    {
        string attended;
        CheckBox cBox = (CheckBox)sender;       
        GridDataItem container = (GridDataItem)cBox.NamingContainer;
        string personnel_id = ((DataRowView)container.DataItem)["personnel_id"].ToString();
        cbAttended.Attributes["personnel_id"] = personnel_id;
        string session_id = cBox.Attributes["session_id"].ToString();
         
        //Eval("Attended").ToString() == "1"
        //cBox.Checked = (bool)((DataRowView)container.DataItem)["Bool"];
 
 
        query = " SELECT ATTENDED FROM   WEB_ATTENDEES WHERE PERSONNEL_ID = " + personnel_id + " AND SESSION_ID = " + session_id;
        attended = db.GetStrDescFromID(query, WebKit.ConnectionString);
 
 
        if (attended=="1")
            cBox.Checked = true;
    }
      
}

On clicking Save button of this page, am trying to retrieve the values of checkboxes.

foreach (GridDataItem item in RadGridAttendance.MasterTableView.Items)
 {
            string test = RadGridAttendance.Columns[1].UniqueName;
            CheckBox cb = (CheckBox)(item[test].FindControl(test));
}
In the above code, there are no controls available in  item[test] .
cb is returning null.

Note the unique name of the column and the ID of the checkbox are same.

Please let me know how do i retrieve the checkbox.

Thanks in advance,
Mani


2 Answers, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 03 May 2012, 07:45 PM
0
Manikandan Balasubramaniyan
Top achievements
Rank 1
answered on 03 May 2012, 08:38 PM
Thank you Jayesh for an awesome code sample.

So far i was creating the grid from Page_load. Now i moved it to Page_Init. 
Also  i now added NeedDataSource.

After doing these, it worked like charm.

Thanks again!
Tags
Grid
Asked by
Manikandan Balasubramaniyan
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Manikandan Balasubramaniyan
Top achievements
Rank 1
Share this question
or