GridView - 如何添加已启用OnRowEditing和OnRowUpdating的记录?

时间:2017-03-02 19:47:09

标签: c# asp.net gridview

我有一个GridView,它有OnRowEditing,UnRowUpdating和OnRowDeleting按钮。现在我们想让用户添加一条新记录。所以目前我有这个:

<asp:Panel runat="server" ID="ShowDiv1" Visible="false" BorderStyle="Solid" BorderWidth="0" Width="440px">
        <asp:Label ID="lblShowDiv1Title" runat="server" Text="Executive Review Leads" Font-Bold="true"></asp:Label>
        <br />
        <div id="divGrid" style='width:450px; overflow:auto'>
                <asp:GridView ID="DataGrid_Leads" runat="server" 
                    AutoGenerateColumns="False" 
                        ShowFooter="true"
                    CellPadding="1" 
                        CssClass="hoverTable"
                    DataKeyNames="LOOKUP_VALUE"
                    OnRowCancelingEdit="DataGrid_Leads_CancelCommand"   
                    OnRowEditing="DataGrid_Leads_EditCommand" 
                    OnRowDeleting="DataGrid_Leads_DeleteCommand"
                    OnRowUpdating="DataGrid_Leads_UpdateCommand">  
                        <Columns>  
                        <asp:TemplateField HeaderText="Lead" ItemStyle-Width="230px">  
                        <ItemTemplate>  
                                <asp:Label ID="lbl_Name" runat="server" Width="220px" Text='<%#Eval("LOOKUP_DESCRIPTION") %>'></asp:Label>  
                        </ItemTemplate>  
                        <EditItemTemplate>  
                                <asp:TextBox ID="txt_Name" runat="server" Width="220px" Text='<%#Eval("LOOKUP_DESCRIPTION") %>'></asp:TextBox>  
                        </EditItemTemplate>  
                                                <FooterTemplate>  
                                <asp:TextBox ID="ntxt_Name" runat="server" Width="220px" ></asp:TextBox>  
                        </FooterTemplate>
                        </asp:TemplateField>        
                        <asp:TemplateField ItemStyle-Width="90px">  
                        <ItemTemplate>  
                                <asp:Button ID="btn_Edit" runat="server" Text="Edit" CommandName="Edit" />  
                        </ItemTemplate>  
                        <EditItemTemplate>  
                                <asp:Button ID="btn_Update" runat="server" Text="Update" CommandName="Update"/>  
                                <asp:Button ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel"/>  
                        </EditItemTemplate>  
                                                <FooterTemplate>
                                                        <asp:Button ID="btn_Add" runat="server" Text="Add" CommandName="Add" />
                                                </FooterTemplate>
                        </asp:TemplateField>  
                        <asp:TemplateField>  
                        <ItemTemplate>  
                                <asp:Button ID="btn_Delete" runat="server" OnClientClick="javascript:return confirm('Are you sure?');" Text="Delete" CommandName="Delete" />  
                        </ItemTemplate>  
                        </asp:TemplateField>        
                                        <asp:TemplateField HeaderText="ID">  
                        <ItemTemplate>  
                                <asp:Label ID="lbl_ID" runat="server" Text='<%#Eval("LOOKUP_VALUE") %>'></asp:Label>  
                        </ItemTemplate>  
                        </asp:TemplateField>        

                        </Columns>  
                                <AlternatingRowStyle Font-Bold="False" Font-Italic="False" 
                                        Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
                                <RowStyle BackColor="White" Font-Bold="False" Font-Italic="False" 
                                        Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
                                <FooterStyle BackColor="#4DA6A6" Font-Bold="False" Font-Italic="False" 
                                        Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
                                <HeaderStyle BackColor="#4DA6A6" Font-Bold="False" Font-Italic="False" 
                                        Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
                                <PagerSettings Mode="Numeric" Position="Bottom" />
                                <SelectedRowStyle BackColor="#e3f561" Font-Bold="False" Font-Italic="False" 
                                        Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
                </asp:GridView>  
                <asp:Label ID="lblEmpty" runat="server" Visible="false" Style="font-weight:bold; font-size:large;"></asp:Label>
        </div> 
</asp:Panel>

在展示我需要看到的东西方面,一切都很好。在代码隐藏中,我有一些函数可以处理所有内容,但添加了一条新记录:

protected void DataGrid_Leads_EditCommand(object sender, GridViewEditEventArgs e)
{
        DataGrid_Leads.EditIndex = e.NewEditIndex;
        LoadLeadsGrid();
}

