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

Get value of selected row cell of radgrid for textbox in formview's insertitemtemplate

2 Answers 325 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jerald
Top achievements
Rank 1
Jerald asked on 19 Jul 2016, 05:15 PM

I have a formview that is based on the selected row of a grid.  For fv_vc.ItemInsert, I would like to set the value of the venIdFk textbox in the formview (fv_vc) to the value of venId in the radgrid's selected row.  I am using server-side selection.  For example, if venId (Cell[1]) of the grid's selected row is 81, then 81 would be inserted as the value of venIdFk in the formview.

Here's the grid:

<telerik:RadGrid
                    ID="rg_vendors"
                    runat="server"
                    AllowPaging="True"
                    AllowSorting="True"
                    AllowFilteringByColumn="True"
                    ShowGroupPanel="True"
                    Skin="Office2010Silver"
                    CellSpacing="-1"
                    DataSourceID="sdc_rg_vendors"
                    GridLines="Both"
                    OnItemCommand="rg_vendors_ItemCommand"
                    OnPreRender="rg_vendors_PreRender"
                    EnableViewState="true"
                    OnSelectedIndexChanged="rg_vendors_SelectedIndexChanged">
                    <ClientSettings
                        AllowDragToGroup="True"
                        AllowColumnsReorder="True"
                        ReorderColumnsOnClient="True">
                    </ClientSettings>
                    <MasterTableView
                        DataSourceID="sdc_rg_vendors"
                        AutoGenerateColumns="False"
                        DataKeyNames="venId"
                        CommandItemDisplay="Top">
                        <CommandItemSettings
                            ShowExportToExcelButton="true"
                            ShowAddNewRecordButton="false"
                            ShowRefreshButton="false" />
                            <Columns>  
                                <telerik:GridButtonColumn Text="Select" CommandName="Select" ButtonType="LinkButton"></telerik:GridButtonColumn>                  
                                <telerik:GridBoundColumn DataField="venId" ReadOnly="True" HeaderText="Id" SortExpression="venId" UniqueName="venId" DataType="System.Int32" FilterControlAltText="Filter venId column" Visible="false" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="venNm" HeaderText="Vendor" SortExpression="venNm" UniqueName="venNm" FilterControlAltText="Filter venNm column" HeaderStyle-HorizontalAlign="Center"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="venCity" HeaderText="City" SortExpression="venCity" UniqueName="venCity" FilterControlAltText="Filter venCity column" HeaderStyle-HorizontalAlign="Center"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="stateAbbr" HeaderText="State" SortExpression="stateAbbr" UniqueName="stateAbbr" FilterControlAltText="Filter stateAbbr column" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="venZip" HeaderText="Zip Code" SortExpression="venZip" UniqueName="venZip" FilterControlAltText="Filter venZip column" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="venPh" HeaderText="Phone" SortExpression="venPh" UniqueName="venPh" FilterControlAltText="Filter venPh column" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center"></telerik:GridBoundColumn>
                                <telerik:GridCheckBoxColumn DataField="venActive" HeaderText="Active" SortExpression="venActive" UniqueName="venActive" DataType="System.Boolean" FilterControlAltText="Filter venActive column" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center"></telerik:GridCheckBoxColumn>
                            </Columns>
                    </MasterTableView>
                </telerik:RadGrid>

Here's the formview:

