0

テーブル従業員テーブルが含まれています

EmployeeID int 主キー。FirstName varchar(50)、LastName varchar(50)、Title varchar(50)、Country varchar(50)

GridView の編集、削除、および更新機能を作成しました。しかし、更新をクリックしても更新されません。私のコード:

  <asp:GridView ID="GridView1" runat="server" GridLines="None" AutoGenerateColumns="false"

    AlternatingRowStyle-BackColor="#EEEEEE" EditRowStyle-BorderColor="Red"

    onrowcancelingedit="GridView1_RowCancelling"  

     onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" 

     onrowupdating="GridView1_RowUpdating" DataKeyNames="EmployeeID">            

     <Columns>            

       <asp:TemplateField Visible="false" HeaderText="EmployeeID">

        <ItemTemplate>

          <asp:Label runat="server" ID="EmployeeID" Text='<%#Eval("EmployeeID")%>' />

          </ItemTemplate>

         </asp:TemplateField>

         <asp:TemplateField HeaderText="LastName">

        <ItemTemplate>

          <asp:Label runat="server" ID="LastName" Text='<%#Eval("LastName") %>' />

        </ItemTemplate>

        <EditItemTemplate>

        <asp:TextBox runat="server" ID="txtLastName" Text='<%#Eval("LastName") %>' />

        <asp:RequiredFieldValidator runat="server" ID="rfdLastName"                 
           ControlToValidate="txtLastName" ValidationGroup="var1" ErrorMessage="*" />

        </EditItemTemplate>

        </asp:TemplateField>

        <asp:TemplateField HeaderText="Title">

        <ItemTemplate>

          <asp:Label runat="server" ID="Title" Text='<%#Eval("Title") %>' />

        </ItemTemplate>

        <EditItemTemplate>

          <asp:TextBox runat="server" ID="txtTitle" Text='<%#Eval("Title") %>' />

          <asp:RequiredFieldValidator runat="server" ID="rfdTitle"                
                 ControlToValidate="txtTitle" ValidationGroup="var1" ErrorMessage="*" />

        </EditItemTemplate>

     </asp:TemplateField>

       <asp:TemplateField HeaderText="Country">
       <ItemTemplate>

         <asp:Label runat="server" ID="Country" Text='<%#Eval("country") %>'/>

       </ItemTemplate>

       <EditItemTemplate>

         <asp:TextBox runat="server" ID="txtCountry" Text='<%#Eval("country") %>' />

         <asp:RequiredFieldValidator runat="server" ID="rfdCountry"          
                ControlToValidate="txtCountry" ValidationGroup="var1" ErrorMessage="*" />

       </EditItemTemplate>

      </asp:TemplateField>

     <asp:TemplateField HeaderText="Action">

    <ItemTemplate>

   <asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" />

  <br />
  <asp:LinkButton ID="btnDelete" Text="Delete" runat="server" CommandName="Delete" />

  </ItemTemplate>

  <EditItemTemplate>

  <asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" />

  <asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />

  </EditItemTemplate>

    </asp:TemplateField>
   </Columns>
  </asp:GridView>

私のC#コード:

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

 namespace new1
{
public partial class WebForm7 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
       {
          SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings    ["vinkpConnectionString"].ConnectionString);
               conn.Open();
               SqlCommand comm = new SqlCommand("select EmployeeID,FirstName,LastName,Title,Country from employeeTable  ", conn);
               comm.ExecuteReader();
               conn.Close();
               SqlDataAdapter da = new SqlDataAdapter(comm);
                   DataTable dt = new DataTable();
                   da.Fill(dt);
                   GridView1.DataSource = dt;
                   GridView1.DataBind();


           }


    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
       {

        GridView1.EditIndex = e.NewEditIndex;

        BindGridData();

        }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

        {

         string s = GridView1.DataKeys[e.RowIndex].Value.ToString();            

         Label EmployeeID = GridView1.Rows[e.RowIndex].FindControl("EmployeeID") as Label;

         TextBox LastName = GridView1.Rows[e.RowIndex].FindControl("txtLastName") as TextBox;

         TextBox Title = GridView1.Rows[e.RowIndex].FindControl("txtTitle") as TextBox;

         TextBox Country = GridView1.Rows[e.RowIndex].FindControl("txtCountry") as TextBox;

         String UpdateQuery = string.Format("UPDATE employeeTable SET LastName,Title,Country WHERE EmployeeID='"+Convert.ToInt32(EmployeeID.Text)+"'",LastName.Text, Title.Text, Country.Text);

           GridView1.EditIndex = -1;

           BindGridData(UpdateQuery);

           }
    private void BindGridData(string Query)

         { 

           string connectionstring  = ConfigurationManager.ConnectionStrings["vinkpConnectionString"].ConnectionString;
           using (SqlConnection conn = new SqlConnection(connectionstring))
             {
             conn.Open();
             using (SqlCommand comm = new SqlCommand(Query +";select EmployeeID,FirstName,LastName,Title,Country from employeeTable ", conn))
                {
                    SqlDataAdapter da = new SqlDataAdapter(comm);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    GridView1.DataSource = ds;
                    GridView1.DataBind();

                  }

               }

          }

    protected void GridView1_RowCancelling(object sender, GridViewCancelEditEventArgs e)
       {
            GridView1.EditIndex = -1;
           BindGridData();
        }
   protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

      {
       string EmployeeID = GridView1.DataKeys[e.RowIndex].Value.ToString();
       string Query = "delete employeeTable where EmployeeID = " + EmployeeID;
          BindGridData(Query);
       }
   private void BindGridData()
      {
          using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["vinkpConnectionString"].ConnectionString))
           {
             conn.Open();
             using (SqlCommand comm = new SqlCommand("select EmployeeID,FirstName,LastName,Title,Country from employeeTable ", conn))

                {
                   SqlDataAdapter da = new SqlDataAdapter(comm);
                   DataSet ds = new DataSet();
                   da.Fill(ds); 
                   GridView1.DataSource = ds;
                   GridView1.DataBind();
                 }

             }

        }


   }

エラーが発生するのはなぜですか?コードに間違いがありますか?クリックすると編集テキストボックスが表示されますが、更新をクリックするとGridviewに更新が表示されません。

4

1 に答える 1

0

page_load イベントが発生するたびに Page_Load で GridView1 をバインドしているため、GridViewonの変更が失われpostbackます。グリッドを!IsPostBackブロックにバインドして、ポストバック時にその状態を保持する

protected void Page_Load(object sender, EventArgs e)
{
      if(!Page.IsPostBack)
      {   
          SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings    ["vinkpConnectionString"].ConnectionString);
          conn.Open();
          SqlCommand comm = new SqlCommand("select EmployeeID,FirstName,LastName,Title,Country from employeeTable  ", conn);
          comm.ExecuteReader();
          conn.Close();
          SqlDataAdapter da = new SqlDataAdapter(comm);
          DataTable dt = new DataTable();
          da.Fill(dt);
          GridView1.DataSource = dt;
          GridView1.DataBind();
      }
}
于 2013-04-07T04:59:39.543 に答える