Data Update in custom Columns

Source: Internet
Author: User

Data Update in custom Columns

Assume that the database has a "permission" field. If the value is 0, it indicates that the user is not audited. If the value is 1, it indicates that the user is normal. If the value is 9, it indicates the administrator user. According to the preceding method of customizing columns, by binding droplistdown, the permission is displayed as "Administrator" on the webpage instead of the number 9. The problem arises. If we adjust the user permissions, for example, change the general user to the Administrator. In the drop-down list of the user permissions in the editing template, how to return its value to the data source to complete the update operation.

The droplistdown control we set in edititemtemplate must select two way databinding, that is, two-way help of data, in order to return data. As mentioned above, in the gridview, events are not single, but two. One is before and the other is after, because we need to transfer the permission value of the drop-down list before data update, We need to encode gridview1_rowupdating, the encoding is as follows:

Protected void gridview1_rowupdating (Object sender, gridviewupdateeventargs E)
{
// Which line is being edited?
Int Index = gridview1.editindex;
// Obtain the gridviewrow object of the currently edited row
Gridviewrow gvr = gridview1.rows [Index];
// Search for the droplistdown control in the current row
Dropdownlist dp = (dropdownlist) gvr. findcontrol ("editdrop ");
/Assign the droplistdown value to the permission field in the newvalues set.

E. newvalues ["rights"] = DP. selectedvalue;

}

 

2. rowdatabound event

When creating a gridview control, you must first create a gridviewrow object for each row of the gridview. When creating each row, A rowcreated event is triggered. When the row is created, each row of gridviewrow is bound to the data in the data source. When the binding is complete, the rowdatabound event is triggered. If we can use the rowcreated event to control the controls bound to each row, we can also use the rowdatabound event to control the data bound to each row, that is, how to present the data to everyone.

For example, there is a gender column in the data table. The databounding of the droplistdown control is used to indicate the gender in Chinese, but it is not very beautiful after all, now we can use the label control and rowdatabound event to achieve a perfect Chinese gender display. Rowdatabound,

First, set the Gender column as the template column, add a label control, and bind the label control to the gender segment of the data source, double-click rowdatabound In the event list of the gridview control property to generate the following event:

Example:

Protected void gridview1_rowdatabound (Object sender, gridviewroweventargs E)

{

// Determine whether the current row is a data row

If (E. Row. rowtype = datacontrolrowtype. datarow)

{// Use findcontrol to find the label control in the template

Label lB1 = (Label) E. Row. findcontrol ("label1 ");

// Because rowdatabound occurs after data binding, we can

// Determine the data bound to the label. If it is true, change its text attribute to male.

If (lb1.text = "true ")

Lb1.text = "male ";

Else

Lb1.text = "female ";

}

}

 

3. rowtype

Rowtype can be used to determine the type of the row in the gridview. rowtype is a value in datacontrolrowtype. Rowtype can be set to datarow, footer, header, emptydatarow, pager, and separator. Most of the time, we need to determine whether the current row is a data row and use the following code to determine whether the current row is a data row:

If (E. Row. rowtype = datacontrolrowtype. datarow)

 

4. rowdeleting and rowdeleted events

Rowdeleting occurs before data is deleted, and rowdeleted occurs after data is deleted.

With the rowdeleting event, you can confirm whether to delete it again before deleting it. You can set gridviewdeleteeventargs. cancel = true to cancel the deletion. It can also be used to determine the number of records in the current database. If there is only one record left and the database cannot be blank, a prompt is displayed and the deletion operation is canceled.

When you use the rowdeleted event, you can use the exception attribute of gridviewdeletedeventargs to determine whether an exception occurs during the deletion process. If no exception exists, a message similar to "1 records deleted" can be displayed.

Example:

Protected void gridview1_rowdeleting (Object sender, gridviewdeleteeventargs E)

{

// Obtain the current row number and obtain the gridviewrow object of the current row

Int Index = E. rowindex;

Gridviewrow gvr = gridview1.rows [Index];

// Obtain the text in the second cell of the current row

Str1 = gvr. cells [1]. text;

// Prompt

Message. Text = "you will delete a user whose name is" + str1;

}

Protected void gridviewinclurowdeleted (Object sender, gridviewdeletedeventargs E)

{

// If no exception occurs, a message is displayed, indicating that the deletion is successful. Otherwise, a message is displayed, indicating that the deletion failed.

If (E. Exception = NULL)

Message. Text + = "<br> you have successfully deleted" + str1;

Else

Message. Text + = "deletion failed. Please contact the Administrator ";

}

5. rowediting event

The rowediting event is triggered before the row in the gridview enters the editing mode. If you need to perform some preprocessing before editing the record, you can perform operations here. To cancel editing the current row, set the cancel attribute of the gridviewediteventargs object to true.

Example:

Protected void gridviewinclurowediting (Object sender, gridviewediteventargs E)

{

// Use neweidindex to get the row number currently edited, and then obtain the gridviewrow object

Gridviewrow gvr = gridview1.rows [E. neweditindex];

 

// Judge. If the name column of the current editing row is admin, the editing of the current row is canceled.

If (gvr. cells [1]. Text = "admin ")

E. Cancel = true;

}

6. rowupdating and rowupdated events

The rowupdating event occurs before the data source is updated, and the rowupdated event occurs after the data source is updated.

We can use rowupdating for preprocessing before record updates. For example, when changing a password, the password is hashed because it is not stored in plain text in the database. Therefore, before updating the password, the hash value should be generated before the update operation. Rowupdated checks whether the update is successful.

Example:

Protected void gridview1_rowupdating (Object sender, gridviewupdateeventargs E)

{

Gridviewrow gvr = gridview1.rows [gridview1. editindex];

// Find the control for entering the password

Textbox tb1 = (textbox) gvr. findcontrol ("tb_password ");

// After the text in the control is hashed, the password is saved to the newvalues dictionary.

E. newvalues ["password"] = tb1.text. gethashcode (). tostring ();

 

}

Protected void gridviewinclurowupdated (Object sender, gridviewupdatedeventargs E)

{

// If no exception occurs, the update is successful.

If (E. Exception = NULL)

Message. Text + = "updated successfully! ";

}

 

7. Keys, oldvalues, and newvalues

Each item in these three sets is a dictionaryentry type object. We can use dictionaryentry. Key to determine the field name of an item, and use dictionaryentry. Value to determine the value of an item.

In the preceding example, we used the newvalues field to reset the value of key to password in order to encrypt the plaintext of the password and store it in the database. To ensure security, we encode all values in newvalues in HTML before updating the data:

Example1:

Protected void gridview1_rowupdating (Object sender, gridviewupdateeventargs E)

{

// Traverse newvalues and obtain each pair of dictionaryentry objects

Foreach (dictionaryentry de in e. newvalues)

 

// De. Key is the field name. If you update a field separately, you can enter the field name directly. // E. newvalues ["password"]

 

E. newvalues [de. Key] = server. htmlencode (De. value. tostring ());

}

 

Example2:

Protected void gridview1_rowupdating (Object sender, gridviewupdateeventargs E)

{

// Use keys, oldvalues, and newvalues to obtain the primary key name, original data, and updated data respectively.

Message. TEXT = E. keys ["username"] + "email address from" + E. oldvalues ["email"] + "changed to" + E. newvalues ["email"];

}

 

 

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.