datalist中的DeleteCommand,无法将int转换为目标类型字符串

时间:2012-03-13 18:26:57

标签: c# asp.net sql-server

我有这个代码,我在这个网站上找到了

  

http://msdn.microsoft.com/en-us/library/efx1fwb6.aspx

<asp:DataList runat="server" ID="Datalist1" DataSourceID="SqlDataSource1" DataKeyField="BrandID" OnDeleteCommand="DataList1_DeleteCommand">
    <ItemTemplate>
        <asp:Label runat="server" ID="BrandNameLabel" Text='<%#Eval("BrandName") %>' />
        <br/>
        <asp:Button runat="server" ID="Delete" Text="Slet" CommandName="Delete" />
    </ItemTemplate>
</asp:DataList>

protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
    int id = (int) Datalist1.DataKeys[e.Item.ItemIndex];
    SqlDataSource1.DeleteParameters["BrandID"].DefaultValue = id;
    SqlDataSource1.Delete();
}

我想我已经把它变成了网站,但我得到的错误名为“无法将源类型'int'转换为目标类型'字符串'”

我的BrandID是和int。 你可以帮助我,并告诉我如何转换它。

1 个答案:

答案 0 :(得分:2)

DefaultValue是一个字符串。你可以这样做:

SqlDataSource1.DeleteParameters["BrandID"].DefaultValue =
    Datalist1.DataKeys[e.Item.ItemIndex].ToString();