Some controls are not displayed during the execution phase, that is, the control itself does not render HTML code during the execution phase, such as sqldatasoruce and scriptmanager; how are the pages displayed in the design phase? This article will further describe the appearance of the control in the design stage.
ProgramCodeDownload: ASP. NET Server Control-day19.rar
I.HtmlCode
The design patterns of Web server controls are all handled by controldesigner, so are the control components in the design phase. The control displays different appearances during the design phase and execution, of course, most of them will be consistent as much as possible so that what they see is what they get.
The HTML code of the control in the design stage is transparent controldesigner. the getdesigntimehtml method is used in controldesigner. getdesigntimehtml defaults to execute the control's rendercontrol method, so in most cases, the design phase and the HTML code output in the execution phase are the same. When the control's visible is false, the execution phase does not output the HTML code at all, but the control is specially set to visible = true during the design phase, so that the control can be fully rendered.
Controldesigner. getdesigntimehtml Method
Public Overridable FunctionGetdesigntimehtml ()As String
DimWriterAs NewStringwriter (cultureinfo. invariantculture)
DimWriter2As NewDesigntimehtmltextwriter (writer)
DimErrordesigntimehtmlAs String=Nothing
DimFlagAs Boolean=False
DimVisibleAs Boolean=True
DimViewcontrolAsControl =Nothing
Try
Viewcontrol =Me. Viewcontrol
Visible = viewcontrol. Visible
If NotVisibleThen
Viewcontrol. Visible =True
Flag =Not Me. Usepreviewcontrol
End If
Viewcontrol. rendercontrol (writer2)
Errordesigntimehtml = writer. tostring
CatchExceptionAsException
Errordesigntimehtml =Me. Geterrordesigntimehtml (exception)
Finally
IfFlagThen
Viewcontrol. Visible = visible
End If
End Try
If((NotErrordesigntimehtmlIs Nothing)Andalso(Errordesigntimehtml. Length <> 0 ))Then
ReturnErrordesigntimehtml
End If
Return Me. Getemptydesigntimehtml
EndFunction
Ii. Custom Controls Designer
Taking tbtoolbar as an example, if the rendercontents method is not targeted at items. when Count = 0 is used to process the output HTML, you will find that the tbtoolbar control is not visible on the design page when the items attribute is not set. For example, the HTML code of the design stage of the control, you can customize the designer of the control.
The inherited controldesigner is named tbtoolbardesigner, which is used to expand the design pattern behavior of the tbtoolbar control. We can override the getdesigntimehtml method to process the HTML tag of the control in the design phase. The original HTML code returned by this method is the display of the control on the design page. Therefore, when tbtoolbar. Items. Count = 0, we can output a prompt HTML code, so that when tbtoolbar does not set the items attribute, the control can be displayed on the design page.
''' <Summary>
''' Fill the tbtoolbar to control the design mode of the timer.
''' </Summary>
Public ClassTbtoolbardesigner
InheritsSystem. Web. UI. Design. controldesigner
''' <Summary>
''' Is used to represent the HTML Tag that controls the watermark in the design segment.
''' </Summary>
Public Overrides FunctionGetdesigntimehtml ()As String
DimShtmlAs String
DimOcontrolAsTbtoolbar
Ocontrol =Ctype(Viewcontrol, tbtoolbar)
IfOcontrol. Items. Count = 0Then
Shtml ="<Div style =""Background-color: # c0c0c0; Border: solid 1px; width: 200px""> Set items adequacy </div>"
Else
Shtml =Mybase. Getdesigntimehtml ()
End If
ReturnShtml
End Function
EndClass
Apply the designerattribute in the tbtoolbar control to set the custom tbtoolbardesigner category.
<Designer (GetType(Tbtoolbardesigner)> _
Public ClassTbtoolbar
InheritsWebcontrol
EndClass
Rebuild the control component and switch to the design page to see the appearance of the tbtoolbar control when the items attribute is not set. This is the HTML code that we return in the tbtoolbardesigner. getdesigntimehtml method.
If you think that the control in the above design stage is a little sunspring, we can also output a style similar to the sqldatasource control, and change the output HTML to the createplaceholderdesigntimehtml method when the items attribute is not set.
IfOcontrol. Items. Count = 0Then
Shtml =Mybase. Createplaceholderdesigntimehtml ("Please set items adequacy")
Else
Shtml =Mybase. Getdesigntimehtml ()
EndIf
Let's take a look at the modified results. Is it more professional.
Note: This article is also published in "the first it state help tie Ren Competition". If you think this articleArticleIt is helpful to you. Remember to add popularity when you connect to this article ^
Http://ithelp.ithome.com.tw/question/10012682