我的命令参数返回";每一次
本文关键字:一次 我的 参数 返回 quot 命令 | 更新日期: 2023-09-27 18:15:47
每当我点击其中一个删除链接按钮时,e.c remand参数总是保留"的值。我在某个地方读到你不能在命令参数中使用<%#,但我已经看到了几个例子,这是有效的。有什么建议吗?
在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") %>' />
|
<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可以解决这个问题。
在寻找了一点之后,我发现了一些帖子,说你不能在服务器标签内使用<%# ... %>
语法,这显然是错误的,因为你正在使用它的Text
属性,我敢肯定我以前做过同样的事情。无论如何,您可以在DataList
上使用ItemDataBound
事件:
<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") %>' />
|
<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
(并将CLASS
更改为CssClass
),这需要在我们的代码中找到控件:
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
}
}
}
您只需要DataKeyField唯一标识您的行,以便执行Delete操作。