RadGrid.AllowFilteringByColumn ="真"原因"预期')'"页面错误

时间:2014-06-30 18:57:41

标签: c# telerik radgrid

在设置AllowFilterByColumn =" true"之前,我的RadGrid在加载数据源时工作正常。在上面。现在我弹出一个页面错误"预期')'"。我尝试调试页面,但它并没有突破错误,因此我假设它在某个Telerik js文件中被引发。

根据Telerik论坛上用户的建议,我已尝试在页面生命周期的各个点检查MasterTableView上的FilterExpression值,但始终为空字符串。

知道这里发生了什么吗?

protected void Page_Init(object sender, EventArgs e)
{
    HydrateSession();
    _site = new Site(sessionData, sessionData.SiteID);

    string participantAlias = GetParticipantAliasSetting();
    CreateDismissedCheckbox(ParticipantGrid, participantAlias);
    lblHeader.InnerText = SetHeaderText(participantAlias);
    lbtnAddNew.Visible = !IsFormSearch;
    lbtnAddNew.Text = String.Format(lbtnAddNew.Text, participantAlias);
    lblAllParticipants.Text = String.Format(lblAllParticipants.Text, participantAlias);

    ParticipantGrid.Visible = false;
    ParticipantGrid.ItemDataBound += new GridItemEventHandler(ParticipantGrid_ItemDataBound);
    ParticipantGrid.NeedDataSource += new GridNeedDataSourceEventHandler(ParticipantGrid_NeedDataSource);
}

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        ParticipantGrid.Visible = true;
    }
}

protected void lbtnSearch_OnClick(object sender, EventArgs e)
{
    ParticipantGrid.MasterTableView.Columns.Clear();
    CreateStandardColumns(ParticipantGrid.MasterTableView);
    DataTable searchResults = ClientData.ViewEditParticipantSearch(sessionData, GetSearchString(), IncludeDismissed);
    CreateCustomColumns(ParticipantGrid.MasterTableView, searchResults);
    ParticipantGrid.DataSource = searchResults;
    ParticipantGrid.DataBind();
}

protected void ParticipantGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = (GridDataItem)e.Item;
        foreach (GridColumn column in item.OwnerTableView.Columns)
        {
            if (column.UniqueName == "SSN")
            {
                MaskSSNFields(item);
            }

            FormatDateColumn(item, column.UniqueName);
            EmboldenSearchText(item, column.UniqueName, GetSearchString());
        }
    }
    else if (e.Item is GridCommandItem)
    {
        //we need to make sure we reset the checkbox to whatever was last checked based off of viewstate
        GridCommandItem item = (GridCommandItem)e.Item;
        CheckBox ck = item.Controls[0].FindControl("ck1") as CheckBox;
        if (ck != null)
        {
            ck.Checked = IncludeDismissed;
        }
    }
}

protected void ParticipantGrid_NeedDataSource(object sender, EventArgs e)
{
    string eventTarget = Request.Params["__EVENTTARGET"];
    if (eventTarget != "lbtnSearch")
    {
        DataTable searchResults = ClientData.ViewEditParticipantSearch(sessionData, GetSearchString(), IncludeDismissed);
        ParticipantGrid.DataSource = searchResults;
    }
}

/// <summary>
/// To properly search all participants (i.e. empty search), we need to pass a specific text string
/// to the stored procedure that retrieves our data set.
/// </summary>
/// <returns></returns>
private string GetSearchString()
{
    return txtParticipantSearch.Text;
}

private void CreateDismissedCheckbox(RadGrid ParticipantGrid, string participantAlias)
{
    ParticipantGrid.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
    CommandItemTemplate cti = new CommandItemTemplate(string.Format("Include Dismissed {0}s", participantAlias), ParticipantGrid);
    cti.ck_OnClick += new CommandItemTemplate.OnCheckBoxClick(cti_ck_OnClick);
    ParticipantGrid.MasterTableView.CommandItemTemplate = cti;
}

/// <summary>
/// Alter the text displayed in "SSN"/"SIN" columns based on site settings.
/// </summary>
/// <param name="item">A row to be added to the RadGrid.</param>
private void MaskSSNFields(GridDataItem item)
{
    bool siteMasksSSN = GetSSNMaskSetting();
    if (siteMasksSSN)
    {
        string ssnAlias = GetSSNAliasSetting();
        if (item[ssnAlias].Text != "&nbsp;")
        {
            item[ssnAlias].Text = SSNMaskUtil.MaskSSN(item[ssnAlias].Text, siteMasksSSN, false, System.Globalization.CultureInfo.CurrentCulture);
        }
    }
}

