ASP.NET GridView需要两个不同的操作按钮

问题描述 投票:0回答:1

我有一个GridView,我想有两个带有两个不同动作的按钮。我曾尝试使它们都成为最佳选择按钮,但是我无法让ASP.NET告诉我这两个按钮中的哪一个触发了事件。它会告诉您行索引,但不会告诉我我看到的列。

我将其中一个按钮更改为编辑按钮,以便随后调用其他方法,但随后将行置于编辑模式。我看不到取消编辑的方法,并且滥用了代码的预期用途。

按钮在gv的第一列和最后一列。

<asp:GridView ID="gvMedList" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="dsMedList" GridLines="Vertical" OnSelectedIndexChanged="gvMedAction" OnDataBound="gvMedList_DataBound" OnRowEditing="gvRefillButton">
    <AlternatingRowStyle BackColor="#DCDCDC" />
    <Columns>
        <asp:CommandField ButtonType="Button" SelectText=" -STOP- " ShowSelectButton="True" ShowCancelButton="False" />
        <asp:CheckBoxField DataField="Active_Med" HeaderText="Active" SortExpression="Active_Med" >
        <HeaderStyle Width="50px" />
        <ItemStyle HorizontalAlign="Center" />
        </asp:CheckBoxField>
        <asp:BoundField DataField="Medication_List_ID" HeaderText="Medication_List_ID" InsertVisible="False" ReadOnly="True" SortExpression="Medication_List_ID" >
        <HeaderStyle Width="50px" />
        </asp:BoundField>
        <asp:BoundField DataField="Label_Name" HeaderText="Medication" SortExpression="Label_Name" >
        <HeaderStyle Width="150px" />
        </asp:BoundField>
        <asp:BoundField DataField="Med_Form" HeaderText="Form" SortExpression="Med_Form" >
        <HeaderStyle Width="50px" />
        </asp:BoundField>
        <asp:BoundField DataField="dose" HeaderText="Dose" SortExpression="dose" >
        <HeaderStyle Width="50px" />
        </asp:BoundField>
        <asp:BoundField DataField="dose_unit" HeaderText="Unit" SortExpression="dose_unit" >
        <HeaderStyle Width="50px" />
        </asp:BoundField>
        <asp:BoundField DataField="Med_Amt" HeaderText="Med_Amt" SortExpression="Med_Amt" Visible="False" />
        <asp:BoundField DataField="Amount_Unit" HeaderText="Amount_Unit" SortExpression="Amount_Unit" Visible="False" />
        <asp:BoundField DataField="Med_Sched_Label" HeaderText="Frequency" SortExpression="Med_Sched_Label" >
        <HeaderStyle Width="150px" />
        </asp:BoundField>
        <asp:BoundField DataField="Med_Dispense" HeaderText="Dispense" SortExpression="Med_Dispense" >
        <HeaderStyle Width="50px" />
        </asp:BoundField>
        <asp:BoundField DataField="Dispense_Unit" HeaderText="Unit" SortExpression="Dispense_Unit" ShowHeader="False" >
        <ItemStyle Width="50px" />
        </asp:BoundField>
        <asp:BoundField DataField="Med_Refill" HeaderText="Med_Refill" SortExpression="Med_Refill" Visible="False" />
        <asp:BoundField DataField="Comments" HeaderText="Comments" SortExpression="Comments" />
        <asp:CommandField ButtonType="Button" EditText="-REFILL-" ShowCancelButton="False" ShowEditButton="True" />
    </Columns>
   <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
    <HeaderStyle BackColor="#27627E" Font-Bold="True" ForeColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" Height="20px" HorizontalAlign="Center" VerticalAlign="Middle" Width="125px" />
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
    <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
    <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#0000A9" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
asp.net aspxgridview
1个回答
0
投票

使用CommandName属性

前端

<asp:GridView ID="gvMedList" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="dsMedList" GridLines="Vertical" OnSelectedIndexChanged="gvMedAction" OnDataBound="gvMedList_DataBound" OnRowEditing="gvRefillButton" OnRowCommand="gvMedList_RowCommand">

        <Columns>
            <asp:TemplateField HeaderText="ColumnName">
                <ItemTemplate>
                    <asp:Button ID="btnDoSomething" runat="server" CommandName="DoSomething" Text="Do Something" />
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="AnotherColumn">
                <ItemTemplate>
                    <asp:ImageButton ID="btnImageSomething" runat="server" CommandName="DoSomethingElse" ImageUrl="~/images/yes.png" />
                </ItemTemplate>
            </asp:TemplateField>

        </Columns>

    </asp:GridView>

隐藏代码(假定为C#...如果需要VB,请告诉我)

protected void gvMedList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DoSomething")
        {
            // code to execute
        }

        if (e.CommandName == "DoSomethingElse")
        {
            // code to execute
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.