Editing (mainly exercise) + detailsview editing and insertion in the gridview

Source: Internet
Author: User

I. Quick review of gridview Editing :(Entry: View graphical quick review 1)

Quick review 2

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> 1. delegate: for example, when a click event of linkbutton_1 is triggered, the "Click delegate" of linkbutton_1 executes the event corresponding to this delegate.
2. why is it invalid when editing detailsview (Specific Code )
in my code: the default text box in the edittemplate of the editing template of ID has been deleted and the tag is used. There is no error in this step.
the tag is subject to a single-item binding (eval ), in this way, the ID cannot be obtained during the update, so bind is OK.
3. Why can dropdownlist. selectedvalue be bound to the field role. roleid during editing?
answer: because a row of data is transmitted from the gridview during click editing, You can bind the data.
however, when a vertex is added, it is not bound to any data, so (eval ("role. roleid ") cannot get any value,
so use role. an error occurs when the roleid is bound to the selectedvalue of the dropdownlist (which has already been bound to a data source).

1. When a parameter in the object class admin is of the Role type, edit the gridview
1. (displayed when you click Edit) convert the role field of the gridview to template edit. Add a dropdownlist (the data source is bound to the role) to the edititemtemplate (the databindings and role are edited. roleid binding (eval ("role. roleid "))
2. (insert when updating) in the objectperformance_admin data source ( The one bound to the gridview ) In the updating event
The value of dropdownlist. selectedvalue is given to the roleid (because a roleid parameter is required during update). The specific code is as follows:
Gridviewrow ROW = gridview1.rows [gridview1.editindex];
Dropdownlist DDL = row. findcontrol ("dropdownlist1") as dropdownlist;
E. inputparameters ["roleid"] = DDL. selectedvalue;
Detailsview Edit Time Code: In the objectperformance_admin data source ( The one bound to detailsview ).
Dropdownlist ddl_role = detailsview1.findcontrol ("dropdownlist2") as dropdownlist;
E. inputparameters ["roleid"] = ddl_role.selectedvalue;
2. In the gridview + detailsview combination, the former is displayed, the latter is edited and added
1. Join the gridview and detailsview:
1. Pay attention to setting datakeynames (Data primary key) of the gridview)
2. Select getadminbyid (int id) when selecting the data source, and select the gridview Control for parameter source configuration.
2. the editing and addition of the detailsview are the same as those of the gridview,
1. You need to re-bind the gridview (GV. databind () after updated ())
2. When adding a new table, dropdownlist cannot be bound to the table field, and neither eval ("role. roleid ")

** 3. quick graphical review:
Gridview + detailsview, the former is displayed, the latter is edited and added

4. Problems:
1. When adding a new value, the system always prompts that the selectedvalue value of dropdownlist is invalid.
Solution: dropdownlist. selectedvalue is not bound to databindings.
2. When the inserting event is added, after the updating event is reused, the new event is available, but the Edit (modification) is invalid (modified after being reminded by Instructor Dong)
Root cause: the default text box in the edittemplate of the ID editing template has been deleted and the label is used. There is no error at this step, mainly because
The tag uses a single-item binding (eval). In this way, the ID cannot be obtained during the update, so it is okay to use bind.

Once The reason
1: Cause of speculation: because there is a piece of code in this event, get the dropdownlist through reflection (the strings are all dropdownlist2)
That is to say: if it is in the update delegate, you can find the dropdownlist2,
If you are adding a delegate, you will find the dropdownlist2 inserted to the template,
However, although the Code is reused, it only delegates new (overwrite the modified delegate) and does not delegate edit (modify), so the edit is invalid.
Protected void objectdatasource_details_updating (Object sender, objectinclucemethodeventargs E)
{
Dropdownlist ddl_role = detailsview1.findcontrol ("dropdownlist2") as dropdownlist;
E. inputparameters ["roleid"] = ddl_role.selectedvalue;
}


Delegate: for example, when a click event of linkbutton_1 is triggered, the "Click delegate" of linkbutton_1 executes the event corresponding to this delegate.
Answer: The above guess is incorrect because I do not know the Asp.net delegation. I will first describe how to edit the detailsview (in two steps, click Edit and click Update)
1. Click Edit: the controls in the edittemplate are automatically bound to the field (such as eval or bind)
2. Click Update: it is actually a linkbutton. When you click linkbutton_update ("Update" on the Interface), linkbutton_update
Click delegate to execute the event corresponding to the Delegate (of course, this delegate can define multiple events, such as updating updated .)
3. Update and insert are two different linkbuttons, so the two of them have different "Click delegates", and the events subscribed to for their delegates are not
Similarly, the linkbutton_update delegate subscribes to a series of events (such as updating updated events ),
The linkbutton_insert delegate subscribes to a series of events (such as inserting inserted events ).
Mistaken 2: found the cause, but the solution is not the real problem!
The update method requires an ID during editing, but an ID is directly added to the database during insertion. The ID can be obtained by @ identity of the ID.
Therefore, you do not need to specify an ID. Therefore, you do not obtain an ID (pair) during the update. This ID is interpreted as setting the detailsview
Datakeynames is ID (incorrect)
3. Correct cause: when updating the retrieved ID, set this ID as eval binding, so it can only be read and cannot be written
4. Some knowledge associated with this problem:
1. There are two steps to modify the modification (click Edit first and then click Update)
1. Click Edit:
The edittemplate text box of a field in detailsview is bound to the table field by default (for example, BIND ("ID "))
2. Click Update: in fact, all fields should be parameters, including IDs. Therefore, if a field is edited using a template, it must be in the edittemplate.
Use two-way binding. If you use single-item binding (for example, dropdownlist exercise), you must write code in the updating of the data source bound to dropdownlist.
2. Add: ID can be ignored during insertion, because ID is not a parameter during insertion (the reason why ID has a value is because the database grows itself)
3. Solution: After learning about the above, you will find the problem. In the code, the edittemplate of the ID editing template binds the default text box.
Deleted, a tag is used, and it is bound to eval. Therefore, when you click Edit, you can see the effect. But when you click Update, you cannot get the ID.

3. Why can dropdownlist. selectedvalue be bound to the field role. roleid during editing?
A: When editing a vertex, You can bind a row of data from the gridview.
However, when a vertex is added, it is not bound to any data, so (eval ("role. roleid") cannot get any value,
Therefore, an error occurs when you use role. roleid to bind the selectedvalue of the dropdownlist (which has already bound the data source ).

Why I thought
previous conjecture:
1. conjecture: If dropdownlist. selectedvalue and field role. when binding a roleid,
when editing, click Edit to find the roleid from the database based on the ID and assign it to dropdownlist. when selectedvalue
is added: When a vertex is added, it is also assigned to dropdownlist (because it is the same as role. the roleid is bound), but you do not know the ID.
it is estimated that the default value is 0. Check the roleid from the database, in this case, it is found that the roleid must be null (because the database does not have data whose ID is 0).
If null is assigned to the dropdownlist (bound to the role table), an error is returned.
If dropdownlist is not bound to a table field when it is added, no value is assigned to dropdownlist.

code: http://www.namipan.com/downfile/GridView2.rar/646d625214c7ec186026b54a5df263bc34bc1d58b1030500

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.