<asp:FormView
                            ID="fv_vc"
                            runat="server"
                            DataKeyNames="vcId"
                            DataSourceID="sdc_fv_vc"
                            OnItemInserted="fv_vc_ItemInserted"
                            OnItemDeleted="fv_vc_ItemDeleted"
                            OnItemUpdated="fv_vc_ItemUpdated">
                            <EmptyDataTemplate>
                                <asp:LinkButton ID="lb_insertitemtemplate" runat="server" OnClick="lb_insertitemtemplate_Click">New Contact</asp:LinkButton>
                            </EmptyDataTemplate>
                            <EditItemTemplate>
                                vcId:
                                <asp:Label Text='<%# Eval("vcId") %>' runat="server" ID="vcIdLabel1" /><br />
                                vcLn:
                                <asp:TextBox Text='<%# Bind("vcLn") %>' runat="server" ID="vcLnTextBox" /><br />
                                vcFn:
                                <asp:TextBox Text='<%# Bind("vcFn") %>' runat="server" ID="vcFnTextBox" /><br />
                                vcEm:
                                <asp:TextBox Text='<%# Bind("vcEm") %>' runat="server" ID="vcEmTextBox" /><br />
                                vcPh:
                                <asp:TextBox Text='<%# Bind("vcPh") %>' runat="server" ID="vcPhTextBox" /><br />
                                vcPrimary:
                                <asp:CheckBox Checked='<%# Bind("vcPrimary") %>' runat="server" ID="vcPrimaryCheckBox" /><br />
                                venIdFk:
                                <asp:TextBox Text='<%# Bind("venIdFk") %>' runat="server" ID="venIdFkTextBox" ReadOnly="true" Visible="false" /><br />
                                <asp:LinkButton runat="server" Text="Update" CommandName="Update" ID="UpdateButton" CausesValidation="True" /> <asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" ID="UpdateCancelButton" CausesValidation="False" />
                            </EditItemTemplate>
                            <InsertItemTemplate>
                                vcLn:
                                <asp:TextBox Text='<%# Bind("vcLn") %>' runat="server" ID="vcLnTextBox" /><br />
                                vcFn:
                                <asp:TextBox Text='<%# Bind("vcFn") %>' runat="server" ID="vcFnTextBox" /><br />
                                vcEm:
                                <asp:TextBox Text='<%# Bind("vcEm") %>' runat="server" ID="vcEmTextBox" /><br />
                                vcPh:
                                <asp:TextBox Text='<%# Bind("vcPh") %>' runat="server" ID="vcPhTextBox" /><br />
                                vcPrimary:
                                <asp:CheckBox Checked='<%# Bind("vcPrimary") %>' runat="server" ID="vcPrimaryCheckBox" /><br />
                                venIdFk:
                                <asp:TextBox Text='<%# Bind("venIdFk") %>' runat="server" ID="venIdFkTextBox" /><br />
                                 
                                <asp:LinkButton runat="server" Text="Insert" CommandName="Insert" ID="InsertButton" CausesValidation="True" /> <asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" ID="InsertCancelButton" CausesValidation="False" />
                            </InsertItemTemplate>
                            <ItemTemplate>                               
                                <asp:Label Text='<%# Eval("vcId") %>' runat="server" ID="vcIdLabel" Visible="false" />
                                <asp:Label Text='<%# Bind("venIdFk") %>' runat="server" ID="venIdFkLabel" Visible="false" />
                                <div class="row">
                                    <div class="col-md-3">
                                        <strong>Last Name</strong><br />
                                        <asp:Label Text='<%# Bind("vcLn") %>' runat="server" ID="vcLnLabel" /><br />
                                    </div>
                                    <div class="col-md-3">
                                        <strong>First Name</strong><br />
                                        <asp:Label Text='<%# Bind("vcFn") %>' runat="server" ID="vcFnLabel" /><br />
                                    </div>
                                    <div class="col-md-3">
                                        <strong>Email</strong><br />
                                        <asp:Label Text='<%# Bind("vcEm") %>' runat="server" ID="vcEmLabel" /><br />
                                    </div>
                                    <div class="col-md-2">
                                        <strong>Phone</strong><br />
                                        <asp:Label Text='<%# Bind("vcPh") %>' runat="server" ID="vcPhLabel" /><br />
                                    </div>
 
                                    <div class="col-md-1">
                                        <strong>Primary</strong><br />
                                        <asp:CheckBox Checked='<%# Bind("vcPrimary") %>' runat="server" ID="vcPrimaryCheckBox" Enabled="false" /><br />
                                    </div>
                                </div>
                                <asp:LinkButton runat="server" Text="Edit" CommandName="Edit" ID="EditButton" CausesValidation="False" /> <asp:LinkButton runat="server" Text="Delete" CommandName="Delete" ID="DeleteButton" CausesValidation="False" /> <asp:LinkButton runat="server" Text="New" CommandName="New" ID="NewButton" CausesValidation="False" />
                            </ItemTemplate>
                        </asp:FormView>

My initial thought was to use a hidden field and set it's value based on the grid's SelectedIndexChanged using something like:

hdn_venId.Value = rg_vendors.SelectedRow.Cells[1].Text;

Then I would set the value of venIdFk in the formview's InsertItemTemplate by making it equal to hdn_venId.  I would like to avoid the use of hidden fields.  Is there a cleaner way of accomplishing this?

Thanks,

J

 

 

2 Answers, 1 is accepted

Sort by
0
Jerald
Top achievements
Rank 1
answered on 20 Jul 2016, 07:17 PM
I decided to stick with using a hidden field and populating it in the FormView's OnDataBound.
0
Maria Ilieva
Telerik team
answered on 22 Jul 2016, 02:18 PM
Hello Jerald,

Currently using hidden field to keep the value and set it back is the optimal approach for achieving this functionality.
Do let us know if this works for you and if further assistance is needed.

Regards,
Maria Ilieva
Telerik by Progress
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Tags
Grid
Asked by
Jerald
Top achievements
Rank 1
Answers by
Jerald
Top achievements
Rank 1
Maria Ilieva
Telerik team
Share this question
or