protected void DataGrid_Leads_UpdateCommand(object sender, GridViewUpdateEventArgs e)
{
        int userid = Convert.ToInt32(DataGrid_Leads.DataKeys[e.RowIndex].Value.ToString());
        GridViewRow row = (GridViewRow)DataGrid_Leads.Rows[e.RowIndex];

        Label lbl_ID = (Label)DataGrid_Leads.Rows[e.RowIndex].FindControl("lbl_ID");

        TextBox txt_Name = (TextBox)DataGrid_Leads.Rows[e.RowIndex].FindControl("txt_Name");

        DataGrid_Leads.EditIndex = -1;
        OracleConnection conn = GetConnection();
        conn.Open();
        ////SqlCommand cmd = new SqlCommand("SELECT * FROM detail", conn);
        OracleCommand cmd = new OracleCommand("UPDATE MY_VALUES set LOOKUP_DESCRIPTION = '" + txt_Name.Text + "' where LOOKUP_VALUE = '" + userid + "' AND LOOKUP_AREA = 'LEAD'", conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        LoadLeadsGrid();
}

protected void DataGrid_Leads_DeleteCommand(object sender, GridViewDeleteEventArgs e)
{
        int userid = Convert.ToInt32(DataGrid_Leads.DataKeys[e.RowIndex].Value.ToString());
        GridViewRow row = (GridViewRow)DataGrid_Leads.Rows[e.RowIndex];
        Label lbldeleteid = (Label)row.FindControl("lbl_ID");

        OracleConnection conn = GetConnection();
        conn.Open();
        OracleCommand cmd = new OracleCommand("DELETE FROM MY_VALUES where LOOKUP_VALUE = '" + userid + "' AND LOOKUP_AREA = 'LEAD'", conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        LoadLeadsGrid();
}

protected void DataGrid_Leads_CancelCommand(object sender, GridViewCancelEditEventArgs e)
{
        DataGrid_Leads.EditIndex = -1;
        LoadLeadsGrid();
}

那么,我该如何处理“添加”按钮?我在网上读到你会使用RowCommand函数,但是当我添加一个并在其中放入一个断点时,代码从未进入该函数。

感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

try something like this

     void ProductsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
    // If multiple buttons are used in a GridView control, use the
    // CommandName property to determine which button was clicked.
    if(e.CommandName=="Add")
    {
      // Convert the row index stored in the CommandArgument
      // property to an Integer.
      int index = Convert.ToInt32(e.CommandArgument);

      // Retrieve the row that contains the button clicked 
      // by the user from the Rows collection.
      GridViewRow row = ProductsGridView.Rows[index];

      // Create a new ListItem object for the product in the row.     
      ListItem item = new ListItem();
      item.Text = Server.HtmlDecode(row.Cells[1].Text);

      // If the product is not already in the ListBox, add the ListItem 
      // object to the Items collection of the ListBox control. 
      if (!ProductsListBox.Items.Contains(item))
      {
        ProductsListBox.Items.Add(item);
      }
    }
  }

    <asp:GridView ID="ProductsGridView" 
                  DataSourceID="ProductsDataSource"
                  AllowPaging="true" 
                  AutoGenerateColumns="false"
                  OnRowCommand="ProductsGridView_RowCommand"
                  OnRowCreated="ProductsGridView_RowCreated"  
                  runat="server">
                  <Columns>
                    <asp:TemplateField>
                      <ItemTemplate>
                        <asp:LinkButton runat="server"
                          ID="AddButton"
                          CommandName="Add"
                          Text="Add" />
                      </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="Name" 
                      HeaderText="Product Name"/> 
                    <asp:BoundField DataField="ProductNumber" 
                      HeaderText="Product Number"/>
                  </Columns>

                </asp:GridView>

答案 1 :(得分:1)

你不需要gridview命令。你可以添加命令直接添加按钮..

<asp:Button ID="btn_Add" runat="server" Text="Add" CommandName="Add" />

更改为

<asp:Button ID="btn_Add" runat="server" Text="Add" OnClick="btn_Add_Click"  />

服务器端:

    protected void btn_Add_Click(object sender, EventArgs e)
    {
        var textBox = GridView1.FooterRow.FindControl("ntxt_Name") as TextBox;
        if (textBox != null)
        {
            var value = textBox.Text;
            // insert operation
        }
    }