【问题标题】:update a row using checkbox in the gridview ASP.NET使用 gridview ASP.NET 中的复选框更新一行
【发布时间】:2016-07-01 02:36:05
【问题描述】:

这是我的 GridView 代码,我不知道该怎么做。如果选中了复选框,我如何更新一行。如果选中复选框,则单击“btnGetSelected”时,所选行的“状态”将变为“已批准”。谢谢!

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="4" AllowPaging="true" PageIndex="2" OnPageIndexChanging="GridView1_PageIndexChanging" HeaderStyle-BackColor ="CornflowerBlue" BorderWidth="5" BorderColor="CornflowerBlue" Width="100%" CssClass="table-hover" >
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true" OnCheckedChanged="chkSelect_CheckedChanged" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Transaction Number" HeaderStyle-ForeColor="White">
                <ItemTemplate>
                    <asp:Label ID ="lblmosID" runat="server" Text='<%#Bind ("TransactionID") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Width="30px" Font-Size="15px" Font-Names="Calibri" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Date" HeaderStyle-ForeColor="White">
                <ItemTemplate>
                    <asp:Label ID ="lblDate" runat="server" Text='<%#Bind ("DateFiled") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Width="130px" Font-Names="Calibri" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Name" HeaderStyle-ForeColor="White">
                <ItemTemplate>
                    <asp:Label ID ="lblName" runat="server" Text='<%#Bind ("ReqName") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Font-Names="Calibri" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Company" HeaderStyle-ForeColor="White">
                <ItemTemplate>
                    <asp:Label ID ="lblComp" runat="server" Text='<%#Bind ("ReqCompany") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Font-Names="Calibri" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Branch" HeaderStyle-ForeColor="White">
                <ItemTemplate>
                    <asp:Label ID ="lblBranch" runat="server" Text='<%#Bind ("ReqBranch") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Font-Names ="Calibri" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Business Unit" HeaderStyle-ForeColor="White">
                <ItemTemplate>
                    <asp:Label ID ="lblBU" runat="server" Text='<%#Bind ("ReqBU") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Font-Names="Calibri" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Department" HeaderStyle-ForeColor="White">
                <ItemTemplate>
                    <asp:Label ID ="lblDept" runat="server" Text='<%#Bind ("ReqDept") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Font-Names="Calibri" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Section" HeaderStyle-ForeColor="White">
                <ItemTemplate>
                    <asp:Label ID ="lblsection" runat="server" Text='<%#Bind ("ReqSection") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Font-Names="Calibri" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Status" HeaderStyle-ForeColor="White">
                <ItemTemplate>
                    <asp:Label ID ="lblStatus" runat="server" Text='<%#Bind ("TransStatus") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Font-Names="Calibri" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="">
                <ItemTemplate>
                    <asp:LinkButton ID ="lnkEdit" runat="server" Text="View" PostBackUrl='<%# "Details.aspx?Id=" + Eval("TransactionID") %>'></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <HeaderStyle BackColor="CornflowerBlue" />
    </asp:GridView>
     <br />
     <asp:Button ID="btnGetSelected" runat="server" Text="Approve selected" OnClick="btnGetSelected_Click" CssClass="btn btn-primary" Width="10%" />
     <br />

【问题讨论】:

    标签: c# sql asp.net gridview


    【解决方案1】:

    我假设 TransactionID 是记录的主键。我进一步假设 transactionID 的数据类型是 int。如果不只是将其更改为任何内容。 将 GridView 的 HTML 中的属性设置为:

    DataKeyNames="TransactionID" 
    

    这将自动为每条记录分配主键。

    在后面的代码中:

    private void UpdateRecord(int transactionID, string status)
    {
        //Put your update code here
    }
    
    protected void btnGetSelected_Click(object sender, EventArgs e)
    {
        CheckBox chkSelect;
        int transactionID; //Or use whatever datatype this should be
    
        foreach (GridViewRow gridViewRow in GridView1.Rows)
        {
            chkSelect = (CheckBox)gridViewRow.FindControl("chkSelect");
            transactionID = (int)GridView1.DataKeys[gridViewRow.RowIndex].Value;
    
            if (chkSelect.Checked)
            {
                UpdateRecord(transactionID, "Approved");
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多