Unable to read value from template coloum

2 posts, 0 answers
  1. Millfield
    Millfield avatar
    3 posts
    Member since:
    Dec 2012

    Posted 18 Sep 2015 Link to this post

    HI I have created a Radgrid and added a textbox as a template coloumn, 

    When the user presses the ok button i want any values that have been entered into the textbox to be returned

    I believe i have code correct on the event of the ok button but i am always returned a Null value. I have looked at a sample of code found here http://kandydeol.blogspot.co.uk/2013/09/fetching-telerik-radgrid-column-and.html

    and as far as I can see all is correct,

    aspx code :-

     <telerik:RadGrid ID="GRD_OTCOrder" runat="server" AutoGenerateColumns="False" OnItemDataBound="GRD_OTCOrder_ItemDataBound" Culture="en-GB" OnNeedDataSource="GRD_OTCOrder_NeedDataSource" GroupPanelPosition="Top" Width="1173px">
            <MasterTableView TableLayout="Auto">
                <Columns>
                    <telerik:GridBoundColumn DataField="TxtName" HeaderText="OTC Name / Type"></telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Packsize" HeaderText="Pack size" ItemStyle-Width="100px">
                        <ItemStyle Width="100px"></ItemStyle>
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Units" ItemStyle-Width="100px">
                        <ItemStyle Width="100px"></ItemStyle>
                    </telerik:GridBoundColumn>
                    <telerik:GridTemplateColumn HeaderText="Qty Required">
                        <ItemTemplate>
                            <asp:TextBox ID="TB_QTYOrder" runat="server" Width="50px"></asp:TextBox>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn>
                        <ItemTemplate>
                            <telerik:RadComboBox ID="DDL_Reason" runat="server"></telerik:RadComboBox>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn Visible="false">
                        <ItemTemplate>
                            <asp:Label ID="LAB_OTCID" runat="server"></asp:Label>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>

                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
        <telerik:RadButton ID="BTN_Order" runat="server" Text="Place Order" OnClick="BTN_Order_Click"></telerik:RadButton>
        <telerik:RadComboBox ID="DDL_House" runat="server" ></telerik:RadComboBox>

     

    and the cs code attached to the ok button

     

    protected void BTN_Order_Click(object sender, EventArgs e)
            {

                foreach (GridDataItem data in GRD_OTCOrder.Items)
                {
                    //find controls in Grid
                    RadComboBox reason = (RadComboBox)data.FindControl("DDL_Reason");
                    TextBox qtyre = (TextBox)data.FindControl("TB_QTYOrder");
                    Label otc = (Label)data.FindControl("LAB_OTCID");

                    //create new OTC Object in memory 
                    OTCOrders temp = new OTCOrders();
                    temp.House = Convert.ToInt64(DDL_House.SelectedValue);
                    temp.OrderBy = Websession.sessionuser.UserID;
                    temp.OTCType = Convert.ToInt32(otc.Text);
                    if (!string.IsNullOrEmpty(qtyre.Text))
                    {
                        temp.QTY = Convert.ToInt32(qtyre.Text);
                    }

                    temp.BolSent = false;
                    temp.BolRec = false;
                    temp.SentBy = 0;
                    temp.RecBy = 0;
                    temp.DteSent = DateTime.Now;
                    temp.dterec = DateTime.Now;
                    temp.House = Convert.ToInt64(DDL_House.SelectedValue);
                    temp.Reason = reason.SelectedValue;
                    temp.DteOrdered = DateTime.Now;
                    //Save object to database
                        temp.SaveOrder();
                    
                }

            }

     

    Many Thanks

     

     

  2. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2466 posts

    Posted 23 Sep 2015 Link to this post

    Hi Millfield,

    The code snippet that you have provided seems correct and after testing it with dummy data, everything is working correctly on my side:
    <telerik:RadGrid ID="GRD_OTCOrder" runat="server" AutoGenerateColumns="False" Culture="en-GB" OnNeedDataSource="RadGrid1_NeedDataSource" GroupPanelPosition="Top" Width="1173px">
        <MasterTableView TableLayout="Auto">
            <Columns>
                <telerik:GridBoundColumn DataField="TxtName" HeaderText="OTC Name / Type"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Packsize" HeaderText="Pack size" ItemStyle-Width="100px">
                    <ItemStyle Width="100px"></ItemStyle>
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Units" ItemStyle-Width="100px">
                    <ItemStyle Width="100px"></ItemStyle>
                </telerik:GridBoundColumn>
                <telerik:GridTemplateColumn HeaderText="Qty Required">
                    <ItemTemplate>
                        <asp:TextBox ID="TB_QTYOrder" runat="server" Width="50px"></asp:TextBox>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn>
                    <ItemTemplate>
                        <telerik:RadComboBox ID="DDL_Reason" runat="server">
                            <Items>
                                <telerik:RadComboBoxItem Text="item1" Value="item1" />
                                <telerik:RadComboBoxItem Text="item2" Value="item2" />
                                <telerik:RadComboBoxItem Text="item3" Value="item3" />
                            </Items>
                        </telerik:RadComboBox>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn Visible="false">
                    <ItemTemplate>
                        <asp:Label ID="LAB_OTCID" runat="server"></asp:Label>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
     
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
    <telerik:RadButton ID="BTN_Order" runat="server" Text="Place Order" OnClick="BTN_Order_Click"></telerik:RadButton>
    <telerik:RadComboBox ID="DDL_House" runat="server"></telerik:RadComboBox>

    And the code-behind:
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("ID", typeof(int));
        table.Columns.Add("TxtName", typeof(string));
        table.Columns.Add("Packsize", typeof(string));
        for (int i = 0; i < 5; i++)
        {
            table.Rows.Add(i, "TxtName" + i, "Packsize" + i);
        }
     
        (sender as RadGrid).DataSource = table;
    }
     
    protected void BTN_Order_Click(object sender, EventArgs e)
    {
        string result = "";
        foreach (GridDataItem data in GRD_OTCOrder.Items)
        {
            TextBox qtyre = (TextBox)data.FindControl("TB_QTYOrder");
            result += "row" + data.ItemIndexHierarchical + " TextBox value: " + qtyre.Text + "<br />";
        }
     
        Response.Write(result);
    }

    On a side note, please ensure that you are not calling the Rebind method of the grid, because it will clear the values in the TextBox control.

    Hope this helps.


    Regards,
    Konstantin Dikov
    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
Back to Top