0

在尝试实现字符串搜索功能以指出用户搜索的网格行时,我遇到了令人费解的异常。这是我正在使用的代码片段,以前这段代码与 griddatabound 列完美配合,现在它向我抛出 No property or field 'ObjSN' exists in type 'DataRowView'

ASPX

<telerik:RadComboBox ID="rcbFieldName" runat="server" Label="Pencarian: " LabelCssClass="filterLabel">
    <Items>
        <telerik:RadComboBoxItem Text="Container No." Value="ObjSN" />
        <telerik:RadComboBoxItem Text="Seal No." Value="TrxSeal" />                                      
    </Items>
</telerik:RadComboBox>
<telerik:RadTextBox ID="txtSearch" runat="server" OnTextChanged="txtSearch_TextChanged" />
<telerik:RadButton ID="btnSearchGrid" runat="server" Text="Search" ToolTip="Search Record" />
<telerik:RadButton ID="btnShowAllItem" runat="server" Text="Reset" ToolTip="Reset View" OnClick="btnShowAll_Click" />

<telerik:RadGrid ID="RadGrid1" 
    GridLines="None" 
    AutoGenerateColumns="false" 
    PageSize="10"
    AllowPaging="true" 
    AllowSorting="true" 
    runat="server" 
    DataSourceID="MasterViewDataSource" 
    OnItemDataBound="OnItemDataBoundHandler" 
    OnItemCommand="RadGrid1_ItemCommand"
    AllowAutomaticInserts="True" 
    AllowAutomaticDeletes="True"
    ShowStatusBar="true">
    <PagerStyle Mode="NextPrevAndNumeric" />
        <MasterTableView ShowFooter="false" DataKeyNames="TrxId" EditMode="InPlace" CommandItemDisplay="TopAndBottom">
            <Columns>
                <telerik:GridBoundColumn DataField="TrxId" HeaderText="Trx No." ReadOnly="true" DataFormatString="{0:0000}" UniqueName="TrxId"/>

                    <telerik:GridTemplateColumn UniqueName="ObjId" HeaderText="Container No."
                        SortExpression="ObjSN">
                        <FooterTemplate>Template footer</FooterTemplate>
                        <FooterStyle VerticalAlign="Middle" HorizontalAlign="Center" />
                            <ItemTemplate>
                                <%#DataBinder.Eval(Container.DataItem, "ObjSN")%>
                            </ItemTemplate>

                        <EditItemTemplate>
                            <telerik:RadComboBox runat="server" ID="RadComboBox1" EnableLoadOnDemand="True" DataTextField="ObjSN" 
                                OnItemsRequested="RadComboBox1_ItemsRequested" OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged" 
                                DataValueField="ObjId" AutoPostBack="true" HighlightTemplatedItems="true" Height="140px" Width="220px" 
                                DropDownWidth="300px">
                                    <HeaderTemplate>
                                        <ul>
                                            <li class="col1">Container No.</li>
                                            <li class="col2">Container Type</li>
                                       </ul>
                                    </HeaderTemplate>

                                    <ItemTemplate>
                                        <ul>
                                            <li class="col1">
                                                <%# DataBinder.Eval(Container, "Text")%>
                                            </li>
                                            <li class="col2">
                                                <%# DataBinder.Eval(Container, "Attributes['ObjType']")%>
                                            </li>
                                       </ul>
                                  </ItemTemplate>
                             </telerik:RadComboBox>
                        </EditItemTemplate>
                   </telerik:GridTemplateColumn>
             </Columns>
         </MasterTableView>
    </telerik:RadGrid>
<asp:SqlDataSource ID="MasterViewDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ZEUS %>"
     SelectCommand="SELECT TransactsData.TrxId,TransactsData.ObjId, 
                           ObjectsData.ObjSN, ObjectsData.ObjType  

                    FROM   TransactsData INNER JOIN              
                           ObjectsData ON TransactsData.ObjId = ObjectsData.ObjId" />

C#

protected void txtSearch_TextChanged(object sender, EventArgs e)
    {
        RadTextBox txt = sender as RadTextBox;
        RadComboBox list = (RadComboBox)((txt.NamingContainer).FindControl("rcbFieldName"));

        string option;
        if (list.SelectedValue == "TrxId")
            {
                option = " = ";
            }
            else
            {
                option = " LIKE ";
            }

        string filterExpression;

        if (option == " = ")
            {
                filterExpression = "(" + list.SelectedValue + option + txt.Text + ")";
            }
            else
            {
                filterExpression = "(" + list.SelectedValue + option + "'%" + txt.Text + "%'" + ")";
            }

        RadGrid1.MasterTableView.FilterExpression = filterExpression;
        RadGrid1.MasterTableView.Rebind();
        RadAjaxManager1.FocusControl(txtSearch.ID);
    }

    protected void btnShowAll_Click(object sender, EventArgs e)
    {
        RadGrid1.MasterTableView.FilterExpression = "";
        RadGrid1.MasterTableView.Rebind();
        txtSearch.Text = string.Empty;
        RadAjaxManager1.FocusControl(txtSearch.ID);
    }

提前致谢。

4

1 回答 1

2

好像忘记把 EnableLinqExpressions 设置为 False,导致 SQL Query 变形。无论如何,此方法仅适用于 gridboundcolumns,不适用于模板列。

于 2013-07-26T02:01:38.320 回答