/// <summary>
/// Some (hopefully few) customers will overwrite the standard "SSN"/"SIN"
/// text that shows up in the RadGrid's HeaderText when they return the 
/// standard demographic column with their quick search results. This method
/// retrieves that possible alternate text.
/// </summary>
/// <returns></returns>
public string GetSSNAliasSetting()
{
    var ssnAliasSetting =
        _site.GetSiteSettings(sessionData)
        .SingleOrDefault(ss => ss.SettingID == (int)Setting.Definition.SSNAlias);

    if (ssnAliasSetting == null)
    {
        if (sessionData.CountryCode != "CA")
        {
            return "SSN";
        }
        else
        {
            return "SIN";
        }
    }
    else
    {
        return ssnAliasSetting.Value.ToString();
    }
}

/// <summary>
/// Lets us know whether or not a customer wants to hide certain portion
/// of SSN/SIN numbers.
/// </summary>
/// <returns></returns>
public bool GetSSNMaskSetting()
{
    var ssnMaskSetting =
        _site.GetSiteSettings(sessionData)
        .SingleOrDefault(ss => ss.SettingID == (int)Setting.Definition.SSNMask);

    if (ssnMaskSetting == null || String.IsNullOrEmpty(ssnMaskSetting.Value.ToString()))
    {
        return false;
    }
    else
    {
        return bool.Parse(ssnMaskSetting.Value.ToString());
    }
}

/// <summary>
/// Some users may want to show a different text other than "Participant" on this page.
/// If so, we want to get that text so we can properly modify this page's HTML.
/// </summary>
/// <returns>Either the user-defined alias or "Participant".</returns>
public string GetParticipantAliasSetting()
{
    var participantAliasSetting =
        _site.GetSiteSettings(sessionData)
        .SingleOrDefault(ss => ss.SettingID == (int)Setting.Definition.AddNewParticipantScreenSetting);

    if (participantAliasSetting == null || String.IsNullOrEmpty(participantAliasSetting.Value.ToString()))
    {
        return "Participant";
    }
    else
    {
        return participantAliasSetting.Value.ToString();
    }
}

/// <summary>
/// Clicking the name of the client should link to the edit client page (varies depending
/// on whether or not the new Edit Participant page is visible).
/// </summary>
private string GetEditClientLink()
{
    if (IsFormSearch)
    {
        return "/SingleForm/ClientForms.aspx?CLID={0}";
    }
    else
    {
        if (RedirectToNewEditPage)
        {
            return "/Modules/Participants/AddEditClient.aspx?CLID={0}";
        }
        else
        {
            return "/EditClient.asp?CLID={0}";
        }
    }
}

/// <summary>
/// Remove timestamps from values in date columns and format dates according
/// to session location.
/// </summary>
/// <param name="item">A row to be added to the RadGrid.</param>
/// <param name="columnName">The column to be formatted.</param>
private void FormatDateColumn(GridDataItem item, string columnName)
{
    DateTime date;
    if (DateTime.TryParse(item[columnName].Text, out date))
    {
        string dateFormatString = GetDateStringFormat();
        item[columnName].Text = string.Format(dateFormatString, date);
    }
}

/// <summary>
/// Format text to be bold if it matches with the search term.
/// </summary>
/// <param name="item">A row of data in the RadGrid.</param>
/// <param name="columnName">Column Name string.</param>
/// <param name="searchTerm">What the user is searching for.</param>
private void EmboldenSearchText(GridDataItem item, string columnName, string searchTerm)
{
    if (ParticipantGrid.MasterTableView.GetColumnSafe(columnName) is GridHyperLinkColumn && columnName == "Name")
    {
        HyperLink nameLink = (HyperLink)item["Name"].Controls[0];
        string cellText = nameLink.Text;
        bool cellTextMatchesSearchTerm = cellText.ToLower().Contains(searchTerm.ToLower());
        if (!String.IsNullOrEmpty(searchTerm) && cellTextMatchesSearchTerm)
        {
            Match searchMatch = Regex.Match(cellText, searchTerm, RegexOptions.IgnoreCase);
            nameLink.Text = cellText.Replace(searchMatch.Value, String.Format("<b>{0}</b>", searchMatch.Value));
        }
    }
    else
    {
        string cellText = item[columnName].Text;
        bool cellTextMatchesSearchTerm = cellText.ToLower().Contains(searchTerm.ToLower());
        if (!String.IsNullOrEmpty(searchTerm) && cellTextMatchesSearchTerm)
        {
            Match searchMatch = Regex.Match(cellText, searchTerm, RegexOptions.IgnoreCase);
            item[columnName].Text = cellText.Replace(searchMatch.Value, String.Format("<b>{0}</b>", searchMatch.Value));
        }
    }
}

