Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everyone.

My current delete code used to work in a Gridview table but no longer, after I stuffed it into an UpdatePanel.

Basically, users will select one or multiple rows with a checkbox and then the selected rows will be deleted upon clicking a button.

This is my aspx code;
<asp:UpdatePanel ID="upl" runat="server">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnSearch" EventName="serverclick" />
        <asp:AsyncPostBackTrigger ControlID="GvGrade" />
    </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>
    </ContentTemplate>
</asp:UpdatePanel>


And the 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);
        }
    }
    //Displaying the Data in GridView
    showData();
}


What I have tried:

I had googled around and read something about re-binding the table after button postback but I don't think that is the issue. I have also tried updating the panel in the code-behind with upl.Update but it still doesn't work. What is happening here?
Posted
Updated 30-Jun-17 21:10pm
Comments
Karthik_Mahalingam 30-Jun-17 22:30pm    
where is the delete button?
LuciusAvette 30-Jun-17 23:05pm    
My bad. The delete button is placed outside the update panel.


<!-- Button -->

<asp:Button ID="BtnRegister" runat="server" class="btn btn-info" Text="Delete" OnClick="BtnLogin_Click" />

Karthik_Mahalingam 30-Jun-17 23:06pm    
place it inside and try
LuciusAvette 30-Jun-17 23:15pm    
Hi Karthik, placing it inside doesn't work.

Even if I create a new column on purpose with

<asp:PostBackTrigger ControlID="BtnRegister" />

it shows an error " A control with ID 'BtnRegister' could not be found for the trigger in UpdatePanel 'upl'.".
Karthik_Mahalingam 30-Jun-17 23:18pm    
place it inside ContentTemplate

1 solution

Fixed.

Seems like my code behind for "DeleteRecord" just dislikes "TemplateField" and prefers "BoundField".
 
Share this answer
 

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