如何在detailsview中更新数据

时间:2015-01-16 02:54:18

标签: asp.net detailsview

我有一个名为“详细信息”的页面,现在我无法更新详细信息视图中的数据,任何可以帮助我的人!

我第一次使用detailsview!感谢您的帮助!

details.aspx代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Details.aspx.cs" Inherits="Details" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!DOCTYPE html>
<%--submit_date,scheduled_date,finished_date,customer,city,CE_involved,TA_involved,working_hours,total_labors,reason,comments,proposer,status--%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .hidden{display:none}
    </style>
    <link rel="stylesheet" href="/../code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script>
      $(function () {
          $("#datepicker").datepicker({ minDate: -20, maxDate: "+1M +10D" });
      });
  </script>
</head>
<body>
    <form id="form1" runat="server">
    <div align="left">
        <asp:DetailsView ID="DetailsView1" runat="server" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataSourceID="SqlDataSource1" GridLines="Horizontal" Height="270px" Width="480px" ForeColor="Black" AutoGenerateRows="False" OnDataBound="DetailsView1_DataBound" OnItemUpdating="DetailsView1_ItemUpdating">
            <EditRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
            <Fields>
                <asp:BoundField DataField="Service_ID" HeaderText="Service_ID">
                <HeaderStyle CssClass="hidden" />
                <ItemStyle CssClass="hidden" />
                </asp:BoundField>
                <asp:BoundField DataField="submit_date" DataFormatString="{0:yyyy-MM-dd}" HeaderText="submit_date" ReadOnly="True" />
                <asp:BoundField DataField="scheduled_date" DataFormatString="{0:yyyy-MM-dd}" HeaderText="scheduled_date" ReadOnly="True" />
                <%--<asp:BoundField DataField="finished_date" DataFormatString="{0:yyyy-MM-dd}" HeaderText="finished_date" />--%>
                <asp:TemplateField HeaderText="finished_date">
                    <ItemTemplate>
                        <asp:Label ID="finished_date" runat="server" Text='<%# Eval("finished_date","{0:yyyy-MM-dd}") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <input type="text" id="datepicker" name="finished">
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="customer" HeaderText="customer" ReadOnly="True" />
                <asp:BoundField DataField="city" HeaderText="city" ReadOnly="True" />
                <asp:BoundField DataField="CE_involved" HeaderText="CE_involved" ReadOnly="True" />
                <asp:BoundField DataField="TA_involved" HeaderText="TA_involved" ReadOnly="True" />
                <asp:BoundField DataField="working_hours" HeaderText="working_hours" />
                <asp:BoundField DataField="total_labors" HeaderText="total_labors" ReadOnly="True" />
                <asp:BoundField DataField="reason" HeaderText="reason" ReadOnly="True" />
                <asp:BoundField DataField="comments" HeaderText="comments" />
                <asp:BoundField DataField="proposer" HeaderText="proposer" ReadOnly="True" />
                <asp:BoundField DataField="status" HeaderText="status" ReadOnly="True" />
                <%--将status打成模板列,添加label在只读状态显示数据,添加dropdownlist和hiddenfield在编辑状态时用于更新数据--%>
                <%--<asp:TemplateField HeaderText="status" >
                    <ItemTemplate >
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("status") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:HiddenField ID="status_hidden" Value='<%# Eval("status") %>' runat="server"/>
                        <asp:DropDownList ID="status_dropdownlist" runat="server">
                            <asp:ListItem Text="being_processed" Value="being_processed">being_processed</asp:ListItem>
                            <asp:ListItem Text="approved" Value="approved">approved</asp:ListItem>
                            <asp:ListItem Text="finished" Value="finished">finished</asp:ListItem>
                        </asp:DropDownList>
                    </EditItemTemplate>
                </asp:TemplateField>--%>
            </Fields>
            <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
            <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
        </asp:DetailsView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=SZISSD02\SQLEXPRESS;Initial Catalog=OnsiteDB;User ID=sa;Password=1qaz2wsx.." ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM [Onsite_Record] WHERE ([Service_ID] = @Service_ID)" UpdateCommand="UPDATE [Onsite_Record] SET [finished_date] = @finished_date , working_hours = @working_hours , comments = @comments WHERE Service_ID = @Service_ID" OnUpdating="SqlDataSource1_Updating">
            <SelectParameters>
                <asp:QueryStringParameter Name="Service_ID" QueryStringField="id" Type="Int32" />
            </SelectParameters>
            <UpdateParameters>
                <asp:QueryStringParameter Name="@Service_ID" QueryStringField="id" Type="Int32"/>
                <asp:Parameter Name="finished_date" Type="String"/>
                <asp:Parameter Name="working_hours" Type="Int32" />
                <asp:Parameter Name="comments" Type="String" />
            </UpdateParameters>
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

details.aspx.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

//Service_ID
//submit_date
//scheduled_date
//finished_date
//customer
//city
//CE_involved
//TA_involved
//working_hours
//total_labors
//reason
//comments
//proposer
//status
public partial class Details : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void DetailsView1_DataBound(object sender, EventArgs e)
    {
        if (DetailsView1.Rows.Count > 0)
        {
            if (DetailsView1.CurrentMode == DetailsViewMode.ReadOnly) {
                DetailsViewRow row = DetailsView1.Rows[13];
                #region updatestatus,前台代码激活status模板列后解锁此行代码
                //String status = ((Label)(row.Cells[1].FindControl("Label1"))).Text; 
                #endregion
                String status = row.Cells[1].Text;
                if (status == "being_processed")
                {
                    DetailsView1.AutoGenerateEditButton = false;
                }
                else if (status == "approved" || status == "finished")
                {
                    DetailsView1.AutoGenerateEditButton = true;
                }
            }
        }
    }
    protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {

    }
    protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
    {
        e.Cancel = true;
    }
    protected void bindDataToDetailsView(int id) {
        int service_id = Convert.ToInt16(id);
        SqlConnection conn = DBUtil.getConnection();
        DataTable dt = DBUtil.getDetailsViewDataSource(conn, service_id);
        DetailsView1.DataSource = dt;
    }
}

1 个答案:

答案 0 :(得分:0)

您需要在区域下再添加一个字段。例如

<asp:CommandField  ButtonType="Button"  ShowDeleteButton="true"  
    DeleteText="Delete Record" ShowEditButton="true"  
    EditText="Edit Record" />