0

每当我单击删除链接按钮之一时,e.CommandArgument 始终保留“”的值。我在某个地方读到您不能在命令参数中使用 <%#,但我已经看到了几个例子,这些例子都有效。有什么建议么?

在 ASPX 中

 <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1">
        <ItemTemplate>
            <asp:Label ID="dataLabel" runat="server" Text='<%# Eval("data") %>' />
            <br />
            <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' />
            &nbsp;|&nbsp;
            <asp:Label ID="Column1Label" runat="server" Text='<%# Eval("Column1") %>' />
            <br />
            <asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
            <LoggedInTemplate>
                <asp:LinkButton CLASS="DeleteButton" runat="server" OnCommand="Delete" CommandArgument='<%# Eval("id") %>' ViewStateMode="Disabled">Delete</asp:LinkButton>
            </LoggedInTemplate>
            </asp:LoginView>
        </ItemTemplate>
    </asp:DataList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 

            SelectCommand="SELECT [id],[name], [data], convert(varchar, [date], 101) FROM [Announcements] ORDER BY [date] DESC">
    </asp:SqlDataSource>

在 ASPX.CS 中

using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString))
        {
            SqlCommand command = conn.CreateCommand();
            command.CommandText = "DELETE FROM Annoucnements WHERE id=@id";
            command.CommandType = System.Data.CommandType.Text;

            command.Parameters.Add(new SqlParameter("id", e.CommandArgument));

            conn.Open();
            command.ExecuteNonQuery();
        }

正如 Royi Namir 所建议的,将 EnableViewState 更改为 true 可以解决问题。

4

2 回答 2

1

环顾四周后,我发现一些帖子说您不能<%# ... %>在服务器标签中使用语法,这显然是错误的,因为您将它用于Text属性,我相信我已经做到了这一点以前一样。无论如何,您可以ItemDataBound在您的DataList

 <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="DataList1_ItemDataBound">
        <ItemTemplate>
            <asp:Label ID="dataLabel" runat="server" Text='<%# Eval("data") %>' />
            <br />
            <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' />
            &nbsp;|&nbsp;
            <asp:Label ID="Column1Label" runat="server" Text='<%# Eval("Column1") %>' />
            <br />
            <asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
            <LoggedInTemplate>
                <asp:LinkButton CssClass="DeleteButton" runat="server" ID="lbDelete" OnCommand="Delete" CommandArgument='<%# Eval("id") %>' ViewStateMode="Disabled">Delete</asp:LinkButton>
            </LoggedInTemplate>
            </asp:LoginView>
        </ItemTemplate>
    </asp:DataList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 

            SelectCommand="SELECT [id],[name], [data], convert(varchar, [date], 101) FROM [Announcements] ORDER BY [date] DESC">
    </asp:SqlDataSource>

请注意,我已添加ID="lbDelete"到您的LinkButton(也为您更改CLASSCssClass),这是在我们后面的代码中找到控件所需的:

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) // this is to ensure that we're looking in an item that will contain controls, and not the header or footer
    {
        LoginView headLoginView = (LoginView)e.Item.FindControl("HeadLoginView") // FindControl is not recursive so we need a reference to a control we can look in to find the button
        LinkButton lbDelete = (LinkButton)headLoginView.FindControl("lbDelete");

        if (lbDelete != null) // check for a null otherwise this code will fail if the user is not logged in, because the controls inside the LoggedInTemplate will not be rendered
        {
            lbDelete.CommandArgument = DataBinder.Eval(e.Item.DataItem, "id").ToString(); // set the CommandArgument on the button using DataBinder.Eval
        }
    }
}
于 2013-05-28T15:05:16.123 回答
0

您只需要DataKeyField来唯一标识您的行即可执行删除操作。

于 2013-05-28T15:08:54.627 回答