none
使用detailsview update 总是出错 The DetailsView 'detailsView1' fired event ItemUpdating which wasn't handled. RRS feed

  • 问题

  • 我的代码

    前台 代码

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Practise.WebForm1" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div style=" margin-left:100px; margin-top:20px;">
      <asp:DetailsView AutoGenerateRows="False"
      OnItemCommand="DetailsView1_ItemCommand"
      OnModeChanging="DetailsView1_ModeChanging"
                HeaderText="Staff Informition" ID="detailsView1" runat="server"
                Width="300px">
                <Fields>
                  <asp:BoundField DataField="CustomerId" HeaderText="Customer Id" ReadOnly="True" SortExpression="CustomerId" />
                  <asp:BoundField DataField="CompanyName" HeaderText="Company Name"  ReadOnly="true" SortExpression="Company Name" />
                  <asp:BoundField DataField="city" HeaderText="city"  ReadOnly ="true" SortExpression="city" />
                  <asp:BoundField DataField="phone" HeaderText="phone" ReadOnly="true" SortExpression="phone" />
                  <asp:BoundField DataField="contactName" HeaderText="contact Name" SortExpression="contactName" />
                  <asp:BoundField DataField="region" HeaderText="Region" SortExpression="region" />
                  <asp:CommandField ShowEditButton="True" />

                 
                </Fields>
              </asp:DetailsView>
    </div>
        </form>
    </body>
    </html>




    后台代码


    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 Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common;

    namespace Practise
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {

                if (!IsPostBack)
                {

                    string id = "ALFKI";


                    DataSet dSet = getMyConnectionById(id);
                    detailsView1.DataSource = dSet;
                    detailsView1.DataBind();
                }

            }


            public void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
            {

                if (e.CommandName.Equals("Edit"))

                    detailsView1.ChangeMode(DetailsViewMode.Edit);
                else if (e.CommandName.Equals("Cancel"))

                    detailsView1.ChangeMode(DetailsViewMode.ReadOnly);

                else if (e.CommandName.Equals("Update"))

                    UpdateDetailsView();

            }
            public void UpdateDetailsView()
            {
                //int id = Convert.ToInt32(Session["staffId"]);
                string id = "ALFKI";
                //string phoneNumber = (String)detailsView1.Rows[2].Cells[0].FindControl("phoneNumber");

                //string address = (String)detailsView1.Rows[2].Cells[0].FindControl("address");
                string contactName = detailsView1.Rows[2].Cells[0].Text;
                string region = detailsView1.Rows[2].Cells[0].Text;

                updateMyCustomers(  id , contactName , region);


            }


            protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
            {

                if (e.NewMode == DetailsViewMode.Edit)
                {
                    detailsView1.ChangeMode(DetailsViewMode.Edit);
                }
                if (e.NewMode == DetailsViewMode.Insert)
                {
                    detailsView1.ChangeMode(DetailsViewMode.Insert);
                }
                if (e.NewMode == DetailsViewMode.ReadOnly)
                {
                    detailsView1.ChangeMode(DetailsViewMode.ReadOnly);
                }

            }
            public static DataSet getMyConnectionById(string CustomerId)
            {
                Database db = DatabaseFactory.CreateDatabase();
                DataSet ds = db.ExecuteDataSet("PractiseForMyPlanById", CustomerId);
                return ds;
            }
            public static void updateMyCustomers(string customerId ,string contactName ,string region)
              {
                    Database  db = DatabaseFactory.CreateDatabase();
                    db.ExecuteNonQuery("CostomerUpdateById",customerId, contactName, region);
                   
       
               }


      

    }
    }



    数据库是Northwind

    2009年5月3日 7:00

答案