在Gridview中标识特定记录的访问键,(对于按钮/ TemplateField)

时间:2013-09-02 19:55:05

标签: c# asp.net gridview

我正在尝试使用SqlDataSource导入动态数据的gridview,以及包含2个按钮的2个TemplateFields Accept Decline 。我需要做的是,更新记录所来自的表的特定列。(将值从Null更改为1或0,在名为&#34的列中;接受")

问:)我需要一种方法来识别按钮被点击的行? (需要识别特定行的访问密钥的值)

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

1 个答案:

答案 0 :(得分: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;
   }
}