Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Experts,
I have a the below GridView :
ASP.NET
<asp:GridView runat="server" ID="SO_Grid" AutoGenerateColumns="false" DataSourceID="so_emails_DataSource" DataKeyNames="U_Email" CssClass="table table-hover table-striped" HeaderStyle-ForeColor="#10466E"
     ShowFooter="true"><%--OnRowDeleting="SO_Grid_RowDeleting"  OnRowCommand="SO_Grid_RowCommand"--%>
<EmptyDataTemplate>no emails found</EmptyDataTemplate>
 <Columns>
 <asp:TemplateField HeaderText="SO Email">
  <ItemTemplate>
    <asp:Label runat="server" ID="lbl_SO_Email"><%#Eval("U_Email") %></asp:Label></ItemTemplate>
  <FooterTemplate>
    <asp:TextBox runat="server" ID="txt_SO_Email"></asp:TextBox></FooterTemplate>
     </asp:TemplateField>
      <asp:TemplateField HeaderText="Delete Email">
     <ItemTemplate>
      <asp:Button runat="server" ID="btn_Delete" Text="Delete" CommandName="remove" ForeColor="#10466E" BackColor="Transparent" BorderStyle="None" OnClientClick="return confirm('Are you sure you want to delete this email?');" OnClick="btn_Delete_Click" CommandArgument='<%#Container.DataItemIndex %>' />
   </ItemTemplate>
   <FooterTemplate>
  <asp:Button runat="server" ID="btn_Add" Text="Add New" CommandName="newEmail" ForeColor="#10466E" BackColor="Transparent" BorderStyle="None" OnClick="btn_Add_Click" /></FooterTemplate>
      </asp:TemplateField>
    </Columns>
</asp:GridView> 

the Add New Email Button on the footer fires twice, it adds new email twice!
I tried to add the new email on RowCommand it does the same so I changed my code to add on Button click
Here is my code to add new Email:
C#
protected void btn_Add_Click(object sender, EventArgs e)
        {
            TextBox new_SO_Email = (TextBox)SO_Grid.FooterRow.FindControl("txt_SO_Email");
            string email = new_SO_Email.Text;
            DataTable mytb = clsBP.Check_BP_Emails(lbl_BP_Id.Text, email, "SO");
            if (mytb.Rows.Count > 0)
            { new_SO_Email.Focus(); ClientScript.RegisterStartupScript(this.GetType(), "callfunction", "alert('This Email already exists.');window.location.href = BP.aspx;", true); }
            else
            {
                bool isAdded = false;
                isAdded = clsBP.Add_BP_Emails(lbl_BP_Id.Text, email, "SO");
                if (isAdded)
                {
                    SO_Grid.DataBind();
                    lbl_SO_Msg.Visible = true;
                    //ClientScript.RegisterStartupScript(this.GetType(), "callfunction", "alert('Thank you! The Email is successfuly deleted.');window.location.href = BP.aspx;", true);
                }
                else
                { ClientScript.RegisterStartupScript(this.GetType(), "callfunction", "alert('Database problem, please try again later.');window.location.href = BP.aspx;", true); }
                new_SO_Email.Text = "";
            }
        }


Please advise

Thanks in advance!
Posted
Comments
Member 11151142 7-Aug-15 6:44am    
insted of onclick event used onrowcommand of gridview..
Samira Radwan 7-Aug-15 8:47am    
i used onrowcommand before onClick and had the same problem!
Member 11151142 8-Aug-15 1:44am    
have you debug your code???This shouldn't be happend....Check somewhere you are made a mistake...may be some function call twice..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900