在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 (将#修改为@)