【问题标题】:Generating Update and cancel buttons on clicking edit button in asp.net在asp.net中单击编辑按钮时生成更新和取消按钮
【发布时间】:2016-08-24 06:50:04
【问题描述】:

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

我知道它必须在 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;

        }
    }
}

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    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">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-10
      • 2013-03-01
      • 1970-01-01
      • 2011-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多