Asp.net GridView handwriting event, including taking the primary key, value, update, select, delete

Source: Internet
Author: User

Just now, on the page for adjusting the link management page, there is a simple GridView. Because the architecture is changed, you need to manually edit, delete, and other events for the GridView. Recently, I was often asked a question about the GridView, so I wrote an experience book to serve as a warning for future generations.

The image is the link management page of the website background:
 
 

The code of the first two is relatively fixed, generally:

 
The code is as follows: Copy code
Protected void gvFriendLink_RowEditing (object sender, GridViewEditEventArgs e)
{
GvFriendLink. EditIndex = e. NewEditIndex;
FetchData ();
}

Protected void gvFriendLink_RowCancelingEdit (object sender, GridViewCancelEditEventArgs e)
{
GvFriendLink. EditIndex =-1;
FetchData ();
}
FetchData () is used to bind data to the GridView. EditIndex =-1 means to exit the editing mode.

2. Value issues in RowUpdating events

2.1 cannot get value

It is useless to directly access the GridView. Row [I]. Cell [j]. Text. In the editing mode, the Cell actually has controls. In this example, it is a TextBox. So we need to force type conversion:

 
The code is as follows: Copy code
Protected void gvFriendLink_RowUpdating (object sender, GridViewUpdateEventArgs e)
{
GeekStudio. ORM. Model. FriendLink model = new GeekStudio. ORM. Model. FriendLink ()
{
Id = Convert. ToInt32 (TextBox) gvFriendLink. Rows [gvFriendLink. EditIndex]. Cells [1]. Controls [0]). Text ),
Title = (TextBox) gvFriendLink. Rows [gvFriendLink. EditIndex]. Cells [2]. Controls [0]). Text,
Url = (TextBox) gvFriendLink. Rows [gvFriendLink. EditIndex]. Cells [3]. Controls [0]). Text,
OrderId = Convert. ToInt32 (TextBox) gvFriendLink. Rows [gvFriendLink. EditIndex]. Cells [4]. Controls [0]). Text)
};

OptFriendLink. Update (model );

GvFriendLink. EditIndex =-1;
FetchData ();
}
2.2 cannot get new value

If you enter a new value when editing the GridView, but it remains unchanged after the update, for example, abc is in the cell, and abcd is written during editing to go to the update event, the obtained value is abc. At this time, you need to check whether you forget to judge the page PostBack?

Solution: write the data binding method in if (! Page. IsPostBack)

 
The code is as follows: Copy code
Protected void Page_Load (object sender, EventArgs e)
{
If (! Page. IsPostBack)
{
FetchData ();
}
}

Protected void FetchData ()
{
GvFriendLink. DataSource = optFriendLink. GetModelList (0 );
GvFriendLink. DataBind ();
}
3. Handwritten deletion event

To delete a RowDeleting event, you only need to write the RowDeleting event:

 
The code is as follows: Copy code
Protected void gvFriendLink_RowDeleting (object sender, GridViewDeleteEventArgs e)
{
Int id = Convert. ToInt32 (gvFriendLink. Rows [e. RowIndex]. Cells [1]. Text );
OptFriendLink. Delete (id );
FetchData ();
}
4. How to obtain the primary key

Careful kids shoes will find that, in the delete event just now, the method for getting the primary key is very silly, actually directly accessing Cells [1], that is, the value of the second cell. However, in many cases, the project requires that the primary key field in the database cannot be displayed on the GridView. What should I do?

In fact, the property of the primary key accessed by the GridView is called DataKey.

To use this attribute, you must first specify a DataKeyName for the GridView.

Then you can access the primary key of a row in the code:

 
The code is as follows: Copy code
Int id = Convert. ToInt32 (gvFriendLink. DataKeys [e. RowIndex]. Value );
5. Select a row in the GridView

My friendship link module does not have this requirement, so I will post an example in the previous course selection system:

 
The code is as follows: Copy code
Protected void gvCourses_SelectedIndexChanged (object sender, EventArgs e)
{
Int userId = uid;
Int courseId = Convert. ToInt32 (gvCourses. SelectedRow. Cells [0]. Text );
DalUca. Add (new Course. Model. UserCourseAssociation () {UserId = userId, CourseId = courseId });
FetchAllCourse ();
FetchUserCourse (userId );
}
It is actually a SelectedIndexChanged event. But the premise is that you must have a button in the GridView to trigger this event:

It is generally a Select Command:

 
The code is as follows: Copy code
<Asp: CommandField ShowSelectButton = "True"/>
Related Article

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.