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

How do you make use of the FireCommandEvent?

4 Answers 189 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Boris
Top achievements
Rank 1
Boris asked on 24 Oct 2012, 07:55 PM
All I want to do is fire off a command to kick a RadGrid into Add mode on the initial load of a page.

If I put the command RadGrid1.MasterTableView.Items[0].FireCommandEvent("InitInsert", ""); into a button click event it does exactly what I want to do.

If I try to put it into any Grid or page event, it crashes.  None of the examples that I've found so far in the forums seem to work.

Sorry.  Found it.  (Not intuitively obvious....)

protected void RadGrid1_PreRender(object sender, EventArgs e)

{

    RadGrid1.MasterTableView.IsItemInserted = true; // Check for the !IsPostBack also for initial page load

    RadGrid1.Rebind();

}

4 Answers, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 26 Oct 2012, 05:24 AM
Hi Boris,

You can even save the additonal Rebind and the ensueing overhead by setting IsItemInserted property in the page's OnInit event. Attached is a small sample.


Greetings,
Tsvetoslav
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
Boris
Top achievements
Rank 1
answered on 28 Oct 2012, 09:12 PM
I'm still having problems.  

No matter where I put this  RadGrid1.MasterTableView.IsItemInserted = true, I get a crash, apparently due to the dropdown boxes on my FormTemplate.  

It's usually something like 'ddl1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value. 

(I usually bind them in the ItemCreatedEvent.)
0
Shinu
Top achievements
Rank 2
answered on 29 Oct 2012, 11:34 AM
Hi,

I was able to replicate the issue when I set the DropDownList Selected value in the ItemCreated event and put the RadGrid in Insert mode on Page load or press 'Add New Record' Button. One suggestion is to bind the selected value only when the Radgrid is in edit mode as follows.

C#:
protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridEditFormItem && e.Item.IsInEditMode && !e.Item.OwnerTableView.IsItemInserted) //Edit Mode
    {
        GridEditFormItem ditem = (GridEditFormItem)e.Item;
        DropDownList ddl = (DropDownList)ditem.FindControl("DDropDownList1");
 
        //your code to bind DropDownList
        //set selected value of DropDownList
 
    }
    if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted) // Insert Mode
    {
         GridEditFormInsertItemitem =(GridEditFormInsertItem)e.Item;
        DropDownList ddl = (DropDownList)item.FindControl("DDropDownList1");
 
        //your code to bind DropDownList
         
    }
}

Thanks,
Shinu.
0
Boris
Top achievements
Rank 1
answered on 29 Oct 2012, 01:53 PM
Still no good.  Same crash.

What's even odder is that there are two dropdowns on the form and both are getting populated correctly. 

(And both are being populated by identical techniques.

However if I trap the crash in the button event and let the screen display, the first dropdown displays correctly while the second is empty.  I've tried reversing the order in which the two are bound in code but the result is the same.

Let's cancel this thread.  I replaced the dropdown lists with RadCombos and the problem went away.

(But thanks for pointer on distinguishing between Insert and Update.  I was looking for that too.)

Tags
Grid
Asked by
Boris
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
Boris
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or