在asp.net中单击编辑按钮时生成更新和取消按钮

时间:2016-08-24 06:50:05

标签: c# asp.net gridview

我有一个动态绑定网格视图的内容页面。我也添加了编辑和删除按钮。但是在点击编辑按钮时,如何生成更新并取消链接按钮。

我知道必须在RowEditing Event中处理它。请帮我详细说明如何获取这些按钮。  这是ap.net网页

<%@ Page Title="" Language="C#" MasterPageFile="~/ManageSMS.Master" AutoEventWireup="true" CodeBehind="WebForm10.aspx.cs" Inherits="SMS_Mod2.WebForm10" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"  OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
    <asp:Panel ID="Panel1" Visible="false" runat="server"><asp:DropDownList ID="DropDownList2" AutoPostBack="true" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" runat="server"></asp:DropDownList></asp:Panel>
    <asp:GridView ID="GridView1" runat="server">
        <Columns>
            <asp:CommandField ShowDeleteButton ="true" />
            <asp:CommandField ShowEditButton="true" />
        </Columns>
    </asp:GridView>
</asp:Content>

这是代码隐藏文件。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BOL;
using DAL;
namespace SMS_Mod2
{
    public partial class WebForm10 : System.Web.UI.Page
    {
        StationaryManagement stMgmt = new StationaryManagement();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownList1.Items.Add(new ListItem("All", "0"));
                DropDownList1.Items.Add(new ListItem("Branch", "1"));
                //DropDownList1.DataBind();

            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(DropDownList1.SelectedIndex != 0)
            {
                Panel1.Visible = true;
                DropDownList2.DataSource = stMgmt.ViewBranches();
                DropDownList2.DataTextField = "BranchName";
                DropDownList2.DataValueField = "BranchID";
                DropDownList2.DataBind();
                DropDownList2.Items.Add(new ListItem("Please select", "0"));
            }
            else
            {
                Panel1.Visible = false;
            }
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {

                GridView1.DataSource = stMgmt.ViewLocations(DropDownList2.SelectedIndex);
            GridView1.DataBind();
            GridView1.AllowSorting = true;
            GridView1.AutoGenerateEditButton = true;
            GridView1.AutoGenerateDeleteButton = true;

        }
    }
}

1 个答案:

答案 0 :(得分:0)

GridView具有您可以使用的AutoGenerateEditButton属性。此设置还将在编辑时显示更新/取消按钮。

<asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="true">
    <Columns>

    </Columns>
</asp:GridView>

要更新网格,您还需要使用以下GridView事件:OnRowEditingOnRowCancelingEditOnRowUpdating

在编辑活动中,您通常不需要做任何事情。在代码隐藏中的更新事件中,您需要执行更新逻辑并最终设置GridView1.EditIndex = -1;以结束编辑。最后在取消事件中,您只需要再次将EditIndex设置为-1。

还有一个属性可以自动生成删除按钮。然后,如果我没记错的话,你需要在OnRowDeleting事件中删除数据集中的行。

GridView看起来像这样:

<asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="true" OnRowDeleting="GridView1_RowDeleting" AutoGenerateEditButton="true" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating">
相关问题