如何通过行按钮单击更新行

时间:2016-07-05 03:10:32

标签: c# jquery asp.net gridview

我的gridview中有一个ID为“btnApprove”的按钮。我想要的是当用户点击按钮时,“状态”行将更新为“已批准”。我怎么能实现这一点?只有在单击按钮时才会更新一行,具体取决于交易号

这是我的aspx代码。

 <asp:UpdatePanel ID="panel1" runat="server">
         <ContentTemplate>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="TransactionID" OnRowDataBound="GridView1_OnRowDataBound" OnRowCommand="GridView1_RowCommand" CellPadding="4" AllowPaging="true" PageIndex="2" OnPageIndexChanging="GridView1_PageIndexChanging" HeaderStyle-BackColor ="CornflowerBlue" BorderWidth="1" BorderColor="Gray" Width="100%" CssClass=" table table-hover" >
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:CheckBox ID="chkHeader" runat="server" />
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="chkSelect" runat="server"  />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <img style="cursor:pointer" src ="../Images/Icons/plus2.png" />
                    <asp:Panel ID ="pnlDetails" runat="server" Style="display: none">
                        <asp:GridView ID="gvDet" runat="server" AutoGenerateColumns="false" CssClass="ChildGrid">
                            <Columns>
                                <%--<asp:BoundField ItemStyle-Width="20px" DataField="ID" HeaderText="ID" />--%>
                                <asp:BoundField ItemStyle-Width="200px" DataField="ItemType" HeaderText="Type" />
                                <asp:BoundField ItemStyle-Width="250px" DataField="ItemModel" HeaderText="Model" />
                                <asp:BoundField ItemStyle-Width="140px" DataField="ItemQuantity" HeaderText="Requested Quantity" />
                                <asp:BoundField ItemStyle-Width="80px" DataField="ItemUnit" HeaderText="Unit" />
                                <asp:BoundField ItemStyle-Width="100px" DataField="ItemDate" HeaderText="Date Needed" />
                                <asp:BoundField ItemStyle-Width="200px" DataField="ItemDesc" HeaderText="Description" />
                                <%--<asp:BoundField ItemStyle-Width="80px" DataField="ItemStatus" HeaderText="Status" />--%>
                            </Columns>
                        </asp:GridView>
                    </asp:Panel>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField ItemStyle-Width="150px" DataField="TransactionID" HeaderText="Transaction Number" />
            <asp:BoundField ItemStyle-Width="150px" DataField="DateFiled" HeaderText ="Date Filed" />
            <asp:BoundField ItemStyle-Width="150px" DataField="ReqName" HeaderText="Name" />
            <asp:BoundField ItemStyle-Width="150px" DataField="ReqCompany" HeaderText="Company" />
            <asp:BoundField ItemStyle-Width="150px" DataField="ReqBranch" HeaderText="Branch" />
            <asp:BoundField ItemStyle-Width="150px" DataField="ReqBU" HeaderText="Business Unit" />
            <asp:BoundField ItemStyle-Width="150px" DataField="ReqDept" HeaderText="Department" />
            <asp:BoundField ItemStyle-Width="150px" DataField="ReqSection" HeaderText="Section" />
            <asp:BoundField ItemStyle-Width="150px" DataField="TransStatus" HeaderText="Status" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="btnApprove" runat="server" Text="Approve" OnClick="btnApprove_Click" CssClass="btn btn-primary" />
                </ItemTemplate>
            </asp:TemplateField>

        </Columns>
        <HeaderStyle BackColor="CornflowerBlue" />
    </asp:GridView>
             </ContentTemplate>
         <Triggers>
             <asp:AsyncPostBackTrigger ControlID="GridView1" />
         </Triggers>
         </asp:UpdatePanel>

这里更新是我的GridView数据源

 public void showTable()
    {
        Utility u = new Utility();
        string conn = u.connect();
        SqlConnection connUser = new SqlConnection(conn);
        SqlDataAdapter adp = new SqlDataAdapter("select * from MosefTransaction where TransStatus = 'Pending'", connUser);
        DataTable dt = new DataTable();
        connUser.Open();
        adp.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

1 个答案:

答案 0 :(得分:2)

添加CommandArgument ='&lt;%#Container.DataItemIndex%&gt;'在您的按钮和后面的代码中,您可以获得在Gridview的RowCommand事件中引发事件的行

<asp:Button ID="btnApprove" runat="server" Text="Approve" CommandName="ApproveTransaction"  CommandArgument='<%# Container.DataItemIndex %>'/> 

在您的代码隐藏中订阅gridview的row命令事件。

protected void myGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "ApproveTransaction")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gvInfo.Rows[index];

                string cellText = row.Cells[2].Text;

               //Update your data in database here and rebind the gridview to updated data

            }
}
相关问题