Hello everyone!
Basically a user is able to select one or multiple rows using checkboxes in a GridView and then a delete button which deletes selected rows.
The problem is, the code behind for deleting works until I started using UpdatePanel. It just broke and I have no idea what went wrong.
Here's my ASPX code;
<asp:UpdatePanel ID="upl" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSearch" EventName="serverclick" />
<asp:AsyncPostBackTrigger ControlID="GvGrade" />
<asp:PostBackTrigger ControlID="BtnRegister" />
</Triggers>
<ContentTemplate>
<asp:GridView ID="GvGrade" runat="server" AutoGenerateColumns="false" ClientIDMode="Static">
<Columns>
<asp:TemplateField HeaderText="Task ID" HeaderStyle-HorizontalAlign="Center">
<HeaderStyle Width="100" BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lblID" CssClass="lblID" runat="server" Text='<%# Eval("drug_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Drug Name" HeaderStyle-HorizontalAlign="Center">
<HeaderStyle Width="300" BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lblDrugName" CssClass="lblImageName" runat="server" Text='<%# Eval("drug_name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Drug Description" HeaderStyle-HorizontalAlign="Center">
<HeaderStyle Width="500" BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lblDrugDescription" CssClass="lblImage" runat="server" Text='<%# Eval("drug_description") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Task Status" HeaderStyle-HorizontalAlign="Center">
<HeaderStyle Width="200" BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lblTaskStatus" CssClass="lblImage" runat="server" Text='<%# Eval("task_status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Task Type" HeaderStyle-HorizontalAlign="Center">
<HeaderStyle Width="100" BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lblTaskType" CssClass="lblImage" runat="server" Text='<%# Eval("task_type") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Delete Tasks" ItemStyle-Width="100px" ItemStyle-HorizontalAlign="Center">
<HeaderStyle Width="100" BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:CheckBox ID="chkbox" runat="server" onclick="javascript:HighlightRow(this);" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="BtnRegister" runat="server" class="btn btn-info" Text="Delete" OnClick="BtnLogin_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
And here's my code behind.
protected void showData()
{
SqlDataAdapter da = new SqlDataAdapter("select * from Drug", con);
DataSet ds = new DataSet();
da.Fill(ds);
GvGrade.DataSource = ds;
GvGrade.DataBind();
}
protected void DeleteRecord(string drug_name)
{
SqlConnection con = new SqlConnection(cs);
SqlCommand com = new SqlCommand("delete from Drug where drug_name=@ID", con);
com.Parameters.AddWithValue("@ID", drug_name);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
protected void BtnLogin_Click(object sender, EventArgs e)
{
foreach (GridViewRow grow in GvGrade.Rows)
{
CheckBox chkbox = (CheckBox)grow.FindControl("chkbox");
if (chkbox.Checked)
{
string drug_name = grow.Cells[1].Text;
DeleteRecord(drug_name);
showData();
}
}
//Displaying the Data in GridView
showData();
}