我的 GridView 有一些奇怪的问题。基本上,我试图在表上运行删除命令。gridview 绑定了 3 个字段,ItemId、ItemSummary 和 ItemDate。
现在我记得,在模式更改期间(例如,在 Select 和 Edit 之间),这些字段使用语法 @+FieldName 自动传递(即 ItemId 作为 @ItemId 传递)。
所以记住这一点,我尝试了 delete 语句,但它给出了一个错误,说参数没有传递。所以我尝试了另一条路线,以编程方式提取 ItemId 并在运行时将其作为参数插入。
我尝试从绑定到控件的 DataItem 中读取,但它一直返回 null。
经过更多调试,我发现当数据最初绑定到控件时,它正确地创建了一个 DataItem,但是一旦更改了模式,DataItem 现在就为空。
我一直在努力解决这个问题,但它只是行不通。这是我的代码:
<asp:DetailsView FieldHeaderStyle-CssClass="bold" CssClass="marginLeftRightBottom10px center"
AutoGenerateDeleteButton="true" AutoGenerateEditButton="true" GridLines="Both"
ID="dvIndividualUpdate" AutoGenerateInsertButton="true" runat="server" AutoGenerateRows="False"
DataSourceID="sqldsSingleUpdate" OnDataBound="dvIndividualUpdate_DataBound">
<Fields>
<asp:TemplateField>
<HeaderTemplate>
Update Id:
</HeaderTemplate>
<ItemTemplate>
<asp:Label Text='<%# Eval("updateId") %>' ID="lblUpdateId" runat="server"></asp:Label>
</ItemTemplate>
<InsertItemTemplate>
<asp:Label Text='Auto Generated' ID="lblUpdateIdInsert" runat="server"></asp:Label>
</InsertItemTemplate>
<EditItemTemplate>
<asp:Label Text='Auto Generated' ID="lblUpdateIdEdit" runat="server"></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Update Summary:
</HeaderTemplate>
<ItemTemplate>
<asp:Label Text='<%# Eval("updateSummary") %>' ID="lblUpdateSummary" runat="server"></asp:Label>
</ItemTemplate>
<InsertItemTemplate>
<asp:TextBox CssClass="tbUpdateSummaryInsert" TextMode="MultiLine" Text='<%# Bind("updateSummary") %>'
ID="tbUpdateSummary" runat="server"></asp:TextBox>
</InsertItemTemplate>
<EditItemTemplate>
<asp:TextBox CssClass="tbUpdateSummaryEdit" TextMode="MultiLine" Text='<%# Bind("updateSummary") %>'
ID="tbUpdateSummary" runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Update Date:
</HeaderTemplate>
<ItemTemplate>
<asp:Label Text='<%# Eval("dateOfUpdate") %>' ID="lblDateOfUpdateInsert" runat="server"></asp:Label>
</ItemTemplate>
<InsertItemTemplate>
<asp:Label Text='' ID="lblEditDateOfUpdate" runat="server"></asp:Label>
</InsertItemTemplate>
<EditItemTemplate>
<asp:Label Text='<%# Eval("dateOfUpdate") %>' ID="lblDateOfUpdateEdit" runat="server"></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
<asp:Label ID="lblUpdateErrors" runat="server" Text="" CssClass="block colorRed marginBottom10px center"></asp:Label>
<asp:SqlDataSource DataSourceMode="DataSet" ID="sqldsSingleUpdate" runat="server"
ConnectionString="<%$ ConnectionStrings:myDbConnection%>" SelectCommandType="StoredProcedure"
SelectCommand="dbo.getUpdate" InsertCommand="dbo.createUpdate" InsertCommandType="StoredProcedure"
OnInserted="sqldsSingleUpdate_Inserted" DeleteCommand="dbo.deleteUpdate" DeleteCommandType="StoredProcedure"
OnDeleted="sqldsSingleUpdate_Deleted" OnDeleting="sqldsSingleUpdate_Deleting">
<DeleteParameters>
<asp:Parameter Name="updateID" />
</DeleteParameters>
<SelectParameters>
<asp:ControlParameter Name="updateId" ControlID="gvUpdates" PropertyName="SelectedDataKey.Value" />
</SelectParameters>
<InsertParameters>
<asp:ControlParameter DbType="Date" ControlID="dvIndividualUpdate$lblEditDateOfUpdate"
Name="dateOfUpdate" PropertyName="Text" />
</InsertParameters>
</asp:SqlDataSource>
还有我相关的代码隐藏:
protected void dvIndividualUpdate_DataBound(object sender, EventArgs e)
{
if (dvIndividualUpdate.CurrentMode == DetailsViewMode.Insert)
{
Label lbl = dvIndividualUpdate.FindControl("lblEditDateOfUpdate") as Label;
lbl.Text = DateTime.Today.ToString("dd-MM-yyyy");
}
}
protected void sqldsSingleUpdate_Inserted(object sender, SqlDataSourceStatusEventArgs e)
{
if (e.Exception != null)
{
Exception ex = e.Exception;
lblUpdateErrors.Text = "There was a problem creating the update.";
ErrorSignal.FromCurrentContext().Raise(ex);
e.ExceptionHandled = true;
return;
}
else
{
lblUpdateErrors.Text = "Update created successfully.";
gvUpdates.DataBind();
}
}
protected void sqldsSingleUpdate_Deleted(object sender, SqlDataSourceStatusEventArgs e)
{
if (e.Exception != null)
{
Exception ex = e.Exception;
lblUpdateErrors.Text = "There was a problem deleting the update.";
ErrorSignal.FromCurrentContext().Raise(ex);
e.ExceptionHandled = true;
return;
}
else
{
lblUpdateErrors.Text = "Update deleted successfully.";
gvUpdates.DataBind();
}
}
非常感谢