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

Inserting a userControl in a specific radGrid cell

6 Answers 157 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sammy78
Top achievements
Rank 2
Sammy78 asked on 13 Jul 2010, 02:01 PM
Hi,
I was wondering if anyone could help me out. I'm trying to add a simple userControl to only one cell of a radGrid. I don't need to repeat it in every cell of that column (since the rest of the column is supposed to be text) and the radGrid has more than one row (4 by 4 to be exact and my uc must be in the bottom one on the right).
Any help would be greatly appreciated.

Thanks a lot
Sam

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 14 Jul 2010, 02:08 PM
Hello,

You can add a UserControl in specific cell of RadGrid by using following code snippet.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridDataItem)
       {
           GridDataItem item = (GridDataItem)e.Item;
           if (item.ItemIndex == 3)
           {
               UserControl uc = (UserControl)Page.LoadControl("~/WebUserControl.ascx"); //Path to your user control
               item["ColumnUniqueName"].Controls.Add(uc);
           }
       }
   }
   protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridDataItem)
       {
           GridDataItem item = (GridDataItem)e.Item;
           if (item.ItemIndex == 3)
           {
                UserControl uc = (UserControl)Page.LoadControl("~/WebUserControl.ascx");//Path to your user control
                item["ColumnUniqueName"].Controls.Add(uc);
           }
       }
   }

Thanks,
Princy.
0
Sammy78
Top achievements
Rank 2
answered on 14 Jul 2010, 02:26 PM
Thanks a lot for the reply, Princy.
I'll try it out as soon as I can and will get back to you with the results.

Again, thank you
Sam
0
Sammy78
Top achievements
Rank 2
answered on 14 Jul 2010, 08:28 PM
Hi Princy,
I just wanted to let you knowe that your snippet worked like a charm. Thanks a lot for the help. Now all I need to figure out is how to get the values of the textboxes that are in the userControl. :)

Thanks again.
Sam
0
Accepted
Princy
Top achievements
Rank 2
answered on 15 Jul 2010, 08:08 AM
Hello,

 I guess you want to access the TextBox placed inside UserControl on an external event other than grid events. If so, you can access UserControl first using its ID, which is set in ItemCreated event and then the embedded TextBox control using FindControl() method. Check out the following code snippet.

C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridDataItem)
       {
           GridDataItem item = (GridDataItem)e.Item;
           if (item.ItemIndex == 3)
           {
               UserControl uc = (UserControl)Page.LoadControl("~/WebUserControl.ascx");
               uc.ID = "MyUserControl";
               item["ColumnUniqueName"].Controls.Add(uc);
           }
       }
   }
   protected void Button1_Click(object sender, EventArgs e)
   {
       GridDataItem item = (GridDataItem)RadGrid1.MasterTableView.Items[3]; // access the corresponding grid row
       UserControl uc = (UserControl)item["ColumnUniqueName"].FindControl("MyUserControl"); // access UserControl
       TextBox txt = (TextBox)uc.FindControl("TextBox1");//access the TextBox
       string value = txt.Text;
   }

Thanks,
Princy.
0
Sammy78
Top achievements
Rank 2
answered on 15 Jul 2010, 02:51 PM
Hi Princy,
thanks for another quick reply. That's exactly what I'm trying to do. The only difference is that my userControl is built dynamically. Basically, it's a placeholder in which I add radNumericTextboxes, depending on the number entered in the previous page (3 users = 3 boxes). Here is the code:

VB:
Dim nbreVoyageurs As Integer = 'number of clients
        Dim deuxiemeLigne As Boolean = False
        Dim i As Integer = 0
        Dim tableAnnulation As New Table
        Dim tRow As New TableRow
 
         
        For i = 0 To nbreVoyageurs - 1
            Dim tCell As New TableCell
 
            TextBoxAnnul = New Telerik.Web.UI.RadNumericTextBox
            TextBoxAnnul.Width = 60
            TextBoxAnnul.Skin = "Windows7"
            TextBoxAnnul.MaxValue = 200000
            TextBoxAnnul.MinValue = 0
            TextBoxAnnul.MaxLength = 3
            TextBoxAnnul.NumberFormat.DecimalDigits = 2
            TextBoxAnnul.NumberFormat.GroupSeparator = " "
            TextBoxAnnul.AllowOutOfRangeAutoCorrect = True
            TextBoxAnnul.ShowButton = False
            TextBoxAnnul.Text = 'text source
 
            If (i < 6) Then   'Pour inscrire les composantes dans la première ligne du tableau
                TextBoxAnnul.ID = "AnnulVoy" + (i + 1).ToString
                lblAnnulVoyageur = New Label
                lblAnnulVoyageur.Text = "Voyageur_" + (i + 1).ToString + ": "
                lblAnnulVoyageur.ID = "lblAnnulVoyageur" + (i + 1).ToString
 
            Else    'si on est rendu au 6ième élément, il faut changer de ligne du tableau
                If deuxiemeLigne = False Then
                    tableAnnulation.Rows.Add(tRow)
                    deuxiemeLigne = True
                    tRow = New TableRow
 
                    TextBoxAnnul.ID = "AnnulVoy" + (i + 1).ToString
                    lblAnnulVoyageur = New Label
                    lblAnnulVoyageur.Text = "Voyageur_" + (i + 1).ToString + ": "
                    lblAnnulVoyageur.ID = "lblAnnulVoyageur" + (i + 1).ToString
 
                Else    'si on est déjà dans la deuxième ligne du tableau
 
                    TextBoxAnnul.ID = "AnnulVoy" + (i + 1).ToString
                    lblAnnulVoyageur = New Label
                    lblAnnulVoyageur.Text = "Voyageur_" + (i + 1).ToString + ": "
                    lblAnnulVoyageur.ID = "lblAnnulVoyageur" + (i + 1).ToString
 
                End If
 
            End If
            tCell.Controls.Add(lblAnnulVoyageur)
            tRow.Cells.Add(tCell)
            tCell = New TableCell
            tCell.Controls.Add(TextBoxAnnul)
            tRow.Cells.Add(tCell)
 
        Next
 
        tableAnnulation.Rows.Add(tRow)
 
        phNbreVoyageurs.Controls.Add(tableAnnulation)


I tried your code and it works great, but I can't get the UserControl, for some reason. Here is what I tried:

VB:
Protected Sub btnGetValue_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnGetValue.Click
     
    Dim item As GridDataItem = DirectCast(rgRecommandation.MasterTableView.Items(3), GridDataItem)
    Dim uc As UserControl = DirectCast(item("Description").FindControl("NbreVoyageurs"), UserControl)
    Dim txt As RadNumericTextBox = DirectCast(uc.FindControl("AnnulVoy1"), RadNumericTextBox)
    lblMontant.Text = txt.Text
End Sub


If you have any idea as to why I can't get any value for "uc", I would be very greatful.

Thanks again for all the help
Sam
0
Sammy78
Top achievements
Rank 2
answered on 15 Jul 2010, 04:11 PM
Yes! I managed to get my value.
I reviewed your example and noticed that I had forgotten to add the id when I added the UC in the radGrid.
Seriously, thanks for all the help.

Have a good day.
Sincerely yours
Sam
Tags
Grid
Asked by
Sammy78
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Sammy78
Top achievements
Rank 2
Share this question
or