2017-03-02 67 views
0

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

<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函数,但是当我添加一个并在其中放置断点时,代码从未输入该函数。

任何帮助表示赞赏。

回答

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

你不需要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 
     } 
    } 
+0

这一个工作出了大门的。 +1的答案,我会看看是否有其他人只是为了确保这是处理此问题的“正确”方式。 –

+0

你可以使用gridview RowCommand像@Yashveer所说的 – levent

相关问题