用gridview需要注意的几点问题

原创文章,转载请注明出处.转载自: Li Haifeng's Blog
本文链接地址: 用gridview需要注意的几点问题

You can take advantage of the <%@ Page %> directive’s MaintainScrollPositionOnPostback attribute to scroll a page back automatically to the same position whenever the page is posted back to the server. For example, if you add this attribute and click an Edit link rendered by a GridView, the page automatically scrolls to the record being edited. This attribute works with Internet Explorer 6+, Firefox 1+, and Opera 8+.



When you click an Edit link, you can edit a particular database row. The GridView automatically renders a check box for any Boolean columns and a text field for any other type of column.



The GridView control does not support inserting new records into a database table. If you need to insert new records, use either the DetailsView or FormView control.




Furthermore, notice that the GridView control includes a DataKeyNames property. When editing and deleting rows with the GridView, you need to assign the name of the primary key field from the database table being modified to this property. In Listing 11.13, the Movies ID column is assigned to the DataKeyNames property.


Finally, notice that the SqlDataSource control associated with the GridView control includes a SelectCommand, UpdateCommand, and DeleteCommand property. These properties contain the SQL statements that are executed when you display, insert, and delete records with the GridView control.


The SQL statements contained in both the UpdateCommand and DeleteCommand include parameters. For example, the UpdateCommand looks like this:



UPDATE Movies SET Title=@Title, Director=@Director
WHERE Id=@Id




The @Title and @Director parameters represent the new values for these columns that a user enters when updating a record with the GridView control. The @Id parameter represents the primary key column from the database table.

From Li Haifeng's Blog, post 用gridview需要注意的几点问题

Post Footer automatically generated by wp-posturl plugin for wordpress.

分享到: