Problem 1: When I use detailsview, I find that the cursor does not automatically locate the first entry box in detailsview every time I enter the new or edit status of detailsview, is there any solution?
A: To enable the cursor to automatically locate the entry control on the detailsview editing interface, you only need to open the design view of the page under Vs and switch to "Source view ", add the following code "defaultfocus =" detailsview1 "" in the form tag. After saving the code and running it, you will find that the cursor has automatically located the first entry box of detailsview.
Question 2: detailsview automatically sets the width of the title column based on the length of the title text. How can I customize the width of the title column?
A: To implement the features you mentioned, use the headerstyle of detailsview to define the appearance style. Like the gridview, detailsview also provides a large number of "***** style" attributes for you to customize the appearance of the detailsview. However, many people who are new to detailsview do not know the specific appearance of each "***** style" attribute. In fact, there is a simple way to help you quickly understand the style attributes of detailsview. You just need to imagine detailsview as a 90-degree Reverse Rotation of the gridview, it is easier to understand the appearance position defined by each style attribute.
Question 3: I want the title of the detailsview to change with the editing status, for example, "new user", "edit user", and "view User" are displayed when you edit a record ".
A: You can use the currentmode attribute in the detailsview modechanged event to indicate the current status of the detailsview, and set the caption to the corresponding prompt text. The specific implementation code is as follows:
Select case detailsview1.currentmode
Case detailsviewmode. Edit
Detailsview1.caption = "edit"
Case detailsviewmode. insert
Detailsview1.caption = "new"
Case detailsviewmode. readonly
Detailsview1.caption = "View"
End select
It should also be noted that if you do not directly use the button that comes with detailsview, but modify the editing status of detailsview using the changemode method of detailsview in code mode, the modechanged event will not be triggered.
Question 4: I want to set a default value for a bound field when creating a record in detailsview, but I don't know how to locate the specified control in detailsview?
A: To solve this problem, you need to consider two steps: first, when to locate the problem; second, how to locate the problem. Among the many events provided by detailsview, The databounded event is triggered after data binding is completed. At this time, the controls on detailsview have been initialized and data bound, which is the most suitable place to modify the default settings. The findcontrol method cannot be used because the control name used to bind the field is dynamically allocated at runtime. To locate the input control, the only method is to use the following code:
Dim currentdate oldpassword as textbox = ctype (detailsview1.rows (2). cells (1). Controls (0), textbox)
Textbox startdate = (textbox) detailsview1.rows [1]. cells [1]. controls [0];
That is, the hierarchical relationship of the Control tree corresponding to the detailsview control is used to locate the input control. Next, set the text attribute of textbox to the default value.
Note that before executing the above Code to set the default value, you must determine whether the currentmode attribute of detailsview is equal to detailsviewmode. insert.
Question 5: I used detailsview to create a password modification interface and hoped to implement the following functions: if the user does not enter the original password and new password, the original password remains unchanged. If you click Update, detailsview does not update data.
Answer: to meet this requirement, you need to find the text box for entering the password in the itemupdating event of detailsview by using the findcontrol method or the method mentioned in question 4. If the content of the text box is blank, set the cancel attribute of the detailsviewupdateeventhandler parameter in the itemupdating event to true. The specific implementation code is as follows:
Protected sub detailsviewincluitemupdating (byval sender as object, byval e as system. Web. UI. webcontrols. detailsviewupdateeventargs) handles detailsview1.itemupdating
Dim oldpassword as textbox = ctype (detailsview1.rows (3). cells (1). Controls (0), textbox)
Dim newpassword as textbox = ctype (detailsview1.rows (4). cells (1). Controls (0), textbox)
If (oldpassword. Text = string. Empty orelse newpassword. Text = string. Empty) then
E. Cancel = true
End if
End sub
Question 6: I found that, even if the data is not modified, after clicking the update button, the detailsview will still submit the update. I think this will affect the program performance. How can I make detailsview submit updates only when data changes?
A: To implement this function, you can use the newvalues attribute and oldvalues attribute of the detailsviewupdateeventhandler parameter in the itemupdating event to save the data set before and after modification, by comparing these two attributes, You can see whether the user has modified the data in the detailsview. The specific implementation code is as follows:
Protected sub detailsviewincluitemupdating (byval sender as object, byval e as system. Web. UI. webcontrols. detailsviewupdateeventargs) handles detailsview1.itemupdating
Dim isupdated as Boolean = false
For I as integer = 0 to E. newvalues. Count-1
'At least one value is updated.
If E. newvalues (I) <> E. oldvalues (I) then
Isupdated = true
Exit
End if
Next
If isupdated = false then
E. Cancel = true
Detailsview1.changemode (detailsviewmode. readonly)
End if
End sub