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

RadGrid EditTemplate controls access

6 Answers 192 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Alin
Top achievements
Rank 1
Alin asked on 10 Jul 2011, 05:02 AM
Hello

I want to access a control which is in the EditTemplate, when the grid is in edit mode. The control would become visible or not
when user chooses an option from a RadioButtonList(possible when grid in edit mode only), and the only ideea I had was to treat the OnSelectedIndexChanged of the RadioButtonList, and then set the Visible property of some control:
protected void PollDistribPointsTypeOptions_SelectedIndexChanged(object sender, EventArgs e)
    {
        RadioButtonList rbl = sender as RadioButtonList;
        if(rbl.SelectedValue == "0" || rbl.SelectedValue=="2")
            (RadGrid1.EditItems[0] as GridEditableItem)["Poll_DistribPointsType"].Controls[3].Visible = false;
    }

Problem is I only get the controls of the ItemTemplate, not the EditTemplate, where the desired controll exists :(
Is there a workaround for this?

Thx a lot.

6 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 11 Jul 2011, 07:50 AM
Hello Alin,

Try the following code snippet to access a control in EditItemTemplate.

C#:
protected void PollDistribPointsTypeOptions_SelectedIndexChanged(object sender, EventArgs e)
{
     RadioButtonList rbl = sender as RadioButtonList;
     GridEditableItem item = (GridEditableItem)(sender as RadioButtonList).NamingContainer;
     string emp = item["FirstName"].Text.ToString();
     Label lbl = (Label)item.FindControl("Label1");
     lbl.Visible = false;
}

Thanks,
Princy.
0
Alin
Top achievements
Rank 1
answered on 11 Jul 2011, 10:54 AM
Of course, that works :)
Thx a lot!
0
Amit
Top achievements
Rank 1
answered on 22 Feb 2012, 08:17 PM
Is it possible to access other controls in EditTemplate when in edit mode beside the drop down list, because drop down list causes post back we can handle the event and access the other controls like you mentioned above. What I want to do is when user click the edit button on row item, I want to access a textbox which is in the EditTemplate and enable a required field validator on this text box. The field is required or not required depends on the drop down list in the form but since user is in edit mode he is not changing item on the drop down box.

If there a way please also let me know the event in which I should access them.

Please help me, I have spend so much time and still haven't found a solution yet.

Thanks in advance,
Amit
0
Andrey
Telerik team
answered on 27 Feb 2012, 02:10 PM
Hello,

You could achieve your goal by hooking the OnItemDataBound event of RadGrid. In its body you should check whether the item is GridEditFormItem and whether the item is in edit mode. After that you could find the TextBox control using the following code:

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditFormItem editForm = e.Item as GridEditFormItem;
        TextBox textBox = editForm.FindControl("TextBoxID") as TextBox;
        //your custom code here...
    }       
}

You should change the "TextBoxID" string with the server ID of the TextBox control you are trying to find.

Give this approach a try and check whether if it works for you.

Greetings,
Andrey
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
Dan
Top achievements
Rank 2
answered on 27 Feb 2012, 03:14 PM
Kudo's for posting this answer.  I had been fighting a similar, but different, problem.  I'll post this addendum for those who might need to find a solution to this.  If you need to show a control in a FormTemplate when in Insert mode vs. not show the control in Edit mode, try this:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
                if (e.Item.RowIndex > 0)
                {
                    GridEditFormItem editForm = e.Item as GridEditFormItem;
                    RadComboBox rcb = (RadComboBox)editForm.FindControl("cmbContact2");
                    rcb.Visible = false;
                    Label lbl = (Label)editForm.FindControl("lblContactLabel");
                    lbl.Visible = false;
                }
                else
                {
                    GridEditFormItem editForm = e.Item as GridEditFormItem;
                    RadComboBox rcb = (RadComboBox)editForm.FindControl("cmbContact2");
                    rcb.Visible = true;
                    Label lbl = (Label)editForm.FindControl("lblContactLabel");
                    lbl.Visible = true;
                }
         }
}
0
Casey
Top achievements
Rank 1
answered on 27 Feb 2012, 03:28 PM
Hi Dan,

Another way of determining if the RadGrid is in Edit mode vs. Insert is to check RadGrid1.MasterTableView.IsItemInserted.

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
   if (e.Item.IsInEditMode)
        {
            if (RadGrid1.MasterTableView.IsItemInserted)
            {
                GridEditFormInsertItem insItm = e.Item as GridEditFormInsertItem;
                // your code here
            }
            else
            {
                GridEditFormItem editItm = e.Item as GridEditFormItem;
                // your code here
            }
        }
}
Tags
Grid
Asked by
Alin
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Alin
Top achievements
Rank 1
Amit
Top achievements
Rank 1
Andrey
Telerik team
Dan
Top achievements
Rank 2
Casey
Top achievements
Rank 1
Share this question
or