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

RadGrid_Prerender seems to overwrite the RadGrid_ItemCommand Event - Help

1 Answer 51 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Gayathri
Top achievements
Rank 1
Gayathri asked on 11 Dec 2014, 11:06 AM
Hi,

I have a radgrid in which i have placed the following controls
1)A RadTabstrip -with 2 pageviews (one for displaying a page and the another for displaying someother page)
2)someother page may not be visible based on the functionality most of the times. so i am checking for its visibility in itemcreated.
if visible false do not display the page 2
on clicking the expand collapse button it should expand /collapse the radgrid displaying the tabstrip.
3)added to this i have a validation in prerender event of the radgrid which has some validations as follows. means it reorders some columns and based on a condition makes few columns visible or not visible.

Here is the radgrid prerender event.
    protected void RadGrid1_PreRender(object sender, EventArgs e)
        {
            DataTable dtVerbatimColums = VerbatimGridSource;
            int neworderindex = 4;
            for (int i = 0; i < dtVerbatimColums.Rows.Count; i++)
            {
                if (dtVerbatimColums.Rows[i]["Label"].ToString() == "Default")
                {
                    try
                    {
                        RadGrid1.Columns.FindByUniqueName(dtVerbatimColums.Rows[i]["ID"].ToString()).OrderIndex = neworderindex++;
                    }
                    catch (GridException)
                    {
                        continue;
                    }
                }
                else
                {
                    try
                    {
                        RadGrid1.Columns.FindByUniqueName(dtVerbatimColums.Rows[i]["Label"].ToString()).OrderIndex = neworderindex++;
                    }
                    catch (GridException)
                    {
                        continue;
                    }

                }
            }
          
            if (RadGrid1.MasterTableView.GetColumn("ActionNotes") != null)
            {
                RadGrid1.MasterTableView.GetColumn("ActionNotes").HeaderText = VS.VerbatimOptions.TextActionsWindow;
                RadGrid1.MasterTableView.GetColumn("ActionNotes").Visible = VS.VerbatimOptions.ShowActionsWindow;
            }
            if (RadGrid1.MasterTableView.GetColumn("AppealStatus") != null)
            {
                RadGrid1.MasterTableView.GetColumn("AppealStatus").Visible = VS.VerbatimOptions.ShowAppeal;
            }
            if (RadGrid1.MasterTableView.GetColumn("Evaluate") != null)
            {
                RadGrid1.MasterTableView.GetColumn("Evaluate").Visible = VS.VerbatimOptions.ShowAppeal;
            }
            RadGrid1.Rebind();
            foreach (GridDataItem item in RadGrid1.Items)
            {

                //Column Download
                CheckBox cbAvailable = item["Download"].FindControl("chAvailable") as CheckBox;
                CheckBox cbHeadAvailable = item["Download"].FindControl("chAvailable") as CheckBox;
                if (cbAvailable != null)
                {

                    cbAvailable.Checked = IsItemChecked(Convert.ToInt32(item["id"].Text));
                    RadAjaxManager1.AjaxSettings.AddAjaxSetting(cbAvailable, cbAvailable);
                    RadAjaxManager1.AjaxSettings.AddAjaxSetting(cbAvailable, RadGrid1);

                    RadAjaxManager1.AjaxSettings.AddAjaxSetting(cbHeadAvailable, cbHeadAvailable);
                    RadAjaxManager1.AjaxSettings.AddAjaxSetting(cbHeadAvailable, RadGrid1);
                }
                string decodedText = "";
                string encodedText = "";
                // Check for empty transcription - if is it empty, remove all controls from cell and replace it with empty text, to avoid missing left and bottom cell border
                if (RadGrid1.Columns.FindByUniqueName("Transcription").Visible)
                {
                    GridTableCell cell = item["Transcription"] as GridTableCell;
                    Label textControl = cell.Controls[1] as Label;
                    if (string.IsNullOrEmpty(textControl.Text))
                    {
                        item["Transcription"].Controls.Clear();
                        item["Transcription"].Text = "&nbsp;";
                    }
                    else
                    {
                        //added 28-Oct-2014
                        decodedText = Server.HtmlDecode(textControl.Text);                  //decode everything back first
                        encodedText = System.Security.SecurityElement.Escape(decodedText);  //encode back only < > " ' & before outputting
                        //encodedText2 = Server.HtmlEncode(decodedText);

                        //item["Transcription"].Text = DescriptionTrunc(Server.HtmlEncode(decodedText));  //html encode output
                        //item["Transcription"].ToolTip = Server.HtmlEncode(decodedText);                 //html encode output

                        //added 28-Oct-2014
                        item["Transcription"].Text = DescriptionTrunc(encodedText);
                        item["Transcription"].ToolTip = encodedText;
                    }

                }

                if (!item["Type"].Text.Contains("V"))
                {
                    item["Play"].Controls.Clear();
                    item["Play"].Text = "&nbsp;";
                }
                else
                {
                    string fileName = item["FileName"].Text;
                    ImageButton ib = item["Play"].Controls[1] as ImageButton;
                    if (ib == null) continue;

                    //Verbatim file check
                    if (fileName != string.Empty)
                    {
                        string path = VerbatimManager.GetVerbatimFilePath(fileName);
                        if (!File.Exists(path))
                        {
                            item["Play"].Controls.Clear();
                            item["Play"].Text = "&nbsp;";
                            item["FileName"].Enabled = false;
                            continue;
                        }
                    }

                    //Permission check
                    if (!user.IsAuthorized(PermissionNames.Security_Verbatim_PlayVerbatim))
                    {
                        ib.Enabled = false;
                        continue;
                    }

                    //Play button tooltip
                    if (fileName != "")
                    {
                        ib.ToolTip = user.Translate("VerbatimResponses.Play") + " " + VerbatimManager.GetVerbatimFilePath(fileName);
                        ib.CommandArgument = fileName;
                        RadAjaxManager1.AjaxSettings.AddAjaxSetting(ib, ib);
                    }

                    if (!isPPTExport)
                    {
                        ib.Attributes.Add("OnMouseOver", "src='Images/play_over.gif';");
                        ib.Attributes.Add("OnMouseOut", "src='Images/play.gif';");
                    }
                    else
                    {
                        ib.Attributes.Add("src", "play.gif");
                    }
                    ib.AlternateText = user.Translate("VerbatimResponses.Play");
                }


                // 20141030
                if (item["AppealStatus"].Text.Contains("Pending"))
                {
                    item["AppealStatus"].ForeColor = System.Drawing.Color.Navy;
                }
                else if (item["AppealStatus"].Text.Contains("Approved"))
                {
                    item["AppealStatus"].ForeColor = System.Drawing.Color.Green;
                }
                else if (item["AppealStatus"].Text.Contains("Declined"))
                {
                    item["AppealStatus"].ForeColor = System.Drawing.Color.Red;
                }
            }
        }

