为什么我的gridview不返回我更新的内容?

时间:2019-05-14 16:37:53

标签: c# asp.net updates crud three-tier

所以这一次,我想更新数据,但是当我这样做时,表最终变成空白并且什么都没有弹出。因此,例如,我的模态将弹出,我可以进行编辑,但是字段最终变成空白,并且当我尝试单击“保存更改”时,gridview中的数据表最终变成空白。可以看到我所缺少的吗?

UPDATE MODAL

    <div id="UpdateModal" class="modal fade" role="dialog">
      <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Update User Account</h4>
          </div>
          <div class="modal-body">
              Id No.: <asp:Label ID="lblGuestID" runat="server" Text=""></asp:Label>
                <table>
                    <tr>
                        <td><asp:Label ID="Label1" runat="server" Text="First Name"></asp:Label></td>
                        <td><asp:TextBox ID="txtUpdateFName" runat="server" CssClass="form-control"></asp:TextBox> </td>
                    </tr>
                     <tr>
                        <td><asp:Label ID="Label2" runat="server" Text="Last Name"></asp:Label></td>
                        <td><asp:TextBox ID="txtUpdateLName" runat="server" CssClass="form-control"></asp:TextBox> </td>
                    </tr>
                     <tr>
                        <td><asp:Label ID="Label3" runat="server" Text="Phone"></asp:Label></td>
                        <td><asp:TextBox ID="txtUpdatePhone" runat="server" CssClass="form-control"></asp:TextBox> </td>
                    </tr>
                </table>
          </div>
          <div class="modal-footer">
              <asp:Button ID="btnExecuteUpdate" class="btn btn-default" runat="server" Text="Save" OnClick="btnExecuteUpdate_Click"/>
              <asp:Button ID="btnCancelUpdate" class="btn btn-default" runat="server" Text="Cancel" data-dismiss="modal"/>
          </div>
        </div>
      </div>
    </div>

GRIDVIEW

<asp:GridView ID="gvGuest" runat="server" AutoGenerateColumns="False"
        OnPageIndexChanging="PageIndexChanging" AllowPaging="true" PageSize="5"
        DataKeyNames="GuestID">
        <Columns>

             <asp:TemplateField HeaderText="Guest ID" InsertVisible="False" SortExpression="GuestID">
                <ItemTemplate>
                    <asp:Label ID="lblGuestID" runat="server" Text='<%#Eval("GuestID")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="First Name">
                <ItemTemplate>
                    <asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Last Name">
                <ItemTemplate>
                    <asp:Label ID="lblLastName" runat="server" Text='<%#Eval("LastName")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

             <asp:TemplateField HeaderText="Phone">
                <ItemTemplate>
                    <asp:Label ID="lblPhone" runat="server" Text='<%#Eval("Phone")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
             <asp:TemplateField HeaderText ="Actions">
                <ItemTemplate>
                    <asp:Button ID="btnUpdate" runat="server" ForeColor="Blue" Text="Update" OnClick="btnUpdate_Click"></asp:Button>
                    <asp:Button ID="btnDelete" runat="server" ForeColor="Red" Text="Delete" OnClick="btnDelete_Click"></asp:Button>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

BEL

 public class GuestBEL
    {
        public string GuestID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Phone { get; set; }
    }

BLL

public void UpdateGuest(GuestBEL objBEL)
        {
            GuestDAL objDAL = new GuestDAL();
            objDAL.UpdateGuest(objBEL);
        }

DAL

 public void UpdateGuest(GuestBEL objBEL)
        {
            SqlCommand cmd = new SqlCommand("GuestUpdate", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@FirstName", objBEL.FirstName);
            cmd.Parameters.AddWithValue("@LastName", objBEL.LastName);
            cmd.Parameters.AddWithValue("@Phone", objBEL.Phone);
            cmd.Parameters.AddWithValue("@GuestID", objBEL.GuestID);
            con.Open();
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            con.Close();
        }

CS

 protected void btnUpdate_Click(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "UpdateModal", "$('#UpdateModal').modal();", true);
            clickedRow = ((Button)sender).NamingContainer as GridViewRow;
            string pk = ((Label)clickedRow.FindControl("lblGuestID")).Text;
            DataTable dt = new DataTable();
            lblGuestID.Text = pk.ToString();
            pk = ((Label)clickedRow.FindControl("Label1")).Text;
            Label1.Text = pk.ToString();
            pk = ((Label)clickedRow.FindControl("Label2")).Text;
            Label2.Text = pk.ToString();
            pk = ((Label)clickedRow.FindControl("Label3")).Text;
            Label3.Text = pk.ToString();

        }

        protected void btnExecuteUpdate_Click(object sender, EventArgs e)
        {
            objBEL.GuestID = lblGuestID.Text;
            objBEL.FirstName = txtUpdateFName.Text;
            objBEL.LastName = txtUpdateLName.Text;
            objBEL.Phone = txtUpdatePhone.Text;
            objBLL.UpdateGuest(objBEL);
            gvGuest.EditIndex = -1;
            readGrid();
            objBEL = null;
            objBLL = null;
            objBEL = null;
            objBLL = null;
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "myPrompt", "alert('Successfully Saved Changes!');", true);
        }

存储过程

CREATE PROCEDURE [dbo].[GuestUpdate]
    @GuestID int,
    @FirstName varchar(20),
    @LastName varchar(20),
    @Phone varchar(15)
AS
BEGIN
    UPDATE Guest SET FirstName = @FirstName, LastName = @LastName, Phone = @Phone
    WHERE GuestID = @GuestID
RETURN
END

READGRID

 private void readGrid()
        {
            DataSet data = new DataSet();
            data = objBLL.ReadGuest();
            gvGuest.DataSource = data;
            gvGuest.DataBind();
        }

插入

 protected void btnInsert_Click(object sender, EventArgs e)
        {
            objBEL.FirstName = txtFName.Text.Trim();
            objBEL.LastName = txtLName.Text.Trim();
            objBEL.Phone = txtPhone.Text.Trim();
            objBLL.SaveGuest(objBEL);
            ScriptManager.RegisterClientScriptBlock(btnSave, GetType(), "a", "alert('Guest details saved.')", true);
            txtFName.Text = string.Empty;
            txtLName.Text = string.Empty;
            txtPhone.Text = string.Empty;
            readGrid();
            objBEL = null;
            objBLL = null;
        }

0 个答案:

没有答案