Telerik radgrid rebind()抛出对象引用未设置为对象实例的错误
本文关键字:设置 对象 实例 错误 对象引用 rebind radgrid Telerik | 更新日期: 2023-09-27 17:49:16
我在一个页面上有两个rad Grid视图。当我从左网格中删除项目时,它应该添加到右网格,反之亦然。我正在使用rad ajax管理器。请看下面的代码:
<telerik:RadGrid runat="server" ID="grdAvailableIOptions" OnNeedDataSource="grdAvailableIOptions_NeedDataSource"
AllowPaging="false" Width="500px" OnRowDrop="grdAvailableIOptions_RowDrop" AllowMultiRowSelection="true" RetainExpandStateOnRebind="true"
PageSize="30" AutoGenerateColumns="false" OnItemCreated="grdAvailableIOptions_ItemCreated" OnItemCommand="grdAvailableIOptions_ItemCommand">
<MasterTableView DataKeyNames="ID" Width="100%" TableLayout="Fixed" ShowHeader="false" GroupsDefaultExpanded="false">
<GroupByExpressions>
<telerik:GridGroupByExpression>
<SelectFields>
<telerik:GridGroupByField FieldAlias="-" FieldName="GroupName"></telerik:GridGroupByField>
</SelectFields>
<GroupByFields>
<telerik:GridGroupByField FieldName="SortOrder"></telerik:GridGroupByField>
</GroupByFields>
</telerik:GridGroupByExpression>
</GroupByExpressions>
<Columns>
<telerik:GridDragDropColumn HeaderStyle-Width="18px" Visible="false">
</telerik:GridDragDropColumn>
<telerik:GridBoundColumn DataField="Name" DataType="System.String" HeaderText="Name"
SortExpression="Name" UniqueName="Name">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Price" DataType="System.String" HeaderText="Price" HeaderStyle-HorizontalAlign="Right"
SortExpression="Price" UniqueName="Price" DataFormatString="{0:c}" ItemStyle-HorizontalAlign="Right">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn HeaderText="Delete" AllowFiltering="false" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton ID="lnkAddItem" ToolTip="Add Machine Item" ImageUrl="~/Content/Images/Add New.png" CommandName="AddItem" CommandArgument=' <%# Eval("ID") %>' runat="server"></asp:ImageButton>
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
<ClientSettings AllowRowsDragDrop="True">
<Selecting AllowRowSelect="True" EnableDragToSelectRows="false"></Selecting>
<ClientEvents OnRowDropping="onRowDropping"></ClientEvents>
</ClientSettings>
</telerik:RadGrid>
<telerik:RadGrid runat="server" AllowPaging="false" ID="grdSelectedOptions" OnNeedDataSource="grdSelectedOptions_NeedDataSource"
Width="550px" AllowMultiRowSelection="true" AutoGenerateColumns="false"
OnItemDataBound="grdSelectedOptions_ItemDataBound" OnItemCommand="grdSelectedOptions_ItemCommand" OnItemCreated="grdSelectedOptions_ItemCreated">
<MasterTableView DataKeyNames="ID" Width="100%" ShowHeader="false">
<GroupByExpressions>
<telerik:GridGroupByExpression>
<SelectFields>
<telerik:GridGroupByField FieldAlias="-" FieldName="GroupName"></telerik:GridGroupByField>
</SelectFields>
<GroupByFields>
<telerik:GridGroupByField FieldName="SortOrder"></telerik:GridGroupByField>
</GroupByFields>
</telerik:GridGroupByExpression>
</GroupByExpressions>
<SortExpressions>
<telerik:GridSortExpression FieldName="SortOrder" SortOrder="Ascending" />
</SortExpressions>
<Columns>
<telerik:GridDragDropColumn HeaderStyle-Width="18px" Visible="false">
</telerik:GridDragDropColumn>
<telerik:GridTemplateColumn HeaderText="Item" UniqueName="MachineItem">
<ItemTemplate>
<asp:Label runat="server" ID="lblItem" Visible="false"></asp:Label>
<telerik:RadComboBox runat="server" ID="RadComboBox1" Width="100%" Visible="false" AutoPostBack="true" OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged">
</telerik:RadComboBox>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Quantity" UniqueName="Quantity" Display="false">
<ItemTemplate>
<asp:TextBox runat="server" ID="txtQuantity" AutoPostBack="true" Width="30px" Text='<%#Eval("Quantity") %>' OnTextChanged="txtQuantity_TextChanged"></asp:TextBox>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn DataField="Price" HeaderText="Price" HeaderStyle-HorizontalAlign="Right"
SortExpression="Price" UniqueName="Price" ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:Label runat="server" ID="lblPrice" Text='<%#Eval("Price","{0:c}") %>'></asp:Label>
<asp:HiddenField runat="server" ID="hdnPrice" Value='<%#Eval("Price") %>'></asp:HiddenField>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Delete" AllowFiltering="false">
<ItemTemplate>
<asp:ImageButton ID="lnkDelete" Visible="false" ToolTip="Delete Machine Item" ImageUrl="~/Common/Images/del.gif" CommandName="Delete" CommandArgument=' <%# Eval("ID") %>' runat="server" OnClientClick="javascript:if (!confirm('Are you sure you want to delete Machine Item ?')) {
return false;
}"></asp:ImageButton>
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
<NoRecordsTemplate>
<div style="height: 30px; cursor: pointer;">
No items to view
</div>
</NoRecordsTemplate>
</MasterTableView>
<ClientSettings AllowRowsDragDrop="True">
<Selecting AllowRowSelect="True" EnableDragToSelectRows="false"></Selecting>
<ClientEvents OnRowDropping="onRowDropping"></ClientEvents>
</ClientSettings>
</telerik:RadGrid>
这里是rad ajax管理器
<telerik:AjaxSetting AjaxControlID="grdAvailableIOptions">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="grdAvailableIOptions"></telerik:AjaxUpdatedControl>
<telerik:AjaxUpdatedControl ControlID="grdSelectedOptions"></telerik:AjaxUpdatedControl>
<telerik:AjaxUpdatedControl ControlID="msg"></telerik:AjaxUpdatedControl>
<telerik:AjaxUpdatedControl ControlID="lblEstimateTotal"></telerik:AjaxUpdatedControl>
<telerik:AjaxUpdatedControl ControlID="lblConfirmationText"></telerik:AjaxUpdatedControl>
<telerik:AjaxUpdatedControl ControlID="modalPopup"></telerik:AjaxUpdatedControl>
</UpdatedControls>
</telerik:AjaxSetting>
<telerik:AjaxSetting AjaxControlID="grdSelectedOptions">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="grdSelectedOptions"></telerik:AjaxUpdatedControl>
<telerik:AjaxUpdatedControl ControlID="grdAvailableIOptions"></telerik:AjaxUpdatedControl>
<telerik:AjaxUpdatedControl ControlID="msg"></telerik:AjaxUpdatedControl>
<telerik:AjaxUpdatedControl ControlID="lblEstimateTotal"></telerik:AjaxUpdatedControl>
<telerik:AjaxUpdatedControl ControlID="lblConfirmationText"></telerik:AjaxUpdatedControl>
<telerik:AjaxUpdatedControl ControlID="modalPopup"></telerik:AjaxUpdatedControl>
</UpdatedControls>
</telerik:AjaxSetting>
下面是我得到这个错误的代码
protected void grdAvailableIOptions_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "AddItem")
{
try
{
int ID = Convert.ToInt32(e.CommandArgument.ToString());
MachineItems tmpMachineItems = GetMachineItem(AvailableIOptionsList, ID);
if (itemRules.Any(i => i.MachineItemID == ID))
{
btnConfirm.CommandArgument = ID.ToString();
lblConfirmationText.InnerHtml = identifyRules(tmpMachineItems);
// When there is a rule of allowed subgroups then this text will be empty.
// Dont show popup just add the item and make the related items available for selection
if (string.IsNullOrEmpty(lblConfirmationText.InnerHtml))
{
SelectedOptionsList.Add(tmpMachineItems);
AvailableIOptionsList.Remove(tmpMachineItems);
lblEstimateTotal.Text = (Decimal.Parse(lblEstimateTotal.Text, numberStyle) + tmpMachineItems.Price).ToString("c");
CheckForAllowedSubgroups();
}
else
{
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "key", "showRulesModal();", true);
}
}
else
{
SelectedOptionsList.Add(tmpMachineItems);
AvailableIOptionsList.Remove(tmpMachineItems);
}
grdSelectedOptions.Rebind();
grdAvailableIOptions.Rebind();
}
catch (Exception ex)
{
throw ex;
}
}
}
我得到一个错误在这一点** grdavailableoptions . rebind();**。它正确地调用了需要的数据源方法,但是在调用了需要的数据源方法之后,它引发了没有设置为实例错误的对象引用。
有谁能指出我哪里做错了吗?我也想知道是否有可能只是使用ajax管理器刷新/重新绑定网格而不使用radgrid.rebind()方法?
在检查了很多之后,我发现有一个空项被添加到我的数据源,这导致了这个问题。因此,在将空对象添加到数据源之前,请始终检查空对象。
希望有人发现这些信息有用。