单击按钮后保存数据并隐藏ASP.NET Gridview行

时间:2016-04-21 12:40:13

标签: c# asp.net gridview

我在ASP.NET,C#中创建预订应用程序。 我有一个带有asp.net按钮的gridview,它调用C#方法根据gridview行中的某些数据更新数据库。

如何在点击按钮上隐藏行并保存数据?

源代码:

ASPX

 <asp:GridView ID="GridView1" runat="server"  HorizontalAlign="Center" AllowPaging="True" CellPadding="3" Width="568px"  GridLines="Vertical" Visible="False" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px">       

        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <HeaderStyle BackColor="#121315" Font-Bold="True" ForeColor="White" />
        <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" />  

           <AlternatingRowStyle BackColor="#DCDCDC" />

      <Columns>          

         <asp:TemplateField HeaderText="HALL">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("HallGroupID") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

          <asp:TemplateField HeaderText="ID">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("ShowSeatID") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("ShowSeatID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

           <asp:TemplateField HeaderText="COLOR">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Color") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("Color") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

           <asp:TemplateField HeaderText="PRICE">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Price") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label5" runat="server" Text='<%# Bind("Price") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>  

          <asp:TemplateField HeaderText="ROW">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("SeatRow") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label6" runat="server" Text='<%# Bind("SeatRow") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField> 

          <asp:TemplateField HeaderText="NUMBER">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("SeatNumber") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label7" runat="server" Text='<%# Bind("SeatNumber") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>               

           <asp:TemplateField HeaderText="STATUS" SortExpression="Active">
                    <ItemTemplate><%# (Boolean.Parse(Eval("IsReserved").ToString())) ? "RESERVED" : "FREE" %></ItemTemplate>
             </asp:TemplateField>            

            <asp:TemplateField>                  
                <ItemTemplate>                        
                   <asp:Button ID="ButtonReserve" style="background:#2ACC16; border-radius:8px;" runat="server" CommandName="Reserve" HeaderText="BOOK SEAT" Text="Reserve" OnClick="ButtonReserve_Click" />                
                </ItemTemplate>            
             </asp:TemplateField> 

       </Columns>                      
   </asp:GridView> 

代码

protected void ButtonReserve_Click(object sender, EventArgs e)
{
    GridViewRow gvr = ((Button)sender).Parent.Parent as GridViewRow;
    Label hallID = (Label)gvr.FindControl("Label1");     
    Label seatID = (Label)gvr.FindControl("Label3");
    Label color = (Label)gvr.FindControl("Label4");
    Label price = (Label)gvr.FindControl("Label5");
    Label seatRow = (Label)gvr.FindControl("Label6");
    Label seatNumber = (Label)gvr.FindControl("Label7");

    Label7.Text = hallID.Text;       
    Label9.Text = seatID.Text;
    Label10.Text = color.Text;
    Label11.Text = price.Text;
    Label12.Text = seatRow.Text;
    Label13.Text = seatNumber.Text;

}

1 个答案:

答案 0 :(得分:1)

通常,您不会隐藏该行。

您将进行更新并重新绑定GridView。

根据您的需要,该行将从数据集中排除或包含并以某种方式标记,以表示其新的“更新”状态。