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

Accessing Readonly/Hidden Fields in Edit Mode (RadGrid)

12 Answers 626 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Peter Maslin
Top achievements
Rank 1
Peter Maslin asked on 23 Jul 2009, 03:08 PM
hi

i use the following Method for retreiving the fields from the edit form (Which is Auto Generated)

 

void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)  
{  
GridEditFormItem Data = (GridEditFormItem)e.Item;  
Hashtable ht = new Hashtable();  
Data.ExtractValues(ht);  

the problem i am having is that i have several readonly/hidden fields in the grid , but this method does not return those fields , only the ones visible to the user ,

currently there are 5 fields on the screen that the user can edit , and about 3-4 that are non editable , but i need the values from them to be able to do the update on the database.

the above method will only return the visible items.

what method can be used to get the edited items as well as the readonly items from the row etc.

many thanks

Peter.

12 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 24 Jul 2009, 05:46 AM
Hi Peter,

I tried the following approach to access the value of the ReadOnly field in the UpdateCommand event.

ASPX:
 
<telerik:GridBoundColumn UniqueName="ProductName" ReadOnly="true" HeaderText="ProductName" DataField="ProductName" > 
           </telerik:GridBoundColumn> 

CS:
 
 protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e) 
    { 
        GridEditFormItem Data = (GridEditFormItem)e.Item;  
        string strHiddenField=Data.ParentItem["ProductName"].Text; 
        Hashtable ht = new Hashtable(); 
        Data.ExtractValues(ht); 
        ht.Add("ProductName", strHiddenField); 
    } 


Thanks
Princy
0
Peter Maslin
Top achievements
Rank 1
answered on 24 Jul 2009, 08:24 AM
hi Princy

thats exactly what i needed , a simple and straight forward awnser , thanks alot.

Peter
0
Peter Maslin
Top achievements
Rank 1
answered on 24 Jul 2009, 08:25 AM
hi Princy

thats exactly what i needed , a simple and straight forward awnser , thanks alot.

Peter
0
Peter Maslin
Top achievements
Rank 1
answered on 24 Jul 2009, 08:45 AM
hi Princy

thats exactly what i needed , a simple and straight forward awnser , thanks alot.

Peter
0
Amy Liu
Top achievements
Rank 1
answered on 29 Jun 2010, 11:12 PM
I don't get it that it only work one field for me:

Code below
      GridEditFormItem Data = (GridEditFormItem)e.Item;  
        string strHiddenField = Data.ParentItem["username"].Text;  
       // string strHiddenSID = Data.ParentItem["SID"].Text;  
        Hashtable ht = new Hashtable();  
        Data.ExtractValues(ht);  
        ht.Add("username", strHiddenField);  
        //ht.Add("SID", Convert.ToInt32(strHiddenSID)); 

if I only use username field, update is successful.
the SID in the table is foreign kay and bigint.
If I add the SID as readOnly then I am unable to update. the error says try to insert null into sid field of the table.
I checked the code. strHiddenSID has value.

Please tell me why. Thanks for any help in advance.

 

 

 

0
Amy Liu
Top achievements
Rank 1
answered on 30 Jun 2010, 02:13 PM
Ha. I solved it not from the function but from by setting the the column's property ForceExtractValue !!!
0
Amy Liu
Top achievements
Rank 1
answered on 30 Jun 2010, 02:41 PM
I don't think the approach works for me. why? Even the hash table have all the fields values but these never been passed to update command for me.The record has been updated with these fields empty. I used Grid with ajax. there should some thing else there I didn't consider. Please tell me. thanks.
0
Tsvetina
Telerik team
answered on 05 Jul 2010, 01:33 PM
Hi Amy,

In order to further troubleshoot the problem, try if this approach works fine for you when AJAX is turned off. In some cases it might  influence the update process, depending on the way it is used.

You might also view this help topic on Updating values in-place and with edit forms, where you will find detailed example on how to handle the UpdateCommand event on a ReadOnly column.

Regards,
Tsvetina
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
MK
Top achievements
Rank 1
answered on 17 Sep 2012, 12:16 PM
I got following error -

Unable to cast object of type 'Telerik.Web.UI.GridDataItem' to type 'Telerik.Web.UI.GridEditFormItem'

Any solve it?
0
Tsvetina
Telerik team
answered on 20 Sep 2012, 07:34 AM
Hi,

Make sure that you are checking the item type before casting the item at the line where you get the error. If you are not sure how to do this, paste your code here, so we can take a look.

Regards,
Tsvetina
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
Eric Harrelson
Top achievements
Rank 2
answered on 22 Dec 2012, 11:45 PM
For what it's worth, this worked for me:
Private Sub grid_UpdateCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles grid.UpdateCommand
 
    Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem)
    Dim uc As UserControl = CType(e.Item.FindControl(GridEditFormItem.EditFormUserControlID), UserControl)
 
 
    Dim Code As String = editedItem.GetDataKeyValue("RaceID")
    Dim RaceName As String = CType(uc.FindControl("txtRaceName"), TextBox).Text
 
 
    For Each r As GRCMA.Data.Race In Me.ModelContext.Races.ToList()
 
        If r.RaceID = Code Then
 
            r.RaceName = RaceName
 
            Me.ModelContext.SaveChanges()
 
            BindData()
 
        End If
 
    Next
 
