System.FormatException'发生在mscorlib.dll但未在用户代码

时间:2017-09-01 18:55:58

标签: c# asp.net

我的Site.Master页面上有以下代码:

<ul class="dropdown-menu dropdown-menu-default">
    <li>
        <a href="<%=GetProfileLink()%>">
            <i class="icon-user"></i>My Profile
        </a>
    </li>
    <li class="divider"></li>
    <li><a href="/Login.aspx?Logout=Y"><i class="icon-key"></i>Log Out</a></li>
</ul>

代码隐藏文件中的公共方法如下:

public string GetProfileLink()
{
    string UserRoleID = Session["UserRoleID"].ToString();
    string ProfileLink = "#";
    if (UserRoleID == "2")
    {
        ProfileLink = "/User/AdminProfile.aspx";
    }
    else if (UserRoleID == "3")
    {
        ProfileLink = "/User/BranchProfile.aspx";
    }
    return ProfileLink;
}

此代码在3年内没有问题,现在,在创建具有DevExpress网格的新Web表单时,此错误正在呈现。我在GetProfileLink()中添加了断点,并且所有值都存在,第一个if语句为true,一旦我单击继续,Visual Studio中会显示以下错误:

An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code

Additional information: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

我不明白的是,代码在每个页面上都能正常运行,并显示正确的配置文件链接。当我浏览到新表单时,为了测试级联控件,我发现该方法返回正确的URL作为字符串(来自断点),但随后Visual Studio挂起几秒钟并抛出异常。奇怪的是ASP页面调用了类似的方法几行并没有相同的问题。我认为ASP标签不适用于此,那么解决方案是什么?

enter image description here

堆栈跟踪

[InvalidOperationException: Stack empty.]
   System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) +52
   System.Collections.Generic.Stack`1.Pop() +6884317
   Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.SelectionMappingRenderTraceListener.EndRendering(TextWriter writer, Object renderedObject) +84
   System.Web.UI.RenderTraceListenerList.EndRendering(TextWriter writer, Object renderedObject) +66
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +170
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1405

我怀疑冲突来自ASP文件中的代码:

    <div class="col-input">
        <dx:ASPxGridLookup ID="gl_UserType" runat="server" CssClass="form-control" Width="100%" AutoGenerateColumns="False" DataSourceID="ds_UserType" KeyFieldName="UTID" TextFormatString="{2}" ReadOnly="True">
            <GridViewProperties>
                <SettingsBehavior AllowFocusedRow="True" AllowSelectSingleRowOnly="True"></SettingsBehavior>
            </GridViewProperties>
            <Columns>
                <dx:GridViewDataTextColumn FieldName="UTID" ReadOnly="True" VisibleIndex="2" Visible="False">
                    <EditFormSettings Visible="False" />
                </dx:GridViewDataTextColumn>
                <dx:GridViewDataTextColumn FieldName="UserType" VisibleIndex="1" Caption="User Type">
                </dx:GridViewDataTextColumn>
            </Columns>
            <DropDownButton Enabled="False">
            </DropDownButton>
            <FocusedStyle CssClass="form-control-focus">
            </FocusedStyle>
        </dx:ASPxGridLookup>
        <asp:SqlDataSource ID="ds_UserType" runat="server" ConnectionString="<%$ ConnectionStrings:Portal_ConnectionString %>" SelectCommand="SELECT [UserTypeID] as UTID, UserType FROM [tbl_UserType]"></asp:SqlDataSource>
    </div>

代码隐藏文件中的相应代码如下:

protected void gl_EntityTypes_Init(object sender, EventArgs e)
{
    ASPxGridLookup gridLookup = sender as ASPxGridLookup;
    gridLookup.GridView.CustomCallback += new ASPxGridViewCustomCallbackEventHandler(gridView_CustomCallback);
}

public void gridView_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
{
    if (e.Parameters != "ValueChanged") return;

    ASPxGridView grid = sender as ASPxGridView;
    var selectedValues = gl_UserType.GridView.GetSelectedFieldValues(gl_UserType.KeyFieldName);
    if (selectedValues.Count == 0)
        selectedValues.Add(-1);
    CriteriaOperator selectionCriteria = new InOperator(gl_UserType.KeyFieldName, selectedValues);
}

0 个答案:

没有答案