如何解决在asp.net中更新后,Detailsview编辑模式未更改
我在这里尝试了很多方法。但他们都不在工作。我有一个gridview和detailsview。我使用detailsview数据与sqldatasource绑定,并在后面的代码中更新事件。但是,更新后,编辑模式不会更改为只读。通常,编辑模式在客户端运行。但是当我使用itemupdating时,不会触发modechange。我该如何解决这个问题或我做错了什么?你能帮我吗?
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" AutoGenerateRows="False" DataKeyNames="Id" DataSourceID="purchasedDetails" Onitemcommand="DetailsView1_itemcommand" OnItemUpdated="DetailsView1_ItemUpdated" OnItemUpdating="DetailsView1_ItemUpdating" OnModeChanging="DetailsView1_ModeChanging">
<Fields>
<asp:BoundField datafield="Id" HeaderText="Id" ReadOnly="True" InsertVisible="False" SortExpression="Id"></asp:BoundField>
<asp:BoundField datafield="name" HeaderText="name" SortExpression="name" ReadOnly="True"></asp:BoundField>
<asp:BoundField datafield="surname" HeaderText="surname" SortExpression="surname" ReadOnly="True"></asp:BoundField>
<asp:BoundField datafield="package_name" HeaderText="package_name" SortExpression="package_name" ReadOnly="True"></asp:BoundField>
<asp:BoundField datafield="package_type" HeaderText="package_type" SortExpression="package_type" ReadOnly="True"></asp:BoundField>
<asp:BoundField datafield="package_period" HeaderText="package_period" SortExpression="package_period" ReadOnly="True"></asp:BoundField>
<asp:BoundField datafield="package_time" HeaderText="package_time" SortExpression="package_time" ReadOnly="True"></asp:BoundField>
<asp:BoundField datafield="package_fee" HeaderText="package_fee" SortExpression="package_fee" ReadOnly="True"></asp:BoundField>
<asp:BoundField datafield="package_description" HeaderText="package_description" SortExpression="package_description" ReadOnly="True"></asp:BoundField>
<asp:TemplateField HeaderText="date" SortExpression="date">
<EditItemTemplate>
<asp:Label runat="server" Text='<%# DateTime.Today.ToString("yyyy-MM-dd") %>' ID="date"></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox runat="server" Text='<%# DateTime.Today.ToString("yyyy-MM-dd") %>' ID="date"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("date","{0:yyyy-MM-dd}") %>' ID="dateLbl"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="status" SortExpression="status">
<EditItemTemplate>
<asp:CheckBox ID="statusDetails" runat="server" Enabled="true" Checked='<%# (Boolean) Eval("status").Equals("Active") ? true : false %>' />
</EditItemTemplate>
<InsertItemTemplate>
<asp:CheckBox ID="statusDetails" runat="server" Enabled="true" Checked='<%# (Boolean) Eval("status").Equals("Active") ? true : false %>' />
</InsertItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="statusLabel" runat="server" Enabled="false" Checked='<%# (Boolean) Eval("status").Equals("Active") ? true : false %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True"></asp:CommandField>
</Fields>
</asp:DetailsView>
<asp:sqlDataSource runat="server" ID="purchasedDetails"
ConnectionString='<%$ ConnectionStrings:btcAdminConnectionString %>'
SelectCommand="SELECT Purchased_List.Id,UserRegistery.name,UserRegistery.surname,Purchased_List.package_name,Purchased_List.package_type,Purchased_List.package_period,Purchased_List.package_time,Purchased_List.package_fee,Purchased_List.package_description,Purchased_List.date,Purchased_List.status FROM Purchased_List INNER JOIN UserRegistery ON Purchased_List.user_ID = UserRegistery.Id WHERE (Purchased_List.Id = @Id)"
UpdateCommand="UPDATE Purchased_List SET status = @status FROM Purchased_List INNER JOIN UserRegistery ON Purchased_List.user_ID = UserRegistery.Id WHERE (Purchased_List.Id = @Id)">
<SelectParameters>
<asp:ControlParameter ControlID="Gridview1" PropertyName="SelectedValue" Name="Id"></asp:ControlParameter>
</SelectParameters>
<SelectParameters>
</SelectParameters>
<UpdateParameters>
</UpdateParameters>
</asp:sqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>
protected void DetailsView1_ModeChanging(object sender,DetailsviewmodeEventArgs e)
{
// Activates the edit,update,delete buttons
DetailsView1.ChangeMode(e.NewMode);
DetailsView1.DataBind();
}
protected void DetailsView1_ItemUpdating(object sender,DetailsViewUpdateEventArgs e)
{
DetailsViewRow row = DetailsView1.Rows[0];
string status = (row.FindControl("statusDetails") as CheckBox).Checked ? "Active" : "Passive";
string date = (row.FindControl("date") as Label).Text;
string update = "UPDATE Purchased_List SET status = '" + status + "',date= '" + date + "' FROM Purchased_List INNER JOIN UserRegistery ON Purchased_List.user_ID = UserRegistery.Id WHERE (Purchased_List.Id = '" + Gridview1.SelectedValue + "')";
db.cmd(update);
}
protected void DetailsView1_ItemUpdated(object sender,DetailsViewUpdatedEventArgs e)
{
DetailsView1.ChangeMode(Detailsviewmode.ReadOnly);
Gridview1.DataBind();
DetailsView1.DataBind();
//e.KeepInEditMode = false;
}
我也尝试了itemcommand,但是没有用。我如何在代码隐藏中触发更改模式
protected void DetailsView1_itemcommand(object sender,DetailsViewCommandEventArgs e)
{
if (e.CommandName.Equals("Update"))
{
this.DetailsView1.DefaultMode = Detailsviewmode.ReadOnly;
this.DetailsView1.DataBind();
}
if (e.CommandName.Equals("Cancel"))
{
DetailsView1.ChangeMode(Detailsviewmode.ReadOnly);
this.DetailsView1.DefaultMode = Detailsviewmode.ReadOnly;
this.DetailsView1.DataBind();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。