First, we drag a DataGridView control into the .aspx page, and then bind the column you need to display. The specific code is as follows.
<asp:GridView ID="gvDepartList" runat="server" AutoGenerateColumns="False"
Height="108px" Width="600px" OnRowDeleting="gvDepartList_RowDeleting" RowDataBound="gvDepartList_RowDataRound">
<Columns>
<asp:TemplateField HeaderText="Department Name" >
<ItemTemplate>
<asp:Label runat="server" style="text-align:center" Text='<%# Eval("DepartName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Agency" DataField="BranchId" />
<asp:BoundField HeaderText="Header in charge" DataField="PrincipalUser" />
<asp:BoundField HeaderText="Contact Number" DataField="ConnectTelNo" />
<asp:BoundField HeaderText="Mobile Phone" DataField="ConnectMobileTelNo"/>
<asp:BoundField HeaderText="Fax" DataField="Faxes" />
<asp:TemplateField HeaderText="Modify">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" ImageUrl="../images/" CommandArgument='<%#Eval("DepartId") %>' CommandName="delete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:ImageButton ImageUrl="../images/" CommandArgument='<%#Eval("DepartId") %>' CommandName="delete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
2: Bind data in the Page_load event in the background of this .aspx page.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
= new DepartInfoManager().GetDepartInfos(-1);
();
}
}
If we want to add a DataGridView light bar effect, we will change the background color of each row of mouse.
/// <summary>
/// Dynamic registration script (before the GridView control renders) Light stick effect
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
//This is to determine that script registration is only performed on the data line
if ( == )
{
//The light stick effect
("onmouseover","currentcolor=;='#6699ff'");
("onmouseout ", "=currentcolor");
LinkButton lnkbtnDel = ("lnkbtnDel") as LinkButton;
("onclick", "return confirm('Are you sure to delete?')");
}
}
Now the key point is coming, what is the data in a row? Since it is deletion, we must delete it based on the ID of a data. Then we add a piece of code to the Page_load method:
= new string[] { "id"};//What does this code mean? It means that a key is set for each line, and this key is used to manipulate data.
Now we use another method to delete it and see the penultimate column in the page. Yes, it is an ImageButtom control. This control puts a small icon of the delete button. What does CommandArgument do? What does CommandName do? CommandArgument specifies the parameters we want to operate, and CommandName is what command this button is to do? What is used here is deletion, let's write Delete.
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:ImageButton ImageUrl="../images/" CommandArgument='<%#Eval("DepartId") %>' CommandName="delete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
Next is the background operation code. You can see that this DataGridView is bound to an OnRowDeleting event, which is used to delete.
Then we write such code on this event.
/// <summary>
/// Delete the selected row
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvDepartList_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
ImageButton buttom = [].FindControl("btnDelete") as ImageButton;
string departId = ();
if ((departId))
{
((), "alert", "<script>alert('Delete Successfully!');</script>");
BindDepartInfos();//Rebound data
}
else
{
((), "alert", "<script>alert('Deletion failed!');</script>");
}
}
For a better user experience, we can not use this ((), "alert", "<script>alert('Delete Successfully!');</script>");
You can choose to place a label control in a conspicuous place on the page, design Visible=false; hide it, and then after the deletion is successful, use this Label control to prompt the user to delete it successfully!