0

我正在尝试使用 SqlDataSource 导入动态数据的 gridview ,加上 2 个包含 2 个按钮的 TemplateFields 作为AcceptDecline。我需要做的是,更新记录来自的表的特定列。(更改从 Null 到 1 或 0 的值,在名为“已接受”的列中)

Q.)我需要一种方法来识别将单击按钮的行吗?(需要识别特定行的访问键值)

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="request_no" DataSourceID="SqlDataSource1" Font-Names="Tahoma" Font-Size="Small" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
                        <AlternatingRowStyle BackColor="White" />
                        <Columns>
                            <asp:BoundField DataField="request_no" HeaderText="request_no" ReadOnly="True" SortExpression="request_no" />
                            <asp:BoundField DataField="employee_name" HeaderText="employee_name" SortExpression="employee_name" />
                            <asp:BoundField DataField="requested_date" HeaderText="requested_date" SortExpression="requested_date" />
                            <asp:BoundField DataField="requested_time" HeaderText="requested_time" SortExpression="requested_time" />
                            <asp:BoundField DataField="requested_from" HeaderText="requested_from" SortExpression="requested_from" />
                            <asp:BoundField DataField="requested_to" HeaderText="requested_to" SortExpression="requested_to" />
                            <asp:BoundField DataField="service_type" HeaderText="service_type" SortExpression="service_type" />
                            <asp:BoundField DataField="reason_data" HeaderText="reason_data" SortExpression="reason_data" />
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:Button ID="AcceptRequest" runat="server" OnClick="AcceptRequest_Click" Text="Accept" CommandName="AcceptRequestcmd" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:Button ID="DeclineRequest" runat="server" OnClick="DeclineRequest_Click" Text="Decline" CommandName="DeclineRequestcmd" />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <EditRowStyle BackColor="#2461BF" />
                        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                        <RowStyle BackColor="#EFF3FB" />
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                        <SortedAscendingCellStyle BackColor="#F5F7FB" />
                        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                        <SortedDescendingCellStyle BackColor="#E9EBEF" />
                        <SortedDescendingHeaderStyle BackColor="#4870BE" />
                    </asp:GridView>
4

1 回答 1

0

你非常接近,只是缺乏CommandArgument

                       <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Button ID="AcceptRequest" runat="server" 
                                   OnClick="AcceptRequest_Click" Text="Accept" 
                                   CommandName="AcceptRequestcmd" 
                                   CommandArgument="<%# Eval("request_no") %> />
                            </ItemTemplate>
                        </asp:TemplateField>

(假设这request_no是行标识符)

然后就

protected void GridView1_RowCommand(...)
{
   switch ( e.CommandName )
   {
      case "AcceptRequestcmd" :
         string rowid = e.CommandArgument;
   }
}
于 2013-09-02T20:02:14.340 回答