This is a migrated thread and some comments may be shown as answers.
Read values from GridTemplateColumn
8 Answers 209 Views
This is a migrated thread and some comments may be shown as answers.
Mario
Top achievements
Rank 1
Mario asked on 22 Apr 2012, 11:01 PM
Hello,
I have a custom button in my grid. After clicking on the button I want the LayoutID and LayoutTypeID read from the grid TemplateColumn. I've tried the following:
<Columns>
    <telerik:GridBoundColumn DataField="PlayerLayoutID" DataType="System.Int64"
        FilterControlAltText="Filter PlayerLayoutID column" HeaderText="PlayerLayoutID"
        ReadOnly="True" SortExpression="PlayerLayoutID"
        UniqueName="PlayerLayoutID" Visible="False">
    </telerik:GridBoundColumn>
    <telerik:GridTemplateColumn DataField="LayoutTypeID"
        FilterControlAltText="Filter LayoutTypeID column" HeaderText="Layout Type"
        UniqueName="LayoutTypeID">
        <EditItemTemplate>
            <telerik:RadComboBox ID="LayoutTypeIDRadComboBox" runat="server"
                DataSourceID="ODSLayoutTypes"
                DataTextField="TypeName"
                DataValueField="TypeID"
                onselectedindexchanged="LayoutTypeIDRadComboBox_SelectedIndexChanged"
                AutoPostBack="True"
                SelectedValue='<%# Bind("LayoutTypeID") %>' Culture="de-DE">
                <Items>
                    <telerik:RadComboBoxItem runat="server" Text="RadComboBoxItem1"
                        Value="RadComboBoxItem1" />
                </Items>
            </telerik:RadComboBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidatorLayoutType"
                runat="server" ErrorMessage="*"
                ControlToValidate="LayoutTypeIDRadComboBox" Display="Dynamic"></asp:RequiredFieldValidator>
            <asp:CustomValidator ID="CustomValidatorLayoutType" runat="server" ErrorMessage="*"
                ControlToValidate="LayoutTypeIDRadComboBox" Display="Dynamic"
                onservervalidate="CustomValidatorLayoutType_ServerValidate" ></asp:CustomValidator>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="LayoutTypeIDLabel" runat="server"
                Text='<%# Eval("TypeName") %>'></asp:Label>
        </ItemTemplate>
    </telerik:GridTemplateColumn>
    <telerik:GridTemplateColumn DataField="LayoutID"
        FilterControlAltText="Filter column1 column" HeaderText="Layout"
        UniqueName="LayoutID">
        <EditItemTemplate>
            <telerik:RadComboBox ID="LayoutIDRadComboBox"
                runat="server" Culture="de-DE" Width="300px" Height="300px"
                OnClientDropDownOpened="OnClientDropDownOpenedHandler"
                ExpandAnimation-Type="None" CollapseAnimation-Type="None"
                <ItemTemplate>
                    <div id="div1" onclick="StopPropagation(event);">
                        <telerik:RadTreeView ID="RTVLayouts"
                            runat="server"                                                                     
                            onnodedatabound="RTVLayouts_NodeDataBound"
                            OnClientNodeClicking="nodeClicking" >
                        </telerik:RadTreeView>
                    </div>
                </ItemTemplate>
                <Items>
                    <telerik:RadComboBoxItem Text="" />
                </Items>                                                   
            </telerik:RadComboBox>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="LayoutIDLabel" runat="server" Text='<%# Eval("LayoutName") %>'></asp:Label>
        </ItemTemplate>
    </telerik:GridTemplateColumn>
    <telerik:GridEditCommandColumn ButtonType="ImageButton"
    FilterControlAltText="Filter EditCommandColumn column"
    HeaderText="<%$ Resources:BasicSettings, Edit %>">
</telerik:GridEditCommandColumn>
<telerik:GridButtonColumn ButtonType="ImageButton" CommandName="Delete"
    ConfirmText="<%$ Resources:BasicSettings, ConfirmDelete %>"
    HeaderText="<%$ Resources:BasicSettings, Delete %>" Text="Delete"
    UniqueName="DeleteColumn">
</telerik:GridButtonColumn>
    <telerik:GridButtonColumn ButtonType="ImageButton" CommandName="makeObjects"
        FilterControlAltText="Filter MakeObjects column" HeaderText="Objects"
        ImageUrl="../images/symbols/settings_16.png" Text="Objects"
        UniqueName="MakeObjects">
    </telerik:GridButtonColumn>
</Columns>
protected void RadGridPlayerLayout_ItemCommand(object sender, GridCommandEventArgs e)
{
    ...
    if (e.CommandName == "makeObjects")
    {
        GridDataItem item = (GridDataItem)e.Item;
        string test = item["LayoutTypeID"].Text;
         
    }
}

Unfortunately that did not work. How can i solve this?

Best regards
Reiner

8 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 23 Apr 2012, 05:47 AM
Hello Mario,

GridDataItem item = (GridDataItem)e.Item;
          string test = (item.FindControl("LayoutTypeIDLabel") as Label).Text;


Thanks,
Jayesh Goyani
0
Mario
Top achievements
Rank 1
answered on 23 Apr 2012, 09:17 AM
Thank you, Jayesh.
Unfortunately I only get the text of the layout types name. I am using a RadComboBox in the EditItemTemplate and in the ItemTemplate i show only the TypeName, not the LayoutTypeID.
Reiner
0
Princy
Top achievements
Rank 1
answered on 23 Apr 2012, 01:30 PM
Hello Mario,

