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

RadGrid Insert/Edit/Update Accessing Public Properties in a user control without using reflection (FindControl)

2 Answers 116 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 03 Jul 2012, 11:25 PM
So here is what I am trying to achieve.  I have multiple radgrids on a single page in a tabbed view.  Some of these radgrids implement the same user control.  However they pull different data.  I have public properties set in the usercontrol that reference the input fields (RadTextBox, RadComboBox, Checkbox, etc.).  When I implement the user control driectly on the page without binding it to a radgrid everything works properly.  I can do:
RadTextBox item1 = userControl1.Item1TextBox;
InsertItem(item1.Text);
where InsertItem is a method that inserts the item text into the database

However on the page with the radgrids the way the user control is bound is
protected override void OnInit(EventArgs e)
{
   base.OnInit(e)
        RadGrid1.Skin = Skin;
        RadGrid1.Width = GridWidth;
        RadGrid1.AutoGenerateColumns = false;
        RadGrid1.NeedDataSource += RadGrid1_NeedDataSource;
        RadGrid1.NeedDataSource += RadGrid1_InsertCommand;
        RadGrid1.NeedDataSource += RadGrid1_UpdateCommand;
        RadGrid1.NeedDataSource += RadGrid1_DeleteCommand;
        RadGrid1.MasterTableView.Width = Unit.Percentage(100);
        RadGrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
        RadGrid1.MasterTableView.CommandItemSettings.ShowAddNewRecordButton = true;
        RadGrid1.MasterTableView.CommandItemSettings.ShowRefreshButton = false;
        RadGrid1.MasterTableView.ShowHeadersWhenNoRecords = true;
        RadGrid1.MasterTableView.EditFormSettings.UserControlName = "/includes/userControls/RoofForm.ascx";
        RadGrid1.MasterTableView.EditFormSettings.EditFormType = GridEditFormType.WebUserControl;
        RadGrid1.MasterTableView.DataKeyNames = DataKeys;
}
with the main point being:

RadGrid1.MasterTableView.EditFormSettings.UserControlName = "/includes/userControls/RoofForm.ascx";
RadGrid1.MasterTableView.EditFormSettings.EditFormType = GridEditFormType.WebUserControl;

So my question is:  Is it possible to access these public properties which return a control in the InsertCommand/UpdateCommand bindings or am I forced to use the FindControl?

For example:
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
    UserControl userControlForm= e.Item.FindControl(GridEditFormItem.EditFormUserControlID) as UserControl;
 
    RadComboBox item1 = userControlForm.Property1;
    RadComboBox item2 = userControlForm.Property2;
    RadComboBox item3 = userControlForm.Property3;
    RadComboBox item4 = userControlForm.Property4;
    RadComboBox item5 = userControlForm.Property5;
    RadDatePicker item6 = userControlForm.Property6;
    RadNumericTextBox item7 = userControlForm.Property7;
    RadNumericTextBox item8 = userControlForm.Property8;
 
        if (insert(item1.text, item2.text, item3.text, item4.text, etc..)
        {
            ErrorMessages.Text += "<br />The  item was saved but there were other errors";
        }
        else
        {
            AllSaved = false;
            ErrorMessages.Text += "<br />The item was not saved.  Please go to the parent entry to add the item.";
        }
}


If this isn't clear let me know and I will be happy to elaborate.

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 04 Jul 2012, 05:18 AM
Hello Kevin,

You can use FindControl method to access controls in UserControl in InsertCommand. Here is the sample code that I tried which worked as expected.
C#:
protected void RadGrid2_InsertCommand(object sender, GridCommandEventArgs e)
{
  GridEditableItem editedItem = e.Item as GridEditableItem;
  UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
  TextBox txtbox = (TextBox)userControl.FindControl("TextBox1");
}

Thanks,
Shinu.
0
Kevin
Top achievements
Rank 1
answered on 05 Jul 2012, 04:21 PM
Thanks.  I mistakenly though that for some reason the FindControl method was a part of the reflections.dll and I try to avoid using reflection because I understand that it can really slow down the application.  Since I was doing this for n number of controls for n number of user controls I was afraid that the cost was going to bog my system on production.  However I did a little more research and found that they are actually a part of the Web.UI dll.
Tags
Grid
Asked by
Kevin
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Kevin
Top achievements
Rank 1
Share this question
or