/// <summary>
/// Format a date in a RadGrid based on session location.
/// </summary>
/// <param name="countryCode">Location as a string.</param>
/// <returns>DataFormatString for RadGrid DateTime columns.</returns>
public string GetDateStringFormat()
{
    if (sessionData.CountryCode == "US")
    {
        return "{0:M/d/yyyy}";
    }
    else
    {
        return "{0:d/M/yyyy}";
    }
}

/// <summary>
/// Generate each of the standard form columns for the grid. Make sure the name of the standard columns matches the type of form
/// we are dealing with (Form => Collection or TouchPoint).
/// </summary>
/// <returns></returns>
private void CreateStandardColumns(GridTableView tableView)
{
    CreateColumn(tableView, "CLID", "CLID", false);
    CreateNameLinkColumn(tableView);
}

/// <summary>
/// Generate each of the custom demographic columns for the grid based on demographic quick search settings.
/// </summary>
/// <param name="tableView">Grid to which we're adding the columns.</param>
/// <param name="dataTable">The DataTable containing the search results.</param>
/// <returns></returns>
private void CreateCustomColumns(GridTableView tableView, DataTable dataTable)
{
    foreach (System.Data.DataColumn column in dataTable.Columns)
    {
        if (ColumnIsCustomDemographic(column.ColumnName.ToLower()))
        {
            if (column.ColumnName == "SSN" || column.ColumnName == "SIN")
            {
                CreateSSNColumn(tableView, column.ColumnName);
            }
            else
            {
                CreateColumn(tableView, column.ColumnName, column.ColumnName);
            }
        }
    }
}

/// <summary>
/// These columns are automatically returned by the stored procedure, but should not
/// be included in the RadGrid because they are not custom demographics.
/// </summary>
/// <param name="columnName">Name of the column.</param>
/// <returns>Whether or not this column contains custom demographic data.</returns>
private bool ColumnIsCustomDemographic(string columnName)
{
    return columnName != "name" &&
        columnName != "lname" &&
        columnName != "fname" &&
        columnName != "clid";
}

/// <summary>
/// This checks if there is an alias that the customer wants to display instead of "SSN" or "SIN".
/// If so, we will instead that alias when creating the column.
/// </summary>
/// <param name="tableView">Grid to which we're adding the column.</param>
/// <param name="columnName">The DataField/HeaderText of the column.</param>
private void CreateSSNColumn(GridTableView tableView, string columnName)
{
    string ssnAlias = GetSSNAliasSetting();
    if (!String.IsNullOrEmpty(ssnAlias))
    {
        CreateColumn(tableView, columnName, ssnAlias);
    }
    else
    {
        CreateColumn(tableView, columnName, columnName);
    }
}

/// <summary>
/// Generate a single column for the grid.
/// </summary>
/// <param name="dataField">The desired data field for the column.</param>
/// <param name="name">The desired display field of the column.</param>
/// <returns></returns>
private void CreateColumn(GridTableView tableView, string dataField, string name, bool isVisible = true)
{
    GridBoundColumn boundColumn = new GridBoundColumn();
    tableView.Columns.Add(boundColumn);
    boundColumn.DataField = dataField;
    boundColumn.UniqueName = name;
    boundColumn.HeaderText = name;
    boundColumn.Visible = isVisible;
    boundColumn.AutoPostBackOnFilter = true;
}

/// <summary>
/// Turn the "Name" column into a hyperlink that links to a page to edit the client.
/// </summary>
/// <param name="tableView"></param>
private void CreateNameLinkColumn(GridTableView tableView)
{
    GridHyperLinkColumn colName = new GridHyperLinkColumn();
    tableView.Columns.Add(colName);
    colName.DataTextField = "Name";
    colName.DataNavigateUrlFormatString = GetEditClientLink();
    colName.DataNavigateUrlFields = new string[] { "CLID" };
    colName.UniqueName = "Name";
    colName.HeaderText = "Name";
    colName.SortExpression = "Name";
}

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>View/Edit Participants</title>
<script type="text/javascript" src="/TelerikCSS/TelerikGridSupport.js"></script>
<style type="text/css">
    .rgMasterTable
    {
        border-collapse: collapse !important;
    }
    .rgHeader
    {
        font-weight: bold !important;
    }
    .pageStyle
    {
        margin-top: -2px;
        padding: 3px;
        border: 1px solid #828282;
    }
    .centerControl
    {
        margin-left: auto;
        margin-right: auto;
    }
    .OverrideBorder
    {
        border-style: none !important;
        border-width: 0px !important;
    }
    #lbtnSearch
    {
        font-size: 115%;
        padding: 1px 7px 1px 7px;
        margin: 2px;
    }
    #lbtnAddNew
    {
        font-size: 115%;
        padding: 1px 7px 1px 7px;
        margin: 2px;
    }
