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

GridEditableItem.UpdateValues() problem with GridMaskedColumn

3 Answers 140 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael Harris
Top achievements
Rank 1
Michael Harris asked on 18 Oct 2012, 12:27 AM
I am having an issue when I use the following column in a grid:

<telerik:GridMaskedColumn DataField="Phone" HeaderText="Phone" UniqueName="Phone" Mask="(###) ###-####" /> 

In code behind:

protected void grid1_UpdateCommand(object sender, GridCommandEventArgs e)
 {
            GridEditableItem item = e.Item as GridEditableItem;
            int licenseSponsorId = Convert.ToInt32(item.GetDataKeyValue("LicenseSponsorId"));
            CDS.LicenseSponsor licenseSponsor = _db.Get<CDS.LicenseSponsor>(licenseSponsorId);
            item.UpdateValues(licenseSponsor);
            _db.Update(licenseSponsor);
 }

The call to UpdateValues() results in "() -" being assigned to the Phone field when the user does not enter any input in the Phone field...

Is there a way to change this behavior?

3 Answers, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 22 Oct 2012, 01:27 PM
Hi Michael,

Thank you for contacting us.

You can not directly change the value. However you could change it in the intercepted event. You could use the ExtractValues() to extract the values from the edit form. Here is a code snippet:
protected void grid1_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        GridEditableItem item = e.Item as GridEditableItem;
        Hashtable table = new Hashtable();
        item.ExtractValues(table);
        //Here you could change the values in the Hashtable
    }
In the Hashtable you could edit the phone and pass the new value or you could set the DefaultInsertValue of the masked field.

All the best,
Angel Petrov
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
Michael Harris
Top achievements
Rank 1
answered on 23 Oct 2012, 02:21 PM
Thanks for the reply...

After I call item.ExtractValues(table) is there a way to update the model object passing in the HashTable?

I like the convenience of item.UpdateValues(modelObject) rather than iterating through the HashTable myself...

0
Angel Petrov
Telerik team
answered on 24 Oct 2012, 12:43 PM
Hi Michael,

There is a better approach that the previously mentioned. You can update both the grid item and the item in the database as shown in the following code snipped:
protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
    {
        var editableItem = ((GridEditableItem)e.Item);
        var sponsorID = (int)editableItem.GetDataKeyValue("LicenseSponsorId");
        //retrive entity form the Db        
        var myItem = DbContext.Products.Where(n => n.LicenseSponsorId == sponsorID).FirstOrDefault();
        if (myItem != null)
        {
            //update entity's state            
            editableItem.UpdateValues(myItem);
            try
            {
                //HERE YOU COULD CHANGE THE PHONE NUMBER
                //IF IT IS EMPTY BEFORE YOU SUBMIT THE CHANGES
                DbContext.SubmitChanges();
            }
            catch (System.Exception)
            {
                ShowErrorMessage();
            }
        }
    }
Feel free to ask if you have any further questions on that matter.

All the best,
Angel Petrov
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.
Tags
Grid
Asked by
Michael Harris
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Michael Harris
Top achievements
Rank 1
Share this question
or