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

Auto populate defaults for insert

4 Answers 116 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bob
Top achievements
Rank 1
Bob asked on 26 Aug 2009, 06:59 PM
I'm trying to accomplish a few things - and I can't get the last 20%.

I have a RadTabStrip (& MultiPage), a RadGrid and a button on my page.  Currently if you click the button, it will (via client-side JavaScript function), switch to the 2nd tab, set the RadGrid to insert a new record, and focus on the first box.

What I'd like to do is to populate a few of the RadTextBoxes with some values (which were passed to the function when the button was clicked).  However, since the process to change the RadGrid so that it can insert a new record is a server-side hit...I can't figure out how to do this.

Suggestions?

4 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 26 Aug 2009, 08:22 PM
Hello Bob,

There are several (similar) ways, depending on the EditForm type:
Inserting values in-place and EditForms
Inserting values using UserControl/FormTemplate

Some samples:
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    if (e.CommandName == RadGrid.InitInsertCommandName) 
    { 
        // cancel the default operation 
        e.Canceled = true
 
        //Prepare an IDictionary with the predefined values 
        ListDictionary newValues = new ListDictionary(); 
        newValues["ContactName"] = "default contact name"
        newValues["CompanyName"] = " default company name"
 
        //set default value for the dropdown list inside the EditItemTemplate 
        newValues["Country"] = "Germany"
 
        //set default checked state for the checkbox inside the EditItemTemplate 
        newValues["Bool"] = false
 
        //Insert the item and rebind 
        e.Item.OwnerTableView.InsertItem(newValues); 
    } 
}  

private void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    if (e.CommandName == RadGrid.InitInsertCommandName) 
    { 
        e.Canceled = true
        e.Item.OwnerTableView.InsertItem(); 
 
        GridEditableItem insertedItem = e.Item.OwnerTableView.GetInsertItem(); 
        String MyUserControlId = GridEditFormItem.EditFormUserControlID; 
        UserControl MyUserControl = insertedItem.FindControl(MyUserControlId) as UserControl; 
        TextBox box = MyUserControl.FindControl("txtEmployeeID"as TextBox; 
        box.Text = "11"
    } 
}  

private void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    if (e.CommandName == RadGrid.InitInsertCommandName) 
    { 
        e.Canceled = true
        e.Item.OwnerTableView.InsertItem(); 
 
        GridEditableItem insertedItem = e.Item.OwnerTableView.GetInsertItem(); 
        TextBox box = insertedItem.FindControl("txtEmployeeID"as TextBox; 
        box.Text = "11"
    } 
}  

I hope this helps.

Best regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Kris
Top achievements
Rank 1
answered on 02 Mar 2010, 05:10 PM
I am also dealing with this issue. Except I am trying to auto-populate some fields and then hide the controls in the edit form. Then on the insert, it will grab those hidden values.

Here is the code I am using to populate the edit form fields.

 protected void SARTSGrid1_ItemCommand(object source, GridCommandEventArgs e) 
        { 
            if (e.CommandName == RadGrid.InitInsertCommandName) 
            { 
                // cancel the default operation  
                e.Canceled = true
 
                //Prepare an IDictionary with the predefined values  
                ListDictionary newValues = new ListDictionary(); 
                newValues["LastUpdate"] = DateTime.Now.Date.ToShortDateString(); 
                newValues["LastUpdateUser"] = "fullerk"
                newValues["ParentID"] = RecordID.ToString(); 
                newValues["ParentTable"] = "facilities"
 
                //Insert the item and rebind  
                e.Item.OwnerTableView.InsertItem(newValues);  
            }  
        }  


