2013-03-27 85 views
1

我有一個網格,它會顯示這是工作的用戶。您可以單擊編輯(圖像按鈕)或刪除(圖像按鈕)來執行操作。但是,如果我點擊對這些我得到一個輸入字符串的不正確的格式asp.net C#

輸入字符串的不正確的格式

這是不應該的 - 我看不到我的錯誤。問題是什麼?

這裏是我的網格:

<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); 
    } 
} 

我在User類

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(); 
} 
+1

你肯定'e.Item.Cells [2] .Text'是數字(整數) ?第二列是姓氏 – Steve 2013-03-27 11:11:48

回答

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); 
    } 
} 
+0

感謝您的幫助完美的解決方案和它的作品!我希望你有更多的選票,因爲你的解決方案正在工作! – Mingebag 2013-03-27 12:21:28

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); 
    } 
} 
+0

我仍然有一個輸入字符串的格式不正確。 – Mingebag 2013-03-27 11:17:21

+2

@Mingebag:我建議使用調試器。在'rgGrid_ItemCommand'中設置斷點並查看'e.Item.Cells [0] .Text'的值。我不熟悉'Telerik的-GridBoundColumns',也許你需要使用'FindControl',而不是'Cell.Text'或使用['GetDataKeyValue'](http://www.telerik.com/help/aspnet-ajax /grid-griddataitem-getdatakeyvalue.html)。 – 2013-03-27 11:19:15