Accessing Readonly/Hidden Fields in Edit Mode (RadGrid)

13 posts, 1 answers
  1. Peter Maslin
    Peter Maslin avatar
    23 posts
    Member since:
    Mar 2009

    Posted 23 Jul 2009 Link to this post

    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.

  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 24 Jul 2009 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Peter Maslin
    Peter Maslin avatar
    23 posts
    Member since:
    Mar 2009

    Posted 24 Jul 2009 Link to this post

    hi Princy

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

    Peter
  5. Peter Maslin
    Peter Maslin avatar
    23 posts
    Member since:
    Mar 2009

    Posted 24 Jul 2009 Link to this post

    hi Princy

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

    Peter
  6. Peter Maslin
    Peter Maslin avatar
    23 posts
    Member since:
    Mar 2009

    Posted 24 Jul 2009 Link to this post

    hi Princy

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

    Peter
  7. Amy Liu
    Amy Liu avatar
    28 posts
    Member since:
    Dec 2009

    Posted 29 Jun 2010 Link to this post

    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.

     

     

     

  8. Amy Liu
    Amy Liu avatar
    28 posts
    Member since:
    Dec 2009

    Posted 30 Jun 2010 Link to this post

    Ha. I solved it not from the function but from by setting the the column's property ForceExtractValue !!!
  9. Amy Liu
    Amy Liu avatar
    28 posts
    Member since:
    Dec 2009

    Posted 30 Jun 2010 Link to this post

    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.
  10. Tsvetina
    Admin
    Tsvetina avatar
    1876 posts

    Posted 05 Jul 2010 Link to this post

    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
  11. MK
    MK avatar
    29 posts
    Member since:
    Jul 2010

    Posted 17 Sep 2012 Link to this post

    I got following error -

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

    Any solve it?
  12. Tsvetina
    Admin
    Tsvetina avatar
    1876 posts

    Posted 20 Sep 2012 Link to this post

    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.
  13. Eric Harrelson
    Eric Harrelson avatar
    3 posts
    Member since:
    Feb 2010

    Posted 22 Dec 2012 Link to this post

    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

  14. Vasil
    Admin
    Vasil avatar
    1547 posts

    Posted 28 Dec 2012 Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017