Item Command Event of the radgrid.
 //To make the tab page visible   
            RadPageView ExplorePageView;
            RadPageView EvaluatePgView;
            RadTabStrip rdtabexploreeval;


            if (e.CommandName == "RowClick" || e.CommandName == "ExpandCollapse")
            {



            //if (e.CommandName == RadGrid.ExpandCollapseCommandName && e.Item is GridDataItem)
            //{
                GridDataItem gridFormItem = e.Item as GridDataItem;
                int VerbatimId = int.Parse(gridFormItem["id"].Text);

                string sessionId = gridFormItem["SessionId"].Text;

                string exploreurl = "VerbatimExplore.aspx?SessionId=" + sessionId + "&VerbatimId=" + VerbatimId + "&ActionName=Explore";
                string evaluateurl = "VerbatimExplore.aspx?SessionId=" + sessionId + "&VerbatimId=" + VerbatimId + "&ActionName=Evaluate";
                foreach (GridNestedViewItem item in radgrid1.MasterTableView.GetItems(GridItemType.NestedView))
                {
                    ExplorePageView = (RadPageView)item.FindControl("radExplorePgView");
                    ExplorePageView.ContentUrl = exploreurl;
                    rdtabexploreeval = (RadTabStrip)item.FindControl("rdtabexploreeval");
                    rdtabexploreeval.Tabs[0].Text = user.Translate("VerbatimResponses.Explore");
                    rdtabexploreeval.Tabs[1].Text = user.Translate("verbatimResponses.evaluate");
                    EvaluatePgView = (RadPageView)item.FindControl("radEvalPgView");
                    EvaluatePgView.ContentUrl = evaluateurl;

                }
                bool lastState = e.Item.Expanded;

                if (e.CommandName == "ExpandCollapse")
                {
                    lastState = !lastState;
                }
                CollapseAllRows();
                e.Item.Expanded = !lastState;
        }
           

                if (e.CommandName.ToUpper() != "PLAY") return;
                string fileName;
                fileName = GetFileToDownload(e.CommandArgument.ToString());

            // Added code by Raguraman on 12-DEC-2011 for VSMD-4612            

            if (!VS.VerbatimOptions.PlayVerbatimInNewBrowserPage)
            {
                SetDownloadButtons();
                if (fileName != "")
                {
                    verbatimManager.SetLastDownload(int.Parse(radgrid1.MasterTableView.DataKeyValues[e.Item.ItemIndex]["id"].ToString()));
                    Misc.DownloadWavToResponse(fileName);
                }
            }

            // END  RadTabStrip rdtabexploreeval;
            RadPageView EvaluatePgView;
            //RadPageView ExplorePgView;
            if (e.Item.Expanded == true)
            {
                e.Item.PreRender += new EventHandler(radgrid1_PreRender);
            }
            if (RadGrid1.MasterTableView.GetColumn("Evaluate") != null)
            {
                //switch off the evaluate tab strip visible false if showappeal is set to false.
                if (VS.VerbatimOptions.ShowAppeal ==false)
                
                {
                    foreach (GridNestedViewItem item in Radgrid1.MasterTableView.GetItems(GridItemType.NestedView))
                    {                
                        rdtabexploreeval = (RadTabStrip)item.FindControl("rdtabexploreeval");
                        EvaluatePgView = (RadPageView)item.FindControl("radEvalPgView");
                        rdtabexploreeval.Tabs[1].Visible = false;
                    }
                }
            }
