1

先生/女士,

我将通过单击添加来开发插入命令,以将 RadTextbox 显示为 InsertItem 模板,并在将所有数据输入到 RadGrid 内后单击添加新记录

但是进入测​​试时发现Getting the Items 无法完成,总是显示null

以下是我的代码,请回答

     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>
4

1 回答 1

2

我认为您的代码基于以下示例

该演示使用自动插入,它自动处理控件的值以及使用数据源插入数据库。在您的情况下,您希望通过代码访问控件。

我不得不简化代码来表达我的观点。这应该得到你需要的

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
于 2012-11-17T18:55:26.803 回答