Do your own blog log management-5.2 Management log classification

Source: Internet
Author: User
Tags first row

As you can see in Figure 1-14, the first one to manage is the log classification, which is what this section describes. It is done using the Ucadmin_logcategory.ascx user control, as shown in Figure 1-1, which is the final layout of the control.

Figure 1-1 Management Log Classification user control layout

As shown in Figure 1-1, the GridView control in the asp.net used here when binding displays the log classification data, rather than the repeater control. The GridView control, also known as a grid view control, can render multiple-column, fully templated tables that are more powerful than repeater controls. By using the GridView control, users can display, edit, and delete data from a variety of different data sources, such as databases, XML files, and business objects that expose data.

You can use the GridView to do the following:

L automatically bind and display data through data source controls

L Select, Sort, page, edit, and delete data from a data source control

L Specify custom columns and styles

• Create custom user interface (UI) elements using templates

• Add your own code to the capabilities of the GridView control by handling events

The GridView control supports a variety of binding column types, such as BoundField, ButtonField, HyperLinkField, CommandField, and TemplateField. The most commonly used are BoundField and CommandField.

The BoundField type displays a column of a field bound to the data source, and the result is in plain text: When you bind the data for that column type, you need to use the property associated with that data. Common important properties for this column type are as follows:

L DATAFIELD The field name of the bound data column in the data source

L dataformatstring Display the format of column data

L HEADERTEXT The title of the column

L FOOTERTEXT The footnote section of the column

L READONLY identifies whether the column is to be edited

L SORTEXPRESSION The column's sort expression

The CommandField type shows the various editing function buttons built into the GridView. such as "edit", "delete" and so on, by default they appear in the form of LinkButton. The most important properties for this column type are as follows:

L ButtonType Set the type of button

L CANCLETEXT The display text of the Cancel button

L DELETETEXT The display text of the Delete button

L edittext Edit button display text

L ShowEditButton Whether the Edit button is displayed

L Showcanclebutton whether to show the Cancel button

L Showdeletebutton whether to show the delete button

The Add log category in the layout shown in Figure 1-1 uses a text box control, a button, and a validation control.

1. View Log Categories

Viewing the log category is the reading of data from the Infosort table and binding to the GridView control display, which first requires understanding the layout of the GridView control with the following code:

<asp:gridview id= "GridView1" runat= "server" backcolor= "white" bordercolor= "#999999"

Borderstyle= "None" borderwidth= "1px" cellpadding= "3" gridlines= "Vertical"

onrowcancelingedit= "Gridview1_rowcancelingedit"

onrowdeleting= "gridview1_rowdeleting" onrowediting= "gridview1_rowediting"

onrowupdating= "gridview1_rowupdating" showfooter= "True"

Width= "500px" autogeneratecolumns= "false" Enableviewstate= "false" >

<footerstyle backcolor= "#CCCCCC" forecolor= "Black"/>

<rowstyle backcolor= "#EEEEEE" forecolor= "Black"/>

<selectedrowstyle backcolor= "#008A8C" font-bold= "True" forecolor= "white"/>

<pagerstyle backcolor= "#999999" forecolor= "Black" horizontalalign= "Center"/>

<alternatingrowstyle backcolor= "Gainsboro"/>

<Columns>

<asp:boundfield datafield= "id" headertext= "number" readonly= "True"/>

<asp:boundfield datafield= "Sortname" headertext= "category name"/>

<asp:boundfield datafield= "sortdate" headertext= "Update Date" readonly= "True"/>

<asp:commandfield showeditbutton= "True"/>

<asp:commandfield buttontype= "Image" deleteimageurl= "~/images/a_delete.gif" showdeletebutton= "True"/>

</Columns>

</asp:GridView>

The columns shown in the above code <Columns> and </Columns> for layout appearance, where the first row specifies a column that displays number, the data used for the column is the bound ID column, and the last ReadOnly property sets the column to read-only.

