ASP. NET Grid View Control не удалось обновить данные - PullRequest
0 голосов
/ 16 апреля 2020

Я пытаюсь разработать N-уровневые CRUD-приложения. Мои операции вставки, удаления работают нормально, но когда я попытался обновить их из gridview, он не обновляет значения в базе данных и извлекает старые значения.

Вот код html.

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="StudentJobPortalPresentationsLayer.Pages.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

        <div>
          <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" AutoGenerateColumns="False"
                AutoGenerateEditButton="True" OnRowCancelingEdit="GridView1_RowCancelingEdit"
                OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" BorderColor="Silver"
                BorderStyle="Solid" BorderWidth="1px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <RowStyle BackColor="#EFF3FB" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#2461BF" />
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:TemplateField HeaderText="Job ID">
                        <EditItemTemplate>
                            <asp:Label ID="lbljobID" runat="server" Text='<%# Eval("Job_ID") %>'></asp:Label>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("Job_ID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Title">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtTitle" runat="server" Text='<%# Bind("Title") %>' Width="100px"></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# Bind("Title") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Location">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtLocation" runat="server" Text='<%# Bind("Location") %>' Width="100px"></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label3" runat="server" Text='<%# Bind("Location") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Exprience">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtExprience" runat="server" Text='<%# Bind("Exprience") %>' Width="100px"></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label4" runat="server" Text='<%# Bind("Exprience") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Type Contract">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtType_Contract" runat="server" Text='<%# Bind("Type_Contract") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label5" runat="server" Text='<%# Bind("Type_Contract") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Posted Date">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtPosted_Date" runat="server" Text='<%# Bind("Posted_Date") %>' Width="100px"></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label6" runat="server" Text='<%# Bind("Posted_Date") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Salary">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtSalary" runat="server" Text='<%# Bind("Salary") %>' Width="100px"></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label7" runat="server" Text='<%# Bind("Salary") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:HyperLinkField Text="Delete" DataNavigateUrlFields="Job_ID" DataNavigateUrlFormatString="~/Pages/DeleteJob.aspx?id={0}" />

                </Columns>
            </asp:GridView>
            <br />
            <asp:Label ID="lblResult" runat="server" Text=""></asp:Label>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add Job" />
        </div>


</asp:Content>

Вот код позади.

using StudentJobPortal_BLL_;
using StudentJobPortal_DAL_.JobObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StudentJobPortalPresentationsLayer.Pages
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        JobHandler jobHandler = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            lblResult.Text = string.Empty;

            jobHandler = new JobHandler();
            if (IsPostBack == false)
            {
                BindData();
            }
        }

        private void BindData()
        {
            GridView1.DataSource = jobHandler.GetJobList();
            GridView1.DataBind();
        }


        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindData();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindData();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Label lblID = GridView1.Rows[e.RowIndex].FindControl("lblJobID") as Label;

            TextBox txtTitle = GridView1.Rows[e.RowIndex].FindControl("txtTitle") as TextBox;
            TextBox txtLocation = GridView1.Rows[e.RowIndex].FindControl("txtLocation") as TextBox;
            TextBox txtExprience = GridView1.Rows[e.RowIndex].FindControl("txtExprience") as TextBox;
            TextBox txtType_Contract = GridView1.Rows[e.RowIndex].FindControl("txtType_Contract") as TextBox;
            TextBox txtPosted_Date = GridView1.Rows[e.RowIndex].FindControl("txtPosted_Date") as TextBox;
            TextBox txtSalary = GridView1.Rows[e.RowIndex].FindControl("txtSalary") as TextBox;



            if (lblID != null && txtTitle != null && txtLocation != null && txtExprience != null &&
                txtType_Contract != null && txtPosted_Date != null && txtSalary != null)
            {
                Job job = new Job();

                job.Job_ID = Convert.ToInt32(lblID.Text.Trim());
                job.Title = txtTitle.Text;
                job.Location = txtLocation.Text;
                job.Exprience = txtExprience.Text;
                job.Type_Contract = txtType_Contract.Text;
                job.Posted_Date = txtPosted_Date.Text;
                job.Salary = txtSalary.Text;

                //Let us now update the database
                if (jobHandler.UpdateJob(job) == true)
                {
                    lblResult.Text = "Record Updated Successfully";
                }
                else
                {
                    lblResult.Text = "Failed to Update record";
                }

                //end the editing and bind with updated records.
                GridView1.EditIndex = -1;
                BindData();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

Вот снимок экрана. enter image description here

...