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

RadGrid Dinamically and Itemtemplate problem

8 Answers 200 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Lasly
Top achievements
Rank 1
Lasly asked on 05 Oct 2011, 11:43 AM
Hi,
I've a radgrid create in runtime mode as in the example 
I've a problem when i'm going to retrieve the value into the item template (Checkbox.Checked)

I need to retreive the value during the click event of a button external to radgrid.

How can I do?
Best Regards

8 Answers, 1 is accepted

Sort by
0
Tsvetina
Telerik team
answered on 10 Oct 2011, 08:27 AM
Hello Lasly,

If you have created the grid correctly and access controls as advised by our documentation, there should be no problem with accessing parts of the grid on external button click.
So, we will need to see the code that you use for creating the grid and for accessing the control inside it in order to tell you what is wrong in the approach taken.

Regards,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Lasly
Top achievements
Rank 1
answered on 10 Oct 2011, 09:13 AM
 This is the method to create radgrid (note:i'm using radtabstripcontrol with ascx loaded in runtime mode.):
public void CaricaRadGrid()
   {
       PHradGrid.Controls.Clear();
       //Creo il RadGrid
       RadGrid grdUIMillesimi = new RadGrid();
       grdUIMillesimi.AutoGenerateColumns = false;
       grdUIMillesimi.MasterTableView.EnableColumnsViewState = false;
       grdUIMillesimi.Height = 450;
       grdUIMillesimi.ClientSettings.Scrolling.AllowScroll = true;
       grdUIMillesimi.ClientSettings.Scrolling.ScrollHeight = 450;
 
 
 
       //Carico l'elenco delle ui
       DataTable dtUI = ElencoUI();
         
       //Creo le colonne del radgrid
       creaColonna(grdUIMillesimi, "stabile", "stabile", "Stabile", typeof(string));
       creaColonna(grdUIMillesimi, "ui", "ui", "UI", typeof(string));
       creaColonna(grdUIMillesimi, "DestUso", "DestUso", "Dest.Uso", typeof(string));
       creaColonna(grdUIMillesimi, "Piano", "Piano", "Piano", typeof(string));
       creaColonna(grdUIMillesimi, "MQ", "mq", "MQ", typeof(decimal));
       creaColonna(grdUIMillesimi, "percAbbattimento", "percAbbattimento", "% Abbattimento", typeof(decimal));
       creaColonna(grdUIMillesimi, "mqmm", "mqmm", "MQ Millesimali", typeof(string));
       creaColonna(grdUIMillesimi, "hpiano", "hpiano", "H Piano", typeof(decimal));
       creaColonna(grdUIMillesimi, "hprogressiva", "hprogressiva", "H Progressiva", typeof(decimal));
       //Ciclo il datatablein per creare le colonne dei mastri che rientrano nella gestione millesimale.
       foreach (DataRow dr in dtUI.Rows)
       {
           for (int i = 10; i < dtUI.Columns.Count; i += 4)
           {
               // recuperiamo la colonna gestito e splittiamo per avere l'ID del mastro
               // la descrizione del mastro è la prima colonna
               // DESC | GEST | FORMULA
               if (grdUIMillesimi.MasterTableView.Columns.FindByUniqueNameSafe(dr.Table.Columns[i + 2].ToString().Split('_')[1]) == null)
               {
                   //creo la colonna dei mastri
                   creaColonna(grdUIMillesimi, dr.Table.Columns[i + 2].ToString().Split('_')[1], dr.Table.Columns[i].Caption, dr[i + 1].ToString(), typeof(decimal));
                   //creo la checkbox per far rientrare l'ui nella gestione del mastro in maniera tale da poter calcolare i dati.
                   GridTemplateColumn template = new GridTemplateColumn();
                   grdUIMillesimi.MasterTableView.Columns.Add(template);
                   template.ItemTemplate = new CheckBoxTemplate(this.Page, dr.Table.Columns[i + 3].ToString().Split('_')[1], Convert.ToBoolean(dr[i + 2]));
                   template.UniqueName = dr.Table.Columns[i + 3].ToString();
                   template.DataField = dr.Table.Columns[i + 2].Caption;
                   //Aggiungo la checkbox
 
               }
           }
       }
 
       PHradGrid.Controls.Add(grdUIMillesimi);
       //Associo l'evento NeedDataSource
       ((RadGrid)PHradGrid.Controls[0]).NeedDataSource += new GridNeedDataSourceEventHandler(grdUIMillesimi_NeedDataSource);
       //Associo l'evento ItemCreated
       //((RadGrid)PHradGrid.Controls[0]).ItemCreated += new GridItemEventHandler(grdUIMillesimi_ItemCreated);
       ((RadGrid)PHradGrid.Controls[0]).ItemDataBound += new GridItemEventHandler(grdUIMillesimi_ItemDataBound);
       ((RadGrid)PHradGrid.Controls[0]).Rebind();
   }


This is method to access control:
foreach (GridDataItem dataItem in ((RadGrid)PHradGrid.Controls[0]).MasterTableView.Items)
       {
           if (dtUI.Rows.Count > 0)
           {
               DataRow dr = dtUI.Rows[dataItem.ItemIndex];
               for (int i = 10; i < dtUI.Columns.Count; i += 4)
               {
 
                   CheckBox checkBox = (CheckBox)dataItem.FindControl(dr.Table.Columns[i + 3].ToString().Split('_')[1]);
                   if (checkBox.Checked)
                   {
                             // My code.....
                          }
                     }
               }
           }

Thanks in advance.
0
Tsvetina
Telerik team
answered on 10 Oct 2011, 04:23 PM
Hello Lasly,

Thank you for posting the code.

One additional question would be when exactly you are creating the grid? It could be either Page_Init, Page_Load or PageViewCreated event of the RadMultiPage (which I assume you are using)? If it is outside of these events, please move it to one of them.

Also, following the order of creating and adding the controls to their parent contorls, you should first attach the event handlers and then add the grid to the placeholder. Also, you should not be calling Rebind() at this point of the grid's lifecycle, since this could mess up its normal flow of events. You could try the TabClick or TabDataBound (if applicable) event to call Rebind() for the grid if it is not initially visible on the page. If the pageview where the grid belongs is always rendered on the page, the control should bind without any explicit calls. 

All the best,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Lasly
Top achievements
Rank 1
answered on 11 Oct 2011, 10:40 AM
Hi thanks for the reply.

I create radgrid in pageLoad of the ascx.
I followed the method suggested in your documentation,but my problem is not resolved because i need to retrieve checked property value set by client side, not server side.
i don't wanna bind the value from database.

Best Regards
0
Tsvetina
Telerik team
answered on 12 Oct 2011, 08:27 PM
Hello Lasly,

I partially misled you in my previous post. Actually, when creating templates dynamically, you would need to create them in Page_Init in order for them to function properly. So, try modifying your code to follow the logic of the last section in the article you initially linked to:
http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html#Section4

Then, if you want to work with the checkboxes on the client, you could attach their onclick client event (through the Attributes collection) and handle the changes in their values.
If you need to access them all at some moment, you could loop through the grid rows client side ( gridInstance.get_masterTableView().get_dataItems() returns a collection of them ) and use jQuery to retrieve all checkboxes and retrieve their checked state.

Greetings,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Lasly
Top achievements
Rank 1
answered on 12 Oct 2011, 08:43 PM
Thanks for the reply.
I follow the example to create the grid.
I can use jquery and client's events to retrieve the value but how can I pass this values at server? with a callback or async request or sync request?
I don't think that this is the best way to work with the radgrid? right?
If I do a postback I don't have the value in the checkbox, because my radgrid is not in ViewState and fires his events but doesn't have any value.
Thanks

Best Regards
0
Accepted
Tsvetina
Telerik team
answered on 17 Oct 2011, 07:07 AM
Hi Lasly,

Depending on your goal, you can use the value either to fire a command through RadGrid and handle it in the ItemCommand server event, or to fire and AJAX request with and argument and handle it in the AjaxRequest event of RadAjaxManager/RadAjaxPanel control.

For the first you could take a look at the following article which explains the fireCommand client method (you can use a custom command name apart from the ones listed in the table):
fireCommand

As for the ajax request, the API that we expose for this purpose is described here:
Client-Side API


Regards,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Lasly
Top achievements
Rank 1
answered on 21 Oct 2011, 07:42 AM
Thanks to all,
with your support we found a solution, fires the checked change event which saves the commandargument into Hashtable, as suggest in one of your demo.
So we able to retrieve all values of the checked boxs.

Thanks
Best Regards
Tags
Grid
Asked by
Lasly
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Lasly
Top achievements
Rank 1
Share this question
or