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

Get DataKeyValues for GridCommandItem on RadGrid1_ItemCreated

2 Answers 114 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sam
Top achievements
Rank 1
Sam asked on 10 Dec 2013, 07:33 PM
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                if (e.Item.OwnerTableView.Name == "Parent")
                {                   
                    Button editLink = (Button)e.Item.FindControl("EditLink");
                    editLink.Attributes["href"] = "javascript:void(0);";
                    editLink.Attributes["onclick"] = String.Format("return ShowEditForm('{0}','{1}','{2}','{3}');",
                        e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["EID"],
                        e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["RID"],
                        e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["VID"],
                        e.Item.ItemIndex);                   
                }
                else if (e.Item.OwnerTableView.Name == "Child")
                {
                    // nothing
                }
            }
            if (e.Item is GridCommandItem)
            {
                if (e.Item.OwnerTableView.Name == "Parent")
                {
                    Button addLink = (Button)e.Item.FindControl("AddNewRecordLink");
                    addLink.Attributes["href"] = "javascript:void(0);";
                    addLink.Attributes["onclick"] = String.Format("return ShowInsertForm('{0}','{1}');",
                        e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["EID"],
                        e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["VID"]);
                }
            }
        }

An exception of type 'System.ArgumentOutOfRangeException' occurred

Can anyone please help me what wrong in the bold text of code block above?  I tried to get DataKeyValues and assign to asp.Button on CommandItemTemplate of RadGrid1

it works in GridDataItem, but it does not work in GridCommandItem

Thanks,
Sam

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 12 Dec 2013, 04:13 AM
Hi Sam,

If you want to access a control in CommandItemTemplate please try the following code snippet:

C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
 if (e.Item is GridCommandItem)
    {
        if (e.Item.OwnerTableView.Name == "Parent")
        {
          GridCommandItem cmditem = (GridCommandItem)e.Item;
          Button addLink = (Button)cmditem.FindControl("AddNewRecordLink");
          addLink.Attributes["href"] = "javascript:void(0);";
          addLink.Attributes["onclick"] = String.Format("return ShowInsertForm();");     
        }
    }  
}

Please note that you cant access the DataKeyValues on Insert since they are not inserted yet. Using the ShowInsertForm page you are going to accept the EID,VID values, hence it will be available only after insert.

Thanks,
Princy
0
Sam
Top achievements
Rank 1
answered on 12 Dec 2013, 03:00 PM
Thanks Princy!

  • your code snippet works for me
  • To work around, I captured the values to HiddenField control and pass to ShowInsertForm function
  • Because on ShowInsertForm page, I do not know what values of EID and VID to accept

Thanks again,
Sam

Tags
Grid
Asked by
Sam
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Sam
Top achievements
Rank 1
Share this question
or