</style>
</head>
<body>
<form id="form1" runat="server">
<script type="text/javascript">
    document.onkeydown = function (e) {
        e = e || window.event;
        if (e.keyCode == 13) {
            //See if Search button is available
            var btnsearch = document.getElementById("lbtnSearch");
            if (btnsearch) btnsearch.click();
        }
    };
</script>

<telerik:RadScriptManager ID="ScriptManager1" runat="server" EnableTheming="True" AsyncPostBackTimeout="600">
    <Scripts>
        <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js">
        </asp:ScriptReference>
        <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js">
        </asp:ScriptReference>
        <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js">
        </asp:ScriptReference>
    </Scripts>
</telerik:RadScriptManager>
<div>
    <table class="OuterTable" style="width:100%;">
        <tr>
            <td>
                <table class="InnerTable" style="width:100%;">
                    <tr>
                        <td class="MainHeaderRow" id="lblHeader" runat="server" colspan="2">
                        </td>
                    </tr>
                    <tr align="center">
                        <td align="left" colspan="2">
                            <span id="searchForm">
                                <asp:TextBox ID="txtParticipantSearch" runat="server" Width="22%"></asp:TextBox>
                                <asp:LinkButton ID="lbtnSearch" runat="server" CssClass="SecondaryButton" Text="Search" OnClick="lbtnSearch_OnClick"/>
                                <asp:LinkButton ID="lbtnAddNew" runat="server" CssClass="SecondaryButton" Text="Add New {0}" OnClick="lbtnAddNew_OnClick" />
                                <br />
                                <asp:Label ID="lblAllParticipants" runat="server">To see <b>all {0}s</b> leave the box empty. </asp:Label>
                                <br />
                                <br />
                            </span>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <telerik:RadAjaxManager ID="RadAjaxManager" runat="server" >
                                <AjaxSettings>
                                    <telerik:AjaxSetting AjaxControlID="lbtnSearch">
                                        <UpdatedControls>
                                            <telerik:AjaxUpdatedControl ControlID="searchResults" LoadingPanelID="RadAjaxLoadingPanel1" />
                                        </UpdatedControls>
                                    </telerik:AjaxSetting>
                                    <telerik:AjaxSetting AjaxControlID="searchResults">
                                        <UpdatedControls>
                                            <telerik:AjaxUpdatedControl ControlID="searchResults" LoadingPanelID="RadAjaxLoadingPanel1" />
                                        </UpdatedControls>
                                    </telerik:AjaxSetting>
                                </AjaxSettings>
                            </telerik:RadAjaxManager>
                            <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" EnableEmbeddedSkins="false">
                                <div>
                                    <img src="/images/ajax-loader_transparent.gif" alt="Loading..."/>
                                </div>
                            </telerik:RadAjaxLoadingPanel>
                            <asp:UpdatePanel ID="searchResults" runat="server" UpdateMode="Conditional" cssclass="pageStyle">
                                <ContentTemplate>
                                    <telerik:RadGrid ID="ParticipantGrid" runat="server" CellSpacing="0" Width="100%"
                                    AutoGenerateColumns="false" GridLines="None" EnableEmbeddedSkins="False"
                                    AllowSorting="true" ViewStateMode="Enabled" PagerStyle-AlwaysVisible="True"
                                    EnableLinqExpressions="false" AllowPaging="true" PageSize="250" AllowFilteringByColumn="true">
                                </telerik:RadGrid>
                                </ContentTemplate>
                            </asp:UpdatePanel>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</div>
</form>

1 个答案:

答案 0 :(得分:1)

想出来。显然Telerik无法处理列名中的撇号。我不得不删除它们以使此消息消失。

更新了CreateColumn方法:

/// <summary>
/// Generate a single column for the grid.
/// </summary>
/// <param name="dataField">The desired data field for the column.</param>
/// <param name="name">The desired display field of the column.</param>
/// <returns></returns>
private void CreateColumn(GridTableView tableView, string dataField, string name, bool isVisible = true)
{
    GridBoundColumn boundColumn = new GridBoundColumn();
    tableView.Columns.Add(boundColumn);
    boundColumn.DataField = dataField;
    boundColumn.UniqueName = name.Replace("'", "");
    boundColumn.HeaderText = Server.HtmlEncode(name);
    boundColumn.Visible = isVisible;
}