如何将GridView中行的id链接到delete命令以删除行?
这是我的代码:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" DataSourceID="SqlDataSource1"
AutoGenerateColumns="False">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="SDate" HeaderText="SDate" SortExpression="SDate" />
<asp:BoundField DataField="STime" HeaderText="STime" SortExpression="STime" />
<asp:BoundField DataField="SPrice" HeaderText="SPrice"
SortExpression="SPrice" />
<asp:BoundField DataField="SDuration" HeaderText="SDuration"
SortExpression="SDuration" />
<asp:BoundField DataField="SNoPlaces" HeaderText="SNoPlaces"
SortExpression="SNoPlaces" />
<asp:BoundField DataField="ScheduleId" HeaderText="ScheduleId"
InsertVisible="False" ReadOnly="True" SortExpression="ScheduleId" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ChinatowndbConnString %>"
DeleteCommand="DELETE FROM Schedule WHERE (ScheduleId = @ScheduleId)"
SelectCommand="SELECT SDate, STime, SPrice, SDuration, SNoPlaces, ScheduleId FROM Schedule">
<DeleteParameters>
<asp:Parameter Name="ScheduleId" />
</DeleteParameters>
</asp:SqlDataSource>
我正在使用Visual Studio 2010。
答案 0 :(得分:3)
您忘记在GridView控件上设置DataKeyNames property。
将GridView更改为:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" DataSourceID="SqlDataSource1"
AutoGenerateColumns="False"
DataKeyNames="ScheduleId" >
答案 1 :(得分:0)
在Gridview属性中链接您的id
DataKeyNames="your_id"
您还可以定义多个ids
,如下所示:
DataKeyNames="your_id1, your_id2"
您可以访问以下值:
protected void GridView1_Deleting(object sender, ObjectDataSourceMethodEventArgs)
{
//e.Command.Parameters["your_id1"].Value;
}