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

editform - determine which mode WebUserControl is in?

3 Answers 82 Views
Grid
This is a migrated thread and some comments may be shown as answers.
matt
Top achievements
Rank 1
matt asked on 30 Jan 2011, 11:28 PM
hello,

im on an older version of RadControls for ASP.NET AJAX ("2009.2 826") but i think the answer to my question should be pretty straight-forward.

im using a RadGrid that uses WebUserControl for the grid's EditFormType. This .ascx form is displayed for Insertion and Update operations. my question -- in the user control's code-behind, how can i tell which mode the code is being executed as? i use this to show different things if the user is performing an Insert vs. an Update.

currently i use this code in the user control's .ascx.cs:

private void Page_PreRender(object sender, EventArgs e)
{
    //this is the only way i know how to check what mode this grid control is operating in.
    bool isGridInsertion = Object.ReferenceEquals(this.DataItem.GetType(), new GridInsertionObject(new ArrayList()).GetType() );
 
    if (isGridInsertion != true) //we check this because the below int assignment breaks on a grid insertion (since ProductTypeID has no value during our Insert form, only populated on Edit)
    {              
        int productTypeID = (int)DataBinder.Eval(this.DataItem, "ProductTypeID");
 
        // do stuff
    }
}

...is there a better way than doing the Object.ReferenceEquals on the DataItem's type (comparing it to a GridInsertionObject's type) ?


thanks!
matt

3 Answers, 1 is accepted

Sort by
0
Accepted
Martin
Telerik team
answered on 03 Feb 2011, 09:24 AM
Hello Matt,

To access the item in which the user control is rendered you can use its Parent.NamingContainer property. Then check the type of that item and perform the required actions based on it:

protected void Page_Load(object sender, System.EventArgs e)
{
     GridEditableItem editedItem = this.Parent.NamingContainer as GridEditableItem;
     if (editedItem is GridEditFormInsertItem)
     {
          // Logic for insert item
     }
     else
     {
          // Logic for edit item
     }
}

For more information on the matter, I would suggest that you review the following help article:

Custom edit forms

I hope this helps.

All the best,
Martin
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
matt
Top achievements
Rank 1
answered on 05 Feb 2011, 07:00 PM
thanks. i see.. so that gets us the same thing as doing this would:

bool isGridInsertion = this.DataItem is GridInsertionObject;

...right? is there any reason i would pick one over the other (speed wise), or do you think its about the same?


thanks,
matt
0
Martin
Telerik team
answered on 07 Feb 2011, 05:45 PM
Hello Matt,

Your approach is applicable for scenarios where DataItem property is implemented in the user control as specified in this help topic. However If it is not implemented, or in case edit operation is triggered, and you need to check the type of the item, then you should use the approach from my previous post which checks the exact type of the generated item and not the DataItem it is bound to.

I hope this helps.

Best wishes,
Martin
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Grid
Asked by
matt
Top achievements
Rank 1
Answers by
Martin
Telerik team
matt
Top achievements
Rank 1
Share this question
or