Consider the below example.
we need to modify SqlDataSource of Gridview in the following way for delete to work and also delete column of gridview must be templated. like below inside columns tag itself in gridview code i.e. in
.aspx file <asp:TemplateField ShowHeader="False" AccessibleHeaderText="Delete" HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" Text="Delete" OnClientClick="return ConfirmOnDelete('');"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
SqlSource modified in
aspx file
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myconnectionstring %>" SelectCommand="SELECT * FROM [EmpInfo2] WHERE Employee_Id=@Employee_Id OR @Employee_Id IS NULL" CancelSelectOnNullParameter="false" DeleteCommand="DELETE [EmpInfo2] WHERE Employee_Id=@Employee_Id">
<SelectParameters>
<asp:QueryStringParameter Name="Employee_Id" DbType = "String" Direction = "Input" QueryStringField="Employee_Id" DefaultValue="" ConvertEmptyStringToNull="True" />
</SelectParameters>
</asp:SqlDataSource>
then in
aspx.cs file protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState != DataControlRowState.Edit)
if (e.Row.RowType == DataControlRowType.DataRow)
{
String Employee_Id = e.Row.Cells[0].Text;
LinkButton lb = (LinkButton)e.Row.Cells[15].FindControl("LinkButton1");
if (lb != null)
{
lb.Attributes.Add("onclick", "return ConfirmOnDelete("+ Employee_Id +")");
}
}
}
I took .cells[15] as in my gridview delete column is 15th and employee_id(Primarykey) is the first and we so is given .cells[0].
In
.aspx file in head tag of gridview write the following script
<script>
function ConfirmOnDelete(Employee_Id) {
if (confirm("Are you sure to delete: " + Employee_Id + "?") == true)
return true;
else
return false;
}
</script>
Venky.Net, if this helps please login to Mark As Answer. | Alert Moderator