After you know the layout, the following code is written, the GridView control has the same binding method as the Repeater control, and the code is as follows:

protected void Page_Load (object sender, EventArgs e)

{

Gbind ();

}

void Gbind ()

{

Dbconfig dbconn = new Dbconfig ();

Gridview1.datasource = Dbconn. Createsource ("SELECT * from Infosort");

Gridview1.databind ();

Dbconn. Clear ();

}

Notice here that the Createsource () method in the database generic class is used for the first time in your code.

2. Modify the log category

Execute the code above, and then click the Edit link in a row, and the editable cells in that row will be rendered as text boxes. At this point, modify the data in the text box, click the Update link to save the changes, and exit the Edit status if you click the Cancel link, as shown in Figure 1-16.

Figure 1-16 Modifying the log category

The GridView control's edit, update, and cancel links depend on the Onrowcancelingedit, Onrowediting, and onrowupdating properties of the control, which we find in the layout code above. Their property values correspond to one method in the background, and are the event model mechanisms that use the GridView.

The "edit" link corresponds to the gridview1_rowediting () method code as follows:

protected void Gridview1_rowediting (object sender, GridViewEditEventArgs e)

{

Gridview1.editindex = E.neweditindex;

Gbind (); To rebind a control's data source

}

The Editindex property in code can set or get the index of the row to be edited, where you specify the row in which the edited behavior is clicked, so that it is in an edited state, which is important and will be used many times later.

The "Cancel" link corresponds to the Gridview1_rowcancelingedit () method code as follows:

protected void Gridview1_rowcancelingedit (object sender, Gridviewcancelediteventargs e)

{

Gridview1.editindex =-1;

Gbind (); To rebind a control's data source

}

The most important is the code for the "Update" link corresponding to the gridview1_rowupdating () method, as follows:

protected void Gridview1_rowupdating (object sender, Gridviewupdateeventargs e)

{

Gridview1.editindex = E.rowindex;

Get the row to update

GridViewRow GVR = this. Gridview1.rows[e.rowindex];

Gets the new taxonomy name in the row

TextBox txt = (textbox) Gvr. CELLS[1]. Controls[0];

Get the updated category number

String UpdateID = Gvr. Cells[0]. Text;

String strSQL = "Update infosort set sortname= '" + txt. Text.trim () + "', sortdate= '" + DateTime.Now.ToString () + "' where id=" + UpdateID;

Dbconfig dbconn = new Dbconfig ();

Dbconn. ExecuteNonQuery (strSQL);

Dbconn. Clear ();

Response.Redirect ("admin_index.aspx");

}

Updating the Sortname field in the Infosort table updates the sortdate as the current date, and finally to the System Management home page.

3. Delete Log category

The delete link corresponds to the Onrowdeleting property of the GridView control, that is, the gridview1_rowdeleting () method, which executes the process to get the category number of the row in which it is located, and then executes the statement and returns, as follows:

protected void Gridview1_rowdeleting (object sender, Gridviewdeleteeventargs e)

{

GridViewRow GVR = (gridviewrow) gridview1.controls[0]. Controls[e.rowindex + 1];

String delid = Gvr. Cells[0]. Text;

Dbconfig dbconn = new Dbconfig ();

Dbconn. ExecuteNonQuery ("Delete from Infosort where id=" + delid);

Dbconn. Clear ();

Response.Redirect ("admin_index.aspx");

}

4. Add log Category

Add a log category at the bottom of the layout in Figure 1-1, simply enter a name in the text box and click OK to complete it. The code needs to be written in the Click event of the OK button, the name of the button is Btnok, and the click Code looks like this:

protected void Btnok_click (object sender, EventArgs e)

{

Dbconfig dbconn = new Dbconfig ();

String strsql= "INSERT into Infosort (sortname) VALUES (' +txtlog.text.trim () + ')";

Dbconn. ExecuteNonQuery (strSQL);

Dbconn. Clear ();

Response.Redirect ("admin_index.aspx");

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.