One option is you can add a Hiddenfield column in ItemTemplate to get the ‘LayoutTypeID’ or you can include that field into DataKeyNames and get the value using GetDataKeyValue() method.

aspx:
<MasterTableView DataKeyNames="LayoutID">
 <Columns>
   <telerik:GridTemplateColumn DataField="LayoutID" FilterControlAltText="Filter column1 column" HeaderText="Layout"UniqueName="LayoutID">
    <EditItemTemplate>
         .   .   .  .  .
     </EditItemTemplate>
     <ItemTemplate>
       <asp:Label ID="LayoutIDLabel" runat="server" Text='<%# Eval("LayoutName") %>'></asp:Label>
       <asp:HiddenField ID="HiddenField1" Value='<%# Eval("LayoutID") %>' runat="server" />
    </ItemTemplate>
 </telerik:GridTemplateColumn>
         .  .   .  .  .
   </Columns>
</MasterTableView>
C#:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
 if (e.CommandName == "makeObjects")
 {
   GridDataItem item = (GridDataItem)e.Item;
   HiddenField hidenfield = (HiddenField)item.FindControl("HiddenField1");
   string test = hidenfield.Value;
        //  or
   string test = item.GetDataKeyValue("EmployeeID").ToString();
 }
}

Thanks,
Princy.
0
Mike Wassermann
Top achievements
Rank 1
answered on 19 Nov 2015, 10:49 PM

Hi can't retreaqve the value of the hiden field :

<Columns>
                <telerik:GridTemplateColumn HeaderText="Customer" SortExpression="LastName">
                    <ItemTemplate>
                        <strong><%#eval("LastName") %>, <%#eval("FirstName") %></strong><br />
                        <%#Eval("Address1")%><br />
                        <%#eval("City") %>, <%#eval("State") %> <%#Eval("Zip")%>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn HeaderText="Term" SortExpression="Term">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="lblTerm" />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridBoundColumn HeaderText="Total" DataField="Total" DataFormatString="{0:c}" SortExpression="Total" />
                <telerik:GridBoundColumn HeaderText="VIN" DataField="Vin" SortExpression="VIN" />
                <telerik:GridTemplateColumn HeaderText="Contract" SortExpression="ContractNum">
                    <ItemTemplate>
                        <a target="_blank" style="text-decoration: underline; color: #231F20;" href="../<%#eval("FileName") %>"><%#eval("ContractNum") %></a>
                        <input type="hidden" id="lblContract" value='<%#Eval("ContractNum") %>'></input>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>

 

 Protected Sub RadGrid1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles RadGrid1.SelectedIndexChanged
        Dim item As GridDataItem = DirectCast(RadGrid1.SelectedItems(0), GridDataItem)

        Dim test As HiddenField = DirectCast(item.FindControl("LayoutTypeIDLabel"), HiddenField)
        'Dim val = test.Value
    End Sub

 

When I select the row I want to show the edit button and get the value in the hidden field.

Make suggestions, since I am brand new to this.

0
Eyup
Telerik team
answered on 24 Nov 2015, 09:54 AM
Hi Mike,

You can use a HiddenField control to achieve this requirement:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield%28v=vs.90%29.aspx

And you can access it in edit mode using the following approaches:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows#accessing-controls-in-editinsert-mode

Hope this helps. If you have different requirements or further instructions, please elaborate on your specific scenario.

Regards,
Eyup
Telerik
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 Feedback Portal and vote to affect the priority of the items
0
Swati
Top achievements
Rank 1
answered on 24 Jul 2017, 12:25 PM

Hi,

I am not able to find GridTemplateColumn checkbox in Item Command event.

Getting error of casting for this line and e.item is not GridDatatitem here.

     //GridDataItem item = (GridDataItem)e.Item;   

I have custom button in CommandItemTemplate for Save record and Commandname as "Update" . I want to get GridTemplateColumn checkbox  value in Item comamnd when Update command fires.

How can I achieve this? Could you please help,

 

Thank you in advance.

0
Swati
Top achievements
Rank 1
answered on 24 Jul 2017, 12:28 PM

Hi,

 I am facing error at this line of code ..

GridDataItem item = (GridDataItem)e.Item;

 

I want to find GridTemplateColumn Checkbox value at Item command. 

I have custom Save button in CommandItemTemplate and command name as 'Update'

I want to find checkbox at Item command  when 'Update' command fires 

Could you please help? Its very urgent.

Thank you in advance.

 

0
Swati
Top achievements
Rank 1
answered on 24 Jul 2017, 12:29 PM

hi,

I want to find GridTemplateColumn Checkbox value at Item command. 
I have custom Save button in CommandItemTemplate and command name as 'Update'
I want to find checkbox at Item command  when 'Update' command fires 
Could you please help? Its very urgent.
Thank you in advance.

Tags
Grid
Asked by
Mario
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Mario
Top achievements
Rank 1
Princy
Top achievements
Rank 1
Mike Wassermann
Top achievements
Rank 1
Eyup
Telerik team
Swati
Top achievements
Rank 1
Share this question
or