[Implement day3 with ASP. NET controls] extend the functions of existing server controls

Source: Internet
Author: User

It is relatively simple and practical to inherit from existing server controls as opposed to development controls from scratch. If you want to add some attributes or functions to the existing controls, it is the fastest way to extend the function by directly inheriting the control. For example, you need to "press the button to bring up a query message" and "when the textbox is set to readonly, you can get the text attribute returned by the front end, you can directly inherit the original control and add the required functions. The following is a simple example to illustrate how to inherit the existing Server and extend the function.

I. Expansion ButtonControl: Add a query message to the button

Before you press a button to execute an action, you are asked if you want to execute the action. For example, if you press the delete button, you are asked if you are sure you want to delete the action. Of course, this only requires simple JavaScript, but compared with. Net'sProgramLanguage, JavaScript is a client-side instruction code that is very difficult to maintain. if Javascript is completely unavailable to developers, why not? The button control itself can provide the function of adding a query message, and the related Javascript is processed by the control.

Generally, you need to add an inquiry message to the button. You only need to set the following Javascript in the onclientclick attribute. Our goal is to let developers omit the JavaScript code that sets the onclientclick attribute and directly set the message to be asked. Then we will start to implement this control.

    Asp: button   id   = "button1"   runat   =" server "  text   = "button"   onclientclick   = "If (confirm (' are you sure you want to confirm? ') = False) {return false;} " />  

 

In the bee. Web project, add the "ASP. NET Server Control", which inherits the button and is named tbbutton (The namespace is bee. Web. webcontrols ). Add the confirmmessage attribute to the tbbutton category to set the content of the query message. In the render method, set the JavaScript code of the inquiry to the onclientclick attribute.

 
NamespaceWebcontrols
<_
 
Description (""),_
 
Toolboxdata ("<{0}: tbbutton runat = Server> </{0}: tbbutton>")_
 
> _
 
Public ClassTbbutton
 
InheritsSystem. Web. UI. webcontrols. Button
 
 
 
<Description ("Alarm information")> _
 
Public PropertyConfirmmessage ()As String
 
Get
DimSconfirmmessageAs String
 
Sconfirmmessage =CSTR(Viewstate ("Confirmmessage"))
 
IfSconfirmmessageIs Nothing Then
 
Return String. Empty
 
Else
 
ReturnSconfirmmessage
 
End If
 
End Get
Set(ByvalValueAs String)
 
Viewstate ("Confirmmessage") = Value
 
End Set
 
End Property
 
 
 
''' <Summary>
 
''' Override the render method.
 
''' </Summary>
Protected Overrides SubRender (ByvalWriterAsSystem. Web. UI. htmltextwriter)
 
DimSscriptAs String
 
DimSconfirmAs String
 
 
 
'If you have set the confirmmessage adequacy, add the ignore message JavaScript to onclientclick.
 
If Me. Confirmmessage <>String. EmptyThen
 
Sscript =Me. Onclientclick
'Your request message Javascript
 
Sconfirm =String. Format ("If (confirm ('{0}') = false) {return false ;}}",Me. Confirmmessage)
 
IfSscript =String. EmptyThen
 
Me. Onclientclick = sconfirm
 
Else
 
Me. Onclientclick = sconfirm & sscript
 
End If
 
End If
Mybase. Render (writer)
 
End Sub
 
 
 
End Class
 
EndNamespace

 

Drag tbbutton to the test page and set the confirmmessage attribute.

    bee: tbbutton   id   = "tbbutton1"   runat   =" server "  confirmmessage   = "are you sure you want to allocate other resources? "  text   =" excluding " />  

 

The following figure shows the result of the rows.

 

Ii. Conclusion

During the development of ASP. NET applications, I usually get used to inheriting all existing controls, no matter whether the control functions need to be extended at present. This method is quite helpful for the development of large systems, because it is impossible to predict whether the control function will be extended due to certain conditions during system development, therefore, we should inherit all of them for future needs, and retain the flexibility of future changes.

3. Related Links

Extended commandfield category-delete prompt message
Button plus inquiry message

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/10011562

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.