3

我是 c# 的新手,如果我的提问方式不正确,我深表歉意。

我已经在网格视图中添加了按钮,现在我想对 c# 中的按钮编码执行一些特定的操作。但是我无法单击按钮,这就是为什么我无法针对按钮事件编写代码。

请告诉我如何针对网格中的按钮事件编写代码???

这是我的编码:

 <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button ID="btnDecline" runat="server" Text="Decline"  onrowcommand="decline" />
                    </ItemTemplate>
                </asp:TemplateField>

这是 c# 编码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sherserve.BusinessLogicLayer;
using Sherserve.CustomTypeLayer;

namespace SherserveLeaveManagementSystem
{
    public partial class LMSAdminPanel : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                LMSManager leavestatus = new LMSManager();
                int emplid = Convert.ToInt32(Session["EmployeeID"]);
                List<EmployeeLeave> leaverecord = leavestatus.GetLeaveRecord(emplid);
                GridView1.DataSource = leaverecord;
                GridView1.DataBind();







            }
        }

        protected void GridView1_PageIndexchanging(object sender, GridViewPageEventArgs e)
        {

            LMSManager leavestatus = new LMSManager();
            int emplid = Convert.ToInt32(Session["EmployeeID"]);
            List<EmployeeLeave> leaverecord = leavestatus.GetLeaveRecord(emplid);
            GridView1.DataSource = leaverecord;
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


    }
}

我是 C# 新手,请不要介意我的提问方式是否长或不正确。

谢谢

4

2 回答 2

1

像这样使用 commandname 属性(在这种情况下选择,删除):

<asp:GridView ID="EntityGridView" runat="server" DataKeyNames="Id" DataSourceID="EntityListDataSource"
    AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True" SkinID="GridViewSmall"
    OnRowCommand="EntityGridView_RowCommand" 
    OnPageIndexChanged="EntityGridView_PageIndexChanged"
    OnSorting="EntityGridView_Sorting">
    <Columns>
        <asp:TemplateField ItemStyle-CssClass="TemplateFieldTwoColumns">
            <ItemTemplate>
                <asp:ImageButton ID="btnView" runat="server" ToolTip="<% $resources:AppResource,Edit %>" SkinID="EditPage" CommandName="Select" CommandArgument='<%# Eval("Id") %>' />
                <asp:ImageButton ID="btnDelete" runat="server" ToolTip="<% $resources:AppResource,Delete %>" SkinID="DeletePage" OnClientClick="<%# GetDeleteConfirmation() %>" CommandName="Delete" CommandArgument='<%# Eval("Id") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Title" HeaderText="<% $resources:AppResource,Title %>" SortExpression="Title" />
    </Columns>
</asp:GridView>

以下是您连接到事件的方式:

    protected void EntityGridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
             //do something on select...                
        }
    }
于 2012-09-03T11:47:24.583 回答
0

有关更多信息,请参阅此 http://www.dreamincode.net/forums/topic/184448-using-gridview-and-rowcommand-events/

protected void myGV_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "click1")
        {
            int index = Convert.ToInt32(e.CommandArgument); //get row number selected
            GridViewRow row = GridView1.Rows[index]; 
            Go ahead do something like above 

        }

        if (e.CommandName == "click2")
        {
            Do something cool ... 
        }
    }
于 2012-09-03T11:48:16.883 回答