I am hiding the field through the readonly property of the column, i.e.

        protected void SARTSGrid1_ColumnCreated(object sender, Telerik.Web.UI.GridColumnCreatedEventArgs e) 
        { 
            if (e.Column.UniqueName.EndsWith("ID")) 
            { 
                GridBoundColumn col = (GridBoundColumn)e.Column; 
                col.Visible = false
                col.ReadOnly = true
            }  
            if (e.Column.UniqueName.EndsWith("LastUpdate")) 
            { 
                GridBoundColumn col = (GridBoundColumn)e.Column; 
                col.Visible = false
                col.ReadOnly = true
            }  
            if (e.Column.UniqueName.EndsWith("LastUpdateUser")) 
            { 
                GridBoundColumn col = (GridBoundColumn)e.Column; 
                col.Visible = false
                col.ReadOnly = true
            }  
            if (e.Column.UniqueName.EndsWith("ParentTable")) 
            { 
                GridBoundColumn col = (GridBoundColumn)e.Column; 
                col.Visible = false
                col.ReadOnly = true
            } 
            if (e.Column.UniqueName.EndsWith("CallDateTime")) 
            { 
                GridBoundColumn col = (GridBoundColumn)e.Column; 
                col.DataFormatString = "{0:MM/dd/yyyy}"
                col.HeaderText = "Call Date";                 
            } 
            if (e.Column.UniqueName.EndsWith("CallNumber")) 
                e.Column.HeaderText = "Call Number"
            if (e.Column.UniqueName.EndsWith("CallPlacedDateTime")) 
            { 
                GridBoundColumn col = (GridBoundColumn)e.Column; 
                //col.DataFormatString = "{0:MM/dd/yyyy}"; 
                col.HeaderText = "Call Placed Date Time"
            } 
            if (e.Column.UniqueName.EndsWith("CallReceivedDateTime")) 
            { 
                GridBoundColumn col = (GridBoundColumn)e.Column; 
                //col.DataFormatString = "{0:MM/dd/yyyy}"; 
                col.HeaderText = "Call Received Date Time"
            } 
            if (e.Column.UniqueName.EndsWith("CallReturnedDateTime")) 
            { 
                GridBoundColumn col = (GridBoundColumn)e.Column; 
                //col.DataFormatString = "{0:MM/dd/yyyy}"; 
                col.HeaderText = "Call Returned Date Time"
            } 
            if (e.Column.UniqueName.EndsWith("CalledPerson")) 
                e.Column.HeaderText = "Called Person"
            if (e.Column.UniqueName.EndsWith("CallerName")) 
                e.Column.HeaderText = "Caller Name"
            if (e.Column.UniqueName.EndsWith("EmployeeName")) 
                e.Column.HeaderText = "Employee Name"
            if (e.Column.UniqueName.EndsWith("LeftVoicemail")) 
                e.Column.HeaderText = "Left Voicemail?"
        } 

The ItemCommand event fires correctly. It is on the actual click of the Insert that it tries to insert but fails because the auto-populated fields are not retaining their values.

Any ideas on how correct this?

thanks
Kris
0
Princy
Top achievements
Rank 1
answered on 03 Mar 2010, 06:04 AM
Hi,

Please set the  ForceExtractValue="InEditMode"  for the ReadOnly column .This will force the grid to extract such columns.

"RadGrid can extract values from columns that are set as read-only, if the column's ForceExtractValue property is set to:
  • "InBrowseMode" - when deleting records
  • "InEditMode" - when inserting or updating records
  • "Always" - for all modes

The default value for the ForceExtractValue property is "None", which means that the extraction of default values will not be performed for read-only columns."


Thanks,
Princy
0
Kris
Top achievements
Rank 1
answered on 05 Mar 2010, 08:15 PM
That works perfect. Thanks

I have an additional question though. 

The in-edit form works perfectly for updating and inserting records for a grid that has at least one row. But if the grid is empty(No records to display), the Edit Form still appears but has no controls. I am assuming this is because it has no bound columns to create those controls from.

How can I show the columns regardless whether the any records fill the grid? I've tried MasterTableView ShowHeader="true" ShowHeadersWhenNoRecords="true" .

Thanks
Kris

Also, Happy Belated Independence Day, or should I say Честит Ден на Независимостта



Tags
Grid
Asked by
Bob
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Kris
Top achievements
Rank 1
Princy
Top achievements
Rank 1
Share this question
or