The GridView control implements data modification (Section 9th) _ Practical Tips

Source: Internet
Author: User

The previous article is to use the GridView control to achieve the display and deletion of data, this section of the design page, using the GridView control to implement the data update operation, when the news content is not entered when the update prompts "cannot be empty."
Learning content:
Learning Tasks one uses the GridView control to implement updates to information
Step one: The data source control defines the data by writing the code in the Newschuli.cs page as follows:

public static void Modifynews (Newsclass news1)
  {
   using (SqlConnection cn = dbconn. Getconnection ())
    {
     CN. Open ();
     SqlCommand cm = new SqlCommand ();
     Cm. Connection = cn;
    String sql = "Update news set title = @content, contents= @as where id= @id";
        Cm.commandtext = SQL;
        Cm. Parameters.addwithvalue ("@content", news1. Title);
        Cm. Parameters.addwithvalue ("@as", news1. Contents);
        Cm. Parameters.addwithvalue ("@id", news1.id);
        Cm. ExecuteNonQuery ();
     }
   

Step two: The ObjectDataSource control gets the data from the data source, selects the Update tab, sets the Select method to Modifynews (Newsclass news1), and clicks Next.
Step three: Set the DataKeyNames property to the ID.
Step four: Add edit columns and delete columns. Open the GridView Task menu, select the Edit Columns command to open the Fields dialog box, expand the + before commandfired in the Available fields list in the upper left, select Edit, Update, cancel, and delete, and click OK.
Learning Task Two sets the template column.
instance One: input non-null validation
STEP1: Open the Fields dialog box and select the News headlines option. "Convert this segment to templatefired" hyperlink to a template column at the bottom right of the stand-alone machine.
STEP2: Open the GridView Task menu, select Edit Template entry, set a data validation control Requiredfiredvalidator to the EditItemTemplate template entry, and implement Non-null validation.
Example two: Add a dialog box to confirm deletion before deleting
STEP1: Opens the Fields dialog box and selects the delete option. Click the "Convert this segment to templatefired" hyperlink in the lower-right section to convert it to a template column.
STEP2: Add a client event OnClientClick, write JavaScript script: onclientclick= "return confirm (' Confirm to delete ')"

Run Effect chart:

The above is the entire content of this article, I hope you will be proficient in using the GridView control to achieve data modification has helped.

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.