Item created event of the radgrid.

RadTabStrip rdtabexploreeval;
            RadPageView EvaluatePgView;
            //RadPageView ExplorePgView;
            if (e.Item.Expanded == true)
            {
                e.Item.PreRender += new EventHandler(RadGrid1_PreRender);
            }
            if (RadGrid1.MasterTableView.GetColumn("Evaluate") != null)
            {
                //switch off the evaluate tab strip visible false if showappeal is set to false.
                if (VS.VerbatimOptions.ShowAppeal ==false)
                
                {
                    foreach (GridNestedViewItem item in RadGrid1.MasterTableView.GetItems(GridItemType.NestedView))
                    {                
                        rdtabexploreeval = (RadTabStrip)item.FindControl("rdtabexploreeval");
                        EvaluatePgView = (RadPageView)item.FindControl("radEvalPgView");
                        rdtabexploreeval.Tabs[1].Visible = false;
                    }
                }
            }

The aspx page has this line of code onprerender="radgrid1_prerender"". my problem is i am not expand or collapse. more over i get this error "controls cannot be modified outside the collection"". please help.
-Thanks
Gayathri

1 Answer, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 16 Dec 2014, 08:44 AM
Hi Gayathri,

I have examined the provided code and noticed couple of things that you should change. In the ItemCreated handler you are binding the PreRender event of a GridItem to the PreRender handler of RadGrid. Remove this line and use only the RadGrid PreRender event to control the visibility of the columns.

Moreover, have in mind that when a column has its Visible property set to false it is not rendered on the page. Try to use the Display property to control the visibility for the columns. You can also try to remove the Rebind() statement for RadGrid and see how the functionality works then.

I also noticed that you are adding AjaxSettings dynamically in the PreRender handler for RadGrid. Note that if you would like to add the settings dynamically you should use Page events that are executed every time the page loads (Page_Load, Page_PreRender). Check out the following article that illustrates the approach.

Regarding the other error you are seeing. Wrap the client-side scripts in a RadCodeBlock control and see if the error disappears. The approach is illustrated in this article.



Regards,
Viktor Tachev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Gayathri
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Share this question
or