Telerik RadGrid - Adding Items into dropdown list

2019-06-03 18:07发布

Sir/Madam,

I am going to develop a insert command by clicking Add to display the RadTextbox as InsertItem Templates and click Add New Record after inputting all the datas INside the RadGrid

But when it goes into the testing, it is found that Getting the Items cannot be done and always show null

thw following is my code , please answer

     protected void btnAddRecord_grdFlex_performInsert(object sender, EventArgs e)
    {
        RadButton butt = sender as RadButton;

        foreach (GridDataItem dataItem in grdFlex.MasterTableView.Items)
        {

            RadTextBox textBox1 = (RadTextBox)(dataItem["Company"].FindControl("txtSubcomName"));
            RadTextBox textBox2 = (RadTextBox)(dataItem["FlexAcctCode"].FindControl("txtFlex"));
            RadComboBox ab= (RadComboBox)(dataItem["Company"].FindControl("cboCompany"));
            RadComboBox cd= (RadComboBox)(dataItem["FlexAcctCode"].FindControl("cboFlexAcctCode"));


            if (!String.IsNullOrEmpty(textBox1.Text))
            {
                ab.Items.Insert(1, new RadComboBoxItem(textBox1.Text));
                ab.SelectedIndex = 0;
                textBox1.Text = String.Empty;
            }

            if (!String.IsNullOrEmpty(textBox2.Text))
            {
                cd.Items.Insert(1, new RadComboBoxItem(textBox2.Text));
                cd.SelectedIndex = 0;
                textBox2.Text = String.Empty;
            }
        }
    }

   ASP://



         <telerik:RadGrid ID="grdFlex" runat="server" AutoGenerateColumns="False"

    ShowStatusBar="true" AllowAutomaticDeletes="True" AllowAutomaticInserts="True"
    AllowAutomaticUpdates="True" 
     EnableDynamicPageSize="False"
                        AllowMultiRowSelection="true" EnableToolbar="False" OnNeedDataSource="grdFlex_NeedDataSource"

                        Width="80%">
                        <MasterTableView CommandItemDisplay="TopAndBottom" EditMode="InPlace"  >
                            <CommandItemTemplate>
                                <div style="padding: 5px 5px;">
                                    <telerik:RadButton runat="server" Text="Add new" ID="btnAdd_grdFlex"  CommandName="InitInsert"
                                        Visible='<%# !grdFlex.MasterTableView.IsItemInserted %>'>
                                        <Icon PrimaryIconUrl="~/Image/Button/add.gif" PrimaryIconLeft="8" PrimaryIconTop="4" />
                                    </telerik:RadButton>
                                    &nbsp;&nbsp;
                                    <telerik:RadButton runat="server" AutoPostBack="false" Text="Delete" ID="btnDelete_grdFlex" 
                                        OnClientClicked="grdFlex_onDeleteClick" CausesValidation="false" Visible='<%# !grdFlex.MasterTableView.IsItemInserted %>'>
                                        <Icon PrimaryIconUrl="~/Image/Button/remove.gif" PrimaryIconLeft="8" PrimaryIconTop="4" />
                                    </telerik:RadButton>
                                    &nbsp;&nbsp;
                                    <telerik:RadButton runat="server" Text="Add New record" ID="btnAddRecord_grdFlex" CommandName="PerformInsert" OnCommand="btnAddRecord_grdFlex_performInsert" 
                                        Visible='<%# grdFlex.MasterTableView.IsItemInserted %>'>

                                        <Icon PrimaryIconUrl="~/Image/Button/yes.gif" PrimaryIconLeft="8" PrimaryIconTop="4" />
                                    </telerik:RadButton>
                                    &nbsp;&nbsp;
                                    <telerik:RadButton runat="server" Text="Cancel editing" ID="btnCancel_grdCancel" CommandName="CancelAll"
                                        Visible='<%# grdFlex.EditIndexes.Count > 0 || grdFlex.MasterTableView.IsItemInserted %>'>
                                        <Icon PrimaryIconUrl="~/Image/Button/cancel.gif" PrimaryIconLeft="8" PrimaryIconTop="4" />
                                    </telerik:RadButton>
                                    &nbsp;&nbsp;
                                </div>
                            </CommandItemTemplate>

 ...........

                                      <telerik:GridTemplateColumn UniqueName="Company"  HeaderText="Company">
                                <InsertItemTemplate>
                                <telerik:RadTextBox ID="txtSubcomName" runat="server" ></telerik:RadTextBox>
                                </InsertItemTemplate>
                                    <ItemTemplate>
                                        <telerik:RadComboBox ID="cboCompany" runat="server" Filter="Contains"   AppendDataBoundItems="true" AllowCustomText="True"
                                            Width="100%" SelectedValue='<%# Bind("Company") %>'>
                                            <Items>
                                                <telerik:RadComboBoxItem Text="Yau Lee Construction Co. Ltd" Value="YLC" />
                                                <telerik:RadComboBoxItem Text="Yau Lee Holdings" Value="YLH" />
                                            </Items>
                                        </telerik:RadComboBox>
                                    </ItemTemplate>
                                    <ItemStyle Width="200px" />
                                </telerik:GridTemplateColumn>
                                <telerik:GridTemplateColumn UniqueName="FlexAcctCode" HeaderText="Flex A/C Code">
                             <InsertItemTemplate>

1条回答
叼着烟拽天下
2楼-- · 2019-06-03 18:21

I take it you based your code on the following example

This demo is using automatic inserts which handles the value of the controls automatically as well as uses the datasource to insert into the database. In you case you want access to the controls through code.

I had to simplify the code to get my point across. This should get what you need

C#

protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
{
var dataItem = e.Item as GridDataItem;
var textBox = dataItem.findControl("txtSubcomName") as RadTextBox;
//... get the rest of the controls like this
textBox2.Text = textBox.Text;
}

ASP.net

<telerik:RadGrid ID="RadGrid1" runat="server" 
        OnInsertCommand="RadGrid1_InsertCommand">
        <MasterTableView DataKeyNames="ID">
            <Columns>
                <telerik:GridButtonColumn UniqueName="InsertColumn" ButtonType="ImageButton" CommandName="Insert">
                </telerik:GridButtonColumn>
<telerik:GridTemplateColumn UniqueName="Company"  HeaderText="Company">
                                <InsertItemTemplate>
                                <telerik:RadTextBox ID="txtSubcomName" runat="server" ></telerik:RadTextBox>
                                </InsertItemTemplate>
                                    <ItemTemplate>
                                        <telerik:RadComboBox ID="cboCompany" runat="server" Filter="Contains"   AppendDataBoundItems="true" AllowCustomText="True"
                                            Width="100%" SelectedValue='<%# Bind("Company") %>'>
                                            <Items>
                                                <telerik:RadComboBoxItem Text="Yau Lee Construction Co. Ltd" Value="YLC" />
                                                <telerik:RadComboBoxItem Text="Yau Lee Holdings" Value="YLH" />
                                            </Items>
                                        </telerik:RadComboBox>
                                    </ItemTemplate>
                                    <ItemStyle Width="200px" />
                                </telerik:GridTemplateColumn>
... rest of grid
查看更多
登录 后发表回答