End Sub

That's working against a grid with this definition:

<telerik:RadGrid ID="grid" runat="server" GridLines="None" PageSize="50" AllowPaging="True" AutoGenerateColumns="False" ShowStatusBar="true" CssClass="RadGrid">
    <MasterTableView Width="100%" CommandItemDisplay="Top" DataKeyNames="RaceID" PagerStyle-PageSizes="50">
        <Columns>
            <telerik:GridEditCommandColumn UniqueName="editcol" ButtonType="ImageButton" />
            <telerik:GridButtonColumn CommandName="Delete" ConfirmText="Are you sure you wish to delete this record?" Text="Delete" UniqueName="Delete" ButtonType="ImageButton" />
            <telerik:GridBoundColumn UniqueName="RaceID" ReadOnly="true" HeaderText="Race ID" DataField="RaceID" />
            <telerik:GridBoundColumn UniqueName="RaceName" HeaderText="Race Name" DataField="RaceName" />
            <telerik:GridBoundColumn UniqueName="UsedBy" ReadOnly="true" HeaderText="# Users" DataField="Users.Count" />
        </Columns>
        <EditFormSettings UserControlName="RaceEditor.ascx" EditFormType="WebUserControl">
            <EditColumn UniqueName="EditCommandColumn1">
            </EditColumn>
        </EditFormSettings>
        <ExpandCollapseColumn ButtonType="ImageButton" Visible="True" UniqueName="ExpandColumn">
            <HeaderStyle Width="19px"></HeaderStyle>
        </ExpandCollapseColumn>
    </MasterTableView>
</telerik:RadGrid>

Here's the UserControl where I'm doing my editing
:
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="RaceEditor.ascx.vb" Inherits="TM.RaceEditor" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<table id="Table2" cellspacing="2" cellpadding="5" width="100%" border="1" rules="none" style="border-collapse: collapse">
    <tr class="EditFormHeader">
        <td>
            RaceID:
        </td>
        <td>
            <asp:TextBox runat="server" ID="txtRaceID" value='<%# DataBinder.Eval( Container, "DataItem.RaceID" ) %>' /><asp:RequiredFieldValidator ID="rfvRaceID" runat="server" ControlToValidate="txtRaceID" ErrorMessage="required" ForeColor="Red" />
        </td>
    </tr>
    <tr>
        <td>
            Race Name:
        </td>
        <td>
            <asp:TextBox runat="server" ID="txtRaceName" value='<%# DataBinder.Eval( Container, "DataItem.RaceName" ) %>' /><asp:RequiredFieldValidator ID="rfvRaceName" runat="server" ControlToValidate="txtRaceName" ErrorMessage="required" ForeColor="Red" />
        </td>
    </tr>
    <tr>
            <td align="right" colspan="2">
                <asp:Button ID="btnUpdate" Text="Update" runat="server" CommandName="Update" Visible='<%# Not (TypeOf DataItem Is Telerik.Web.UI.GridInsertionObject) %>' />
                <asp:Button ID="btnInsert" Text="Insert" runat="server" CommandName="PerformInsert" Visible='<%# (TypeOf DataItem Is Telerik.Web.UI.GridInsertionObject) %>' />
                  
                <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel" />
            </td>
    </tr>
</table>
Code behind for ascx:
Public Class RaceEditor
    Inherits UserControlBaseClass
 
    Private _dataItem As Object = Nothing
 
    Public Property DataItem() As Object
        Get
            Return Me._dataItem
        End Get
        Set(ByVal value As Object)
            Me._dataItem = value
        End Set
    End Property
 
    Protected Sub RaceEditor_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DataBinding
 
        ' allow user to edit raceid if inserting
 
 
        If TypeOf DataItem Is Telerik.Web.UI.GridInsertionObject Then
 
            ' new mode
 
            txtRaceID.ReadOnly = False
            rfvRaceID.Enabled = True
 
        Else
 
            'edit mode
 
            txtRaceID.ReadOnly = True
            rfvRaceID.Enabled = False
 
 
        End If
 
    End Sub
 
End Class

0
Vasil
Telerik team
answered on 28 Dec 2012, 09:31 AM
Hello,

I am attaching a project archive kindly provided by Eric.
I have striped out the Telerik.OpenAccess.35.Extensions.dll and Telerik.Web.UI.dll assemblies, add them in order to run the project.

Greetings,
Vasil
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
Peter Maslin
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Peter Maslin
Top achievements
Rank 1
Amy Liu
Top achievements
Rank 1
Tsvetina
Telerik team
MK
Top achievements
Rank 1
Eric Harrelson
Top achievements
Rank 2
Vasil
Telerik team
Share this question
or