1

我有一个网格,它正在向用户显示这是有效的。您可以单击编辑(图像按钮)或删除(图像按钮)来执行操作。但是如果我点击这些,我会得到一个

输入字符串的格式不正确

这不应该发生——我没有看到我的错误。问题是什么?

这是我的网格:

<asp:EntityDataSource ID="EntityDataSource1" runat="server"
    ConnectionString="name=HolidayTrackerEntities"
    DefaultContainerName="HolidayTrackerEntities" EnableFlattening="False"
    EntitySetName="HtUsers">
</asp:EntityDataSource>
<telerik:RadGrid ID="rgGrid" runat="server" DataSourceID="EntityDataSource1"
    AllowSorting="True" AllowPaging="True" PageSize="20" 
    AllowFilteringByColumn="True" ShowStatusBar="True" Width="100%" 
    CellSpacing="0" GridLines="None" OnItemCommand="rgGrid_ItemCommand">
    <MasterTableView AutoGenerateColumns="False" DataKeyNames="UserId">
        <NoRecordsTemplate>
            Can't find Users to display
        </NoRecordsTemplate>
        <Columns>
            <telerik:GridBoundColumn DataField="UserId" DataType="System.Int32"
                FilterControlAltText="Filter UserId column" HeaderText="UserId" 
                ReadOnly="True" SortExpression="UserId" UniqueName="UserId"
                Visible="false">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="FirstName"
                FilterControlAltText="Filter FirstName column"
                HeaderText="FirstName" ItemStyle-Width="60px"
                SortExpression="FirstName" UniqueName="FirstName">
                <ItemStyle Width="60px" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="LastName"
                FilterControlAltText="Filter LastName column"
                HeaderText="LastName" ItemStyle-Width="60px" 
                SortExpression="LastName" UniqueName="LastName">
                <ItemStyle Width="60px" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="UserName"
                FilterControlAltText="Filter UserName column"
                HeaderText="UserName" ItemStyle-Width="60px"
                SortExpression="UserName" UniqueName="UserName">
                <ItemStyle Width="60px" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Email"
                FilterControlAltText="Filter Email column"
                HeaderText="Email" SortExpression="Email" UniqueName="Email"
                Visible="False">
            </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn UniqueName="DeleteColumn" 
                ItemStyle-HorizontalAlign="Right" ItemStyle-Width="20px"
                AllowFiltering="false">
                <ItemTemplate>
                    <telerik:RadButton ID="btnEdit" CommandName="Edit" runat="server" Width="20px" ToolTip="View Details" Height="20px">
                        <Image ImageUrl="~/Resources/Images/Grid/edit-app.png" IsBackgroundImage="true" />
                    </telerik:RadButton>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn UniqueName="Delete"
                ItemStyle-HorizontalAlign="Right" ItemStyle-Width="20px"
                AllowFiltering="false" FilterImageUrl="../Image/Filter.gif">
                <ItemTemplate>
                    <telerik:RadButton ID="btnDelete" CommandName="Delete" runat="server" Width="20px" ToolTip="Delte User" Height="20px">
                        <Image ImageUrl="~/Resources/Images/Grid/delete-app.png" IsBackgroundImage="true" />
                    </telerik:RadButton>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

正如我所说,没有什么特别的。以下是后面代码中的按钮:

protected void rgGrid_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName.Equals("Edit"))
    {
        int id = int.Parse(e.Item.Cells[2].Text);
        Response.Redirect("~/Administrator/UserEditPanel.aspx?userId=" + id);
    }
    else if (e.CommandName.Equals("Delete"))
    {
        int id = int.Parse(e.Item.Cells[2].Text);
        HtUser.DeleteUserById(id);
    }
}

我在用户类中的删除功能

public static void DeleteUserById(int id)
{
    HolidayTrackerEntities ctx = HtEntityFactory.Context;
    HtUser userToDelete = ctx.HtUsers.Where(user => user.UserId == id).FirstOrDefault();

    //Get all the userroles where user is containing
    IEnumerable<HtUserRole> roles = ctx.HtUserRoles.Where(ur => ur.HtUsers.Where(x => x.UserId == id).Any());
    foreach (HtUserRole role in roles)
    {
        //Remove reference from htuserroles table
        role.HtUsers.Remove(userToDelete);
    }

    ctx.HtUsers.DeleteObject(userToDelete);
    ctx.SaveChanges();
}
4

2 回答 2

2

e.Item.Cells[2]是带有 的单元格DataField="LastName"。所以我假设这不是一个有效的整数。您将 id 存储在第一列中,因此您需要:

protected void rgGrid_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName.Equals("Edit"))
    {
        int id = int.Parse(e.Item.Cells[0].Text);
        Response.Redirect("~/Administrator/UserEditPanel.aspx?userId=" + id);
    }
    else if (e.CommandName.Equals("Delete"))
    {
        int id = int.Parse(e.Item.Cells[0].Text);
        HtUser.DeleteUserById(id);
    }
}
于 2013-03-27T11:13:07.463 回答
2

与其解析列文本,更好的方法是将 分配UserId给编辑和删除按钮的CommandArgument属性,然后将其解析为rgGrid_ItemCommand. 分配UserId给按钮:

<telerik:GridTemplateColumn UniqueName="DeleteColumn" ItemStyle-HorizontalAlign="Right" ItemStyle-Width="20px" AllowFiltering="false">
    <ItemTemplate>
        <telerik:RadButton ID="btnEdit" CommandName="Edit" runat="server" Width="20px" ToolTip="View Details"
       Height="20px" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "UserId")%>'>
            <Image ImageUrl="~/Resources/Images/Grid/edit-app.png" IsBackgroundImage="true" />
        </telerik:RadButton>
    </ItemTemplate>
</telerik:GridTemplateColumn>

然后在您的代码隐藏中:

protected void rgGrid_ItemCommand(object sender, GridCommandEventArgs e)
{
    var userId = int.Parse(e.CommandArgument.ToString());

    if (e.CommandName.Equals("Edit"))
    {
        Response.Redirect("~/Administrator/UserEditPanel.aspx?userId=" + userId);
    }
    else if (e.CommandName.Equals("Delete"))
    {
        HtUser.DeleteUserById(userId);
    }
}
于 2013-03-27T11:52:29.577 回答