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

Refresh Dynamic Created Grid On Window Close

1 Answer 49 Views
Grid
This is a migrated thread and some comments may be shown as answers.
awhughes
Top achievements
Rank 1
awhughes asked on 29 Jul 2013, 10:53 PM

I am dynamically creating a Grid in Page_Init. I have set Edit Mode to equal Batch.  When the user hits the save changes button, I display a window which allows the user to capture some additional data and which point the user can save the changes or cancel. This part of my program works fine.  My problem occurs when I want to refresh the Grid after the user has applied the changes and closed the window.  I have tried adding the following javascript and code to my program which I execute it when the window closes. Works fine but the Grid does not refresh.

 

              function refreshGrid(arg) {

                  if (!arg) {

                      $find("<%= RadAjaxManager1.ClientID %>").ajaxRequest("Rebind");

                  }

                  else {

                      $find("<%= RadAjaxManager1.ClientID %>").ajaxRequest("RebindAndNavigate");

                  }

              }

    protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)

    {

        if (e.Argument == "Rebind")

        {

            RadGrid1.Rebind();

        }

        else if (e.Argument == "RebindAndNavigate")

        {

            DirectoryRefresh();

        }

    }

    void grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)

    {

        RadGrid grid = (RadGrid)sender;

        if (SessionManager.GetCache().MarketChannels != null)

        {

            grid.DataSource = SessionManager.GetCache().MarketChannels;

        }

    }

    protected void DirectoryRefresh()

    {

        SessionManager.SetChangedValues(null);

        MarketDirectory directory = SessionManager.GetCurrentMarketDirectory();

        directory = null;

        LoadMarketChannelTable();

        //RadGrid1.DataSource = SessionManager.GetCache().MarketChannels;

        RadGrid1.Rebind();

    }

 

 

  I assumed that this was because my grid was not a control in the AjaxManager to update. Because I create my Grid in the program, I added my Grid to the Ajaxmanager in the PageLoad.  When I do this, the script I use to pop the window when the user hits the Save Changes Button does not execute. See highlighted code.

 

   protected void Page_Init(object sender, System.EventArgs e)

    {

        PopulateGridOnPageInit();

    }

    protected void Page_Load(object sender, EventArgs e)

    {

        RadGrid1 = (RadGrid)PlaceHolder1.FindControl("grdMarkets");

        RadAjaxManager1.AjaxSettings.AddAjaxSetting(RadAjaxManager1, RadGrid1);

        RadAjaxManager1.AjaxSettings.AddAjaxSetting(RadGrid1, RadGrid1);

    }

    protected void PopulateGridOnPageInit()

    {

        RadGrid grid = new RadGrid();

        //grid.ClientIDMode = System.Web.UI.ClientIDMode.Static;

        RadGrid1 = grid;

        string selectedValue = Master.Request.Form.Get(RadComboBox1.ClientID.Replace('_', '$'));

        grid.ID = "grdMarkets";

        grid.Width = Unit.Percentage(100);

        grid.AllowSorting = true;

        grid.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;

        grid.AllowPaging = true;

        grid.Skin = "Web20";

        grid.MasterTableView.AutoGenerateColumns = false;

        grid.MasterTableView.EnableColumnsViewState = false;

        grid.MasterTableView.EditMode = GridEditMode.Batch;

        grid.MasterTableView.CommandItemTemplate = new MyCommandItemTemplate("gridCommands");

        grid.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;

        grid.MasterTableView.CommandItemSettings.ShowCancelChangesButton = true;

      

        grid.MasterTableView.DataKeyNames = new string[] { "Id" };

        grid.MasterTableView.CommandItemSettings.ShowSaveChangesButton = true;

        grid.MasterTableView.BatchEditingSettings.EditType = GridBatchEditingType.Cell;

        grid.NeedDataSource += new GridNeedDataSourceEventHandler(grid_NeedDataSource);

        grid.BatchEditCommand += new GridBatchEditEventHandler(grid_BatchEditCommand);

        grid.AllowFilteringByColumn = true;

        if (selectedValue != null)

        {

            if ((SessionManager.GetCurrentMarket() == null) ||

                (selectedValue != SessionManager.GetCurrentMarket().Name))

            {

                SessionManager.SetCurrentMarket(selectedValue);

                hdMarket.Value = selectedValue;

                LoadMarketChannelTable();

            }

            DataTable dt = SessionManager.GetCache().MarketChannels;

            foreach (DataColumn dc in dt.Columns)

            {

                if (dc.ColumnName == "group")

                {

                    GridTemplateColumn boundColumn = new GridTemplateColumn();

                    boundColumn.ItemTemplate = new LabelTemplate(dc.ColumnName);

                    boundColumn.EditItemTemplate = new ComboBoxTemplate(dc.ColumnName);

                    boundColumn.DataType = dc.DataType;

                    boundColumn.HeaderText = dc.ColumnName;

                    boundColumn.SortExpression = dc.ColumnName;

                    boundColumn.DataField = dc.ColumnName;

                    grid.MasterTableView.Columns.Add(boundColumn);

                }

                else

                {

                    if (dc.DataType == typeof(bool))

                    {

                        GridCheckBoxColumn boundColumn = new GridCheckBoxColumn();

                        boundColumn.DataType = dc.DataType;

                        boundColumn.HeaderText = dc.ColumnName;

                        boundColumn.DataField = dc.ColumnName;

                        grid.MasterTableView.Columns.Add(boundColumn);

                        if ((dc.ColumnName == "id") || (dc.ColumnName == "channelID"))

                        {

                            boundColumn.Visible = false;

                        }

                    }

                    else

                    {

                        GridBoundColumn boundColumn = new GridBoundColumn();

                        boundColumn.DataType = dc.DataType;

                        boundColumn.HeaderText = dc.ColumnName;

                        boundColumn.DataField = dc.ColumnName;

                        grid.MasterTableView.Columns.Add(boundColumn);

                        if ((dc.ColumnName == "id") || (dc.ColumnName == "channelID"))

                        {

                            boundColumn.Visible = false;

                        }

                    }

                }

            }

           

 

        }

        PlaceHolder1.Controls.Add(grid);

        string script = "<script language='javascript' type='text/javascript'>function SaveChangesInGrid(sender, args) { " +

                "var grid = $find('" + grid.ClientID + "'); " +

                    "grid.get_batchEditingManager().saveChanges(grid.get_masterTableView());" +

                    "}</script>";

        ClientScript.RegisterStartupScript(this.GetType(), "SaveChanges", script);

    }

    void grid_BatchEditCommand(object sender, GridBatchEditingEventArgs e)

    {

       List<string> changedValues = new List<string>();

       SessionManager.SetChangedValues(null);

       DataTable dt = SessionManager.GetCache().MarketChannels;

       bool updateNeeded = false;

        foreach(GridBatchEditingCommand cmd in e.Commands)

        {

             if(cmd.Type == GridBatchEditingCommandType.Update)

             {

                // int editRowIdx = Int32.Parse(e.Item.ItemIndexHierarchical);

                 foreach(string columnName in cmd.NewValues.Keys)

                 {

                     int key = Convert.ToInt32(cmd.Item.GetDataKeyValue("Id"));

                     DataRow row = dt.Rows.Find(key);

                    

                     if((cmd.NewValues[columnName] != null) && (cmd.NewValues[columnName].ToString().Trim() != row[columnName].ToString().Trim()))

                     {

                         changedValues.Add(row["channelID"].ToString() + ";"  + columnName + ";" + cmd.NewValues[columnName].ToString());

                         row[columnName] = cmd.NewValues[columnName];

                         updateNeeded = true;

                        

                     }

                 }

             }

        }

        if (updateNeeded)

        {

            SessionManager.SetChangedValues(changedValues);

           string script = "<script language='javascript' type='text/javascript'>Sys.Application.add_load(showBatchChangeWindow);</script>";  // Doesn’t execute

            ClientScript.RegisterStartupScript(this.GetType(), "showWindow", script);

   

        }

    }

 

 

Any help would be appreciated.

 

Thanks!

 

1 Answer, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 30 Jul 2013, 06:57 AM
Hello,

Can you please try with the ScriptManager ?

string script = "myTestFunction('" + showBatchChangeWindow + "')"// Doesn’t execute
 
       ScriptManager.RegisterStartupScript(this, this.GetType(), "showWindow123", script, true);
function myTestFunction(strText) {
                    alert('JS function called successfully');
               Sys.Application.add_load(strText);
           }

Let me know if any concern.

Thanks,
Jayesh Goyani
Tags
Grid
Asked by
awhughes
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Share this question
or