How to create parameters for fields bound to data in the data source control

Source: Internet
Author: User
Tags connectionstrings

When data is bound to a control (such , Or Controls) when used together with the ASP. NET data source control, the data binding control can pass the parameter name and value to the data source control based on the binding fields in the data binding control. The data source control then contains the field name and value in the parameter set for selection or update. For more information, see using parameters for the sqldatasource control and using parameters for the objectdatasource control.

Dictionary passed to the data source control

When a data binding control requests an operation from a data source control, one or more Set. These parameter names and values are used by the requested data operation. The value of the name/value pair in the dictionary is derived from the Child control. For example, in the update operation, the data binding control displays Or Control. The name/value pair name comes from the field name bound to the Child control andDatakeynamesThe name of the field specified in the property. For update or delete operations, the data binding control can also transmit a dictionary containing the original value of the data record.

Use the followingIdictionarySet transfer name/Value Pair:

  • ValuesThe set is passed for the insert operation. Contains the name/value pair of the new record.ValuesThe field names and values of the set are from OrDetailsviewControls (its Set propertyTrue.

  • KeysThe collection is passed for update and delete operations. Contains the primary key or key of the record being updated or deleted. If you can modify the key field in the data sourceKeysThe Set also contains the original value of the key field. If the data in the data source control is used to fill in the data binding control, the data is maintained in the view status. When requesting an update or delete operation,KeysThe set is filled by the values stored in the previous view status. If the data is bound to Set propertyFalseWill not fill in the update or delete operationKeysSet.

  • NewvaluesThe set is passed for the update operation. Contains name/value pairs with updated new values, including new values of updatable key fields.NewvaluesThe field names and values of the set are fromEdititemtemplateOrDetailsviewControls (its Set propertyFalse.

  • OldvaluesThe collection is passed for update or delete operations. Contains the original value of the data record for the open concurrency check. (For more information about the open concurrency check, seeConflictdetectionAttribute .)OldvaluesSet does not containDatakeynamesThe value of the key field of the attribute identifier. Key field names and values are only included inKeysCollection. If the data in the data source control is used to fill in the data binding control, the data is maintained in the view status. When you request an update or delete operation, the values stored in the view State will be filled.OldvaluesSet. If the data is bound toEnableviewstateSet propertyFalseWill not fill in the update or delete operationOldvaluesSet.

You can use the data binding control event to access all these sets by passing parameters for the requested operation. For exampleGridviewControl Event, Class Set access.

Parameter Name

The data source control automaticallyIdictionarySet to create parameters. For the insert operation, the data source control usesValuesFill in the value in the name/value pair in the SetInsertparametersSet. For update operations, the data source control usesKeys,NewvaluesAndOldvaluesFill in the value in the name/value pair in the SetUpdateparametersSet. For the delete operation, the data source control usesKeysAndOldvaluesFill in the value in the name/value pair in the SetDeleteparametersSet.

By default, do not fillOldvaluesSet. Only inConflictdetectionSet property To fill it.

By default, only parameters of the currently bound value are created for update or delete operations. If you need to access the current and original bound values (for example, you must support the open concurrency check), you can have the data source control create parameters for both the current value and the original value. Therefore, you must create a naming convention for parameters that contain the original values. The format of parameters for these original values is as follows:OldvaluesparameterformatstringConfirm the property. SetOldvaluesparameterformatstringAttribute is set to a string. The string "{0}" is a placeholder for the field name. For example, if you are using Control, you will The attribute is set to "Old _ {0}". The name of the original value parameter will be resolved as the field name prefixed with "@ old (SqldatasourceThe control also appends a "@" character to the beginning of all parameter names .) ConsiderLastModifiedDate. The current value of this field is passedValuesDictionary, and the original value of this field is passedOldvaluesDictionary. In this case,@LastModifiedDateParameter to pass the current value.@old_LastModifiedDateParameter to pass the original value. These two parameters can be included in the SQL statement to distinguish the current value and the original value of the field, as shown in the following example:

  Copy code
UPDATE Table1 SET LastModifiedDate = @LastModifiedDate            WHERE Key = @Key AND LastModifiedDate = @old_LastModifiedDate

No need to directly access the name/valueIdictionarySet. You can simply include automatically generated parameter names in SQL statements (if the data source supports named parameters), or use The Parameter Name of the data method in the Business Object accessed by the control.

You can also selectUpdateparameters,InsertparametersOrDeleteparametersIn the set To customize the value passed by the data binding control. You can createParameterStrongly typed value of the object, orNullSpecify a default value.

The following code example demonstrates how to bindSqldatasourceControlDetailsviewControl.SqldatasourceControl , And Attribute usageSqldatasourceParameter name automatically generated by the control. AccordingKeysAndNewvaluesDictionary to fill in these parameter values. By default Set property , So do not useOldvaluesDictionary.

Visual Basic Copy code
<%@ Page language="VB" %>            <script RunAt="server">            Sub EmployeesDropDownList_OnSelectedIndexChanged(sender As Object, e As EventArgs)            EmployeeDetailsView.DataBind()            End Sub            Sub EmployeeDetailsView_ItemUpdated(sender As Object, e As DetailsViewUpdatedEventArgs)            EmployeesDropDownList.DataBind()            EmployeesDropDownList.SelectedValue = e.Keys("EmployeeID").ToString()            EmployeeDetailsView.DataBind()            End Sub            Sub EmployeeDetailsView_ItemDeleted(sender As Object, e As DetailsViewDeletedEventArgs)            EmployeesDropDownList.DataBind()            End Sub            Sub EmployeeDetailsSqlDataSource_OnInserted(sender As Object, e As SqlDataSourceStatusEventArgs)            Dim command As System.Data.Common.DbCommand = e.Command            EmployeesDropDownList.DataBind()            EmployeesDropDownList.SelectedValue = _            command.Parameters("@EmpID").Value.ToString()            EmployeeDetailsView.DataBind()            End Sub            </script>            

C # Copy code
<%@ Page language="C#" %>            <script RunAt="server">            void EmployeesDropDownList_OnSelectedIndexChanged(Object sender, EventArgs e)            {            EmployeeDetailsView.DataBind();            }            void EmployeeDetailsView_ItemUpdated(Object sender, DetailsViewUpdatedEventArgs e)            {            EmployeesDropDownList.DataBind();            EmployeesDropDownList.SelectedValue = e.Keys["EmployeeID"].ToString();            EmployeeDetailsView.DataBind();            }            void EmployeeDetailsView_ItemDeleted(Object sender, DetailsViewDeletedEventArgs e)            {            EmployeesDropDownList.DataBind();            }            void EmployeeDetailsSqlDataSource_OnInserted(Object sender, SqlDataSourceStatusEventArgs e)            {            System.Data.Common.DbCommand command = e.Command;            EmployeesDropDownList.DataBind();            EmployeesDropDownList.SelectedValue =            command.Parameters["@EmpID"].Value.ToString();            EmployeeDetailsView.DataBind();            }            </script>            

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.