At the beginning, we drag a dview control to the. aspx page, and then bind the column you want to display. The Code is as follows.
Copy codeThe Code is as follows:
<Asp: GridView ID = "gvDepartList" runat = "server" AutoGenerateColumns = "False"
Height = "pixel PX" Width = "600px" OnRowDeleting = "gv1_list_rowdeleting" RowDataBound = "gv1_list_rowdataround">
<Columns>
<Asp: TemplateField HeaderText = "department name">
<ItemTemplate>
<Asp: Label runat = "server" style = "text-align: center" Text = '<% # Eval ("audio name") %>'/>
</ItemTemplate>
</Asp: TemplateField>
<Asp: BoundField HeaderText = "institution" DataField = "BranchId"/>
<Asp: BoundField HeaderText = "owner" DataField = "PrincipalUser"/>
<Asp: BoundField HeaderText = "contact number" DataField = "ConnectTelNo"/>
<Asp: BoundField HeaderText = "Mobile Phone" DataField = "connectmobimobiletelno"/>
<Asp: BoundField HeaderText = "fax" DataField = "Faxes"/>
<Asp: TemplateField HeaderText = "modify">
<ItemTemplate>
<Asp: ImageButton ID = "ImageButton1" ImageUrl = ".. /images/edit.gif "CommandArgument = '<% # Eval (" DepartId ") %> 'commandname =" delete "runat =" server "/>
</ItemTemplate>
</Asp: TemplateField>
<Asp: TemplateField HeaderText = "delete">
<ItemTemplate>
<Asp: ImageButton ImageUrl = ".. /images/delete.gif "CommandArgument = '<% # Eval (" DepartId ") %> 'commandname =" delete "runat =" server "/>
</ItemTemplate>
</Asp: TemplateField>
</Columns>
</Asp: GridView>
2. Bind data to the Page_load event in the background of this. aspx page.
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
GvDepartList. DataSource = new DepartInfoManager (). GetDepartInfos (-1 );
GvDepartList. DataBind ();
}
}
If we want to add a dview with the effect of the light bar, the background color is changed when each row of the mouse is suspended.
Copy codeThe Code is as follows:
/// <Summary>
/// Dynamically register the script (before the GridView control is displayed ).
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Protected void gvUsers_RowDataBound (object sender, GridViewRowEventArgs e)
{
// The Script registration is performed only on the Data row.
If (e. Row. RowType = DataControlRowType. DataRow)
{
// Beam Effect
E. Row. Attributes. Add ("onmouseover", "currentcolor = this. style. backgroundColor; this. style. backgroundColor = '# 6699ff '");
E. Row. Attributes. Add ("onmouseout", "this. style. backgroundColor = currentcolor ");
LinkButton lnkbtnDel = e. Row. FindControl ("lnkbtnDel") as LinkButton;
LnkbtnDel. Attributes. Add ("onclick", "return confirm ('Are you sure you want to delete it? ')");
}
}
Now the point is, how is a row of data? Since it is delete, We must delete it based on the ID of a piece of data, so we add a piece of code in the Page_load method:
GvDepartList. DataKeyNames = new string [] {"id"}; // What does this code mean? It sets a key for each row, which is used to operate data.
Now we can use another method to delete it. We can see the penultimate column on the page. Yes, it's an ImageButtom control. This control has a small icon for the delete button. What does CommandArgument do? What is CommandName? CommandArgument specifies the parameter to be operated, and CommandName indicates the command button? Here we use Delete. We will write Delete.
Copy codeThe Code is as follows:
<Asp: TemplateField HeaderText = "delete">
<ItemTemplate>
<Asp: ImageButton ImageUrl = ".. /images/delete.gif "CommandArgument = '<% # Eval (" DepartId ") %> 'commandname =" delete "runat =" server "/>
</ItemTemplate>
</Asp: TemplateField>
The following is the background operation code. You can see that the DataGridView is bound to an OnRowDeleting event, which is used to delete the event.
Then we will write such code in this event.
Copy codeThe Code is as follows:
/// <Summary>
/// Delete the selected row
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Protected void gv1_list_rowdeleting (object sender, GridViewDeleteEventArgs e)
{
ImageButton buttom = gvDepartList. Rows [e. RowIndex]. FindControl ("btnDelete") as ImageButton;
String departId = buttom. CommandArgument. ToString ();
If (manage. DeleteDepart (departId ))
{
Page. ClientScript. RegisterClientScriptBlock (this. GetType (), "alert", "<script> alert ('deleted successfully! '); </Script> ");
BindDepartInfos (); // rebind data
}
Else
{
Page. ClientScript. RegisterClientScriptBlock (this. GetType (), "alert", "<script> alert ('deletion failed! '); </Script> ");
}
}
For better user experience, we can not use this Page. ClientScript. RegisterClientScriptBlock (this. GetType (), "alert", "<script> alert ('deleted successfully! '); </Script> ");
You can choose to place a label control in the Visible area on the page, design Visible = false; hide it, and then use this Label control to prompt the user, delete successfully!