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

RadGrid Span around Checkbox disabled

5 Answers 265 Views
Grid
This is a migrated thread and some comments may be shown as answers.
MGrassman
Top achievements
Rank 2
MGrassman asked on 22 Feb 2010, 08:07 AM
I have a grid that uses paging, frozen headers and autogenerated columns.  My issue is when I click on the pager to get to the second row set all the checkboxes are disabled.  These work fine on the first page view but then after I click on the any page they are all disabled.  There is a <span disabled=disabled">checkbox</span> placed around them.  Can't seem to figure out what is causing this.

Thanks

<telerik:RadGrid ID="RadGrid1" AllowMultiRowEdit="true" EnableViewState="False" 
        AllowAutomaticUpdates="false" MasterTableView-EditMode="InPlace" runat="server"   
        AutoGenerateColumns="true" OnItemCreated="RadGrid1_ItemCreated" OnItemDataBound="RadGrid1_ItemDataBound" 
        onneeddatasource="RadGrid1_NeedDataSource" OnPreRender="RadGrid1_PreRender" AllowPaging="True" PageSize="50" PagerStyle-Mode="NextPrevNumericAndAdvanced">  
<MasterTableView EditMode="InPlace" ClientDataKeyNames="Product" TableLayout="Auto">  
</MasterTableView> 
<AlternatingItemStyle BackColor="AliceBlue" /> 
    <ClientSettings AllowKeyboardNavigation="false" EnableAlternatingItems="false" EnableRowHoverStyle="false">  
        <Scrolling AllowScroll="True" UseStaticHeaders="True" SaveScrollPosition="true" FrozenColumnsCount="2" /> 
 
        <Selecting AllowRowSelect="false" /> 
    </ClientSettings> 
</telerik:RadGrid> 
private DataTable _dt = null;  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            //Load the data  
            ddlContacts.DataBind();  
            if (ddlContacts.SelectedIndex == -1) { ddlContacts.SelectedIndex = 0; }  
            int contactUserID = 0;  
            Int32.TryParse(ddlContacts.SelectedValue, out contactUserID);  
            _dt = AFPI.Inventory.DAL.StoredProcedureCallerClasses.RetrievalProcedures.ProductUserEmailMatrix(contactUserID);  
            if (_dt.Rows.Count == 0)  
            {  
                RadGrid1.Visible = false;  
                lblNoRecords.Visible = true;  
                lblNoRecords.Text = string.Format("<font color=\"red\">Missing Information!</font><br />There are no users setup for {0}!<br /><br />Please <href=\"EditContactUser.aspx?c={1}\">add</a> at least one user to setup user based emails settings.<br /><br />If every inventory notification will go to the same person <href=\"EditContacts.aspx\">edit</a> the customer's To, CC and BCC settings.", ddlContacts.SelectedItem.Text, ddlContacts.SelectedValue);  
                return;  
            }  
            lblNoRecords.Visible = false;  
            RadGrid1.Visible = true;  
            DataRow row = _dt.Rows[0];  
            List<DataColumn> columnsToDelete = new List<DataColumn>();  
            for (int i = 0; i < _dt.Columns.Count; i++)  
            {  
                if (row[i].ToString().Length == 0)  
                {  
                    columnsToDelete.Add(_dt.Columns[i]);  
                }  
            }  
 
              
            foreach (DataColumn dc in columnsToDelete)  
            {  
                _dt.Columns.Remove(dc);  
            }  
 
            //Set the edit indexes of the grid so they show up in edit mode for the number of rows we are using.  
            for (int i = 0; i < _dt.Rows.Count; i++)  
            {  
                RadGrid1.EditIndexes.Add(i);  
            }  
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "RemoveContactUserRecIds", "RemoveContactUserRecIds();", true);  
        }  
 
        //if AddMapping = false you should remove mapping.  
        [WebMethod]  
        public static void ChangeMapping(int ProductID, int UserID, bool AddMapping) {  
            Guid? currentUserGuid = UserUtil.getCurrentUserGuid(Membership.GetUser());  
            DateTime moment = DateTime.Now;  
            ProductEventEmailTargetMapEntity mapping = null;  
 
            ProductEventEmailTargetMapCollection existingEvents = new ProductEventEmailTargetMapCollection();  
            PredicateExpression f = new PredicateExpression();  
            f.Add(ProductEventEmailTargetMapFields.ContactUserRecId == UserID);  
            f.AddWithAnd(ProductEventEmailTargetMapFields.ProductRecId == ProductID);  
              
            if (existingEvents.GetDbCount(f) == 0)  
            {  
                mapping = new ProductEventEmailTargetMapEntity();  
                mapping.ContactUserRecId = UserID;  
                mapping.ProductRecId = ProductID;  
                mapping.ProductEventType = "Any";  
                mapping.CreatedBy = currentUserGuid;  
                mapping.CreatedDate = moment;  
            }  
            else  
            {  
                existingEvents.GetMulti(f);  
                mapping = existingEvents[0];  
            }  
            if (AddMapping)  
            {  
                mapping.DeletedBy = null;  
                mapping.DeletedDate = null;  
            }  
            else  
            {  
                mapping.DeletedDate = moment;  
                mapping.DeletedBy = currentUserGuid;  
            }  
              
            mapping.ModifiedBy = currentUserGuid;  
            mapping.ModifiedDate = moment;  
            mapping.Save();  
              
        }  
 
 
        protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)  
        {  
            if (e.Item is GridDataItem)  
            {  
                //Change the first column to readonly and bold  
                (RadGrid1.MasterTableView.AutoGeneratedColumns[0] as GridBoundColumn).ReadOnly = true;  
                (RadGrid1.MasterTableView.AutoGeneratedColumns[0] as GridBoundColumn).ItemStyle.Font.Bold = true;  
                //(RadGrid1.MasterTableView.AutoGeneratedColumns[0] as GridBoundColumn).ItemStyle.BackColor = System.Drawing.Color.LightGray;  
                //Set width to increase performance large dataset for IE  
                (RadGrid1.MasterTableView.AutoGeneratedColumns[0] as GridBoundColumn).HeaderStyle.Width = Unit.Pixel(200);  
                (RadGrid1.MasterTableView.AutoGeneratedColumns[0] as GridBoundColumn).ItemStyle.Width = Unit.Pixel(200);  
                  
                for (int i = 1; i < RadGrid1.MasterTableView.AutoGeneratedColumns.Length; i++ )  
                {  
                    (RadGrid1.MasterTableView.AutoGeneratedColumns[i] as GridCheckBoxColumn).HeaderStyle.Width = Unit.Pixel(100);  
                    (RadGrid1.MasterTableView.AutoGeneratedColumns[i] as GridCheckBoxColumn).ItemStyle.Width = Unit.Pixel(100);  
                    (RadGrid1.MasterTableView.AutoGeneratedColumns[i] as GridCheckBoxColumn).ReadOnly = false;  
                }  
            }  
        }  
 
        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)  
        {  
            if (_dt == null) return;  
            //In order to get the checkboxes on all but the first row we have to recreate the datatable with columns 1 - n to boolean   
            //so the autogenerate columns knows to use a checkbox and not a textbox in edit mode.  
 
            //Copy the datatable structure  
            DataTable dtToBindTo = _dt.Clone();  
 
            // Loop through our new datatable setting the columns 1 - n to boolean  
            //We have to create a new datatable because once the datatable has data we can't change the datatype of the column  
            for (int i = 1; i < _dt.Columns.Count; i++)  
            {  
                dtToBindTo.Columns[i].DataType = System.Type.GetType("System.Boolean");  
            }  
 
            //Copy every row of our data to the new boolean table.  
            foreach (DataRow row in _dt.Rows)  
            {  
                dtToBindTo.ImportRow(row);  
            }  
            _dt.Dispose();  
            DataView dv = dtToBindTo.DefaultView;  
            dv.Sort = "Product";  
            //bind the data to the grid.  
            RadGrid1.DataSource = dv;  
              
        }  
        protected void RadGrid1_PreRender(object source, EventArgs e)  
        {  
            ConvertToActualHeaders();  
        }  
 
        private void ConvertToActualHeaders()  
        {  
            foreach (GridEditableItem edititem in RadGrid1.MasterTableView.GetItems(GridItemType.EditItem))  
            {  
                foreach (GridColumn col in RadGrid1.MasterTableView.RenderColumns)  
                {  
                    if (col.ColumnType == "GridBoundColumn")  
                    {  
                        if (edititem.IsInEditMode)  
                        {  
                            Literal txtbx = edititem[col.UniqueName].Controls[1] as Literal;  
                            string[] productValues = txtbx.Text.Split('_');  
                            txtbxtxtbx.Text = txtbx.Text.Replace("_" + productValues[productValues.Length - 1], "");  
                        }  
                    }  
                }  
            }  
 
              
        }  
 
        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)  
        {  
 
            if (e.Item is GridDataItem)  
            {  
                GridDataItem dataItem = (GridDataItem)e.Item;  
                int numCols = RadGrid1.MasterTableView.AutoGeneratedColumns.Length;  
                  
                string productColumn = DataBinder.Eval(dataItem.DataItem, "Product").ToString();  
                int productID = 0;  
                string[] productColumnproductColumnArray = productColumn.Split('_');  
                  
                if (!Int32.TryParse(productColumnArray[productColumnArray.Length - 1], out productID))  
                {  
                    return;  
                }  
                  
 
                for (int i = 1; i < numCols; i++)  
                {  
                    CheckBox chk = dataItem[(RadGrid1.MasterTableView.AutoGeneratedColumns[i] as GridCheckBoxColumn).UniqueName].Controls[0] as CheckBox;  
                    string columnHeader = (RadGrid1.MasterTableView.AutoGeneratedColumns[i] as GridCheckBoxColumn).HeaderText;  
                    string[] userDataArray = columnHeader.Split('_');  
                    int userId = 0;  
                    if (Int32.TryParse(userDataArray[userDataArray.Length - 1], out userId))  
                    {  
                        chk.Attributes.Add("onclick", String.Format("checkboxClick({0}, {1}, this);", productID, userId));  
                    }  
                }  
            }  
        } 

5 Answers, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 24 Feb 2010, 04:39 PM
Hi MGrassman,

I can see that a GridCheckBoxColumn is created automatically in RadGrid's Columns. The GriCheckBoxColumn renders ReadOnly checkboxes in view mode, but you are further using ItemCreated to enable the checkboxes. The problem is, ItemDataBound (fired after ItemCreated), re-creates the checkboxes and resets their readonly state. So, you may consider using ItemDataBound for making your checkboxes editable instead.

Sincerely yours,
Veli
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
MGrassman
Top achievements
Rank 2
answered on 24 Feb 2010, 05:52 PM
Thanks for the help.  I tried adding the readonly = false in the item databound event but nothing changes.

for (int i = 1; i < numCols; i++) 
      CheckBox chk = dataItem[(RadGrid1.MasterTableView.AutoGeneratedColumns[i] as GridCheckBoxColumn).UniqueName].Controls[0] as CheckBox; 
                    string columnHeader = (RadGrid1.MasterTableView.AutoGeneratedColumns[i] as GridCheckBoxColumn).HeaderText; 
                    string[] userDataArray = columnHeader.Split('_'); 
                    int userId = 0
                    if (Int32.TryParse(userDataArray[userDataArray.Length - 1], out userId)) 
                    { 
                        chk.Attributes.Add("onclick", String.Format("checkboxClick({0}, {1}, this);", productID, userId)); 
                    } 
                    (RadGrid1.MasterTableView.AutoGeneratedColumns[i] as GridCheckBoxColumn).ReadOnly = false
                     
                } 

(RadGrid1.MasterTableView.AutoGeneratedColumns[i] as GridCheckBoxColumn).ReadOnly = false;  
is the line I added to this event.

Am I assigning this incorrect?
0
Accepted
Veli
Telerik team
answered on 25 Feb 2010, 08:51 AM
Hello MGrassman,

You need to re-enable the checkbox control, not the column itself:

CheckBox chk = dataItem[(RadGrid1.MasterTableView.AutoGeneratedColumns[i] as GridCheckBoxColumn).UniqueName].Controls[0] as CheckBox;
chk.ReadOnly = false;

Note, however, that ItemDataBound does not fire on every postback, but only when you rebind your grid. So, unless you rebind your grid in every postback, your checkboxes will be recreated as ReadOnly=true when the form is posted.

You may also consider using a GridTemplateColumn with a CheckBox control in the ItemTemplate, so that you have an editable checkbox in view mode all along.

Greetings,
Veli
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Princy
Top achievements
Rank 2
answered on 25 Feb 2010, 09:12 AM
Hi,

Try the code snippet below to access the checkbox in the GridCheckboxColumn and enable it.You can access the AutoGenerated checkbox using the DataField as the UniqueName.

 protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem dataItem = (GridDataItem)e.Item; 
            ((CheckBox)dataItem["Discontinued"].Controls[0]).Enabled = true
        } 
    } 


Thanks,
Princy
0
MGrassman
Top achievements
Rank 2
answered on 25 Feb 2010, 09:16 AM
Thanks for your help.

That worked but instead of 

chk.ReadOnly = false;

I had to use chk.Enabled = true;

Thanks again.
Tags
Grid
Asked by
MGrassman
Top achievements
Rank 2
Answers by
Veli
Telerik team
MGrassman
Top achievements
Rank 2
Princy
Top achievements
Rank 2
Share this question
or