Hi Experts,
I have a the below GridView :
<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"><%----%>
<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:
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;
}
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!