ASP.NET user control

zhaozj2021-02-16  44

This article describes how to create user controls in ASP.NET, dynamic modifications of control, control attributes, and control of the control event. ============================================================================================================================================================================================================= ================ Introduction ASP.NET server control makes web development work easier and more powerful. We introduce how to use the server control in the ASP.NET page. However, what should I do if the server does not have any requiring controls?

Of course, ASP.NET will not change from a need for a control. In fact, you can do your own control to replace the control provided by .NET. This control is the user control, which is also the topic discussed in this article.

Some people who write the first user control believe that knowing how to use the server control can not necessarily mean that writing user controls is easy.

In fact, writing a basic user control (sometimes known as Pagelets) and uses these controls as the ASP.NET page icon using the server control. Here is a simple example:

Basic.ascx

this is a user control ... really!

This is a user control! Seeing this, I think you will say that I should not get drunk, my mind is not clear. But this code is indeed a user control that is easy to use. Although this control has nothing, it is about what is a good description of user controls. Things are not as complex. Pay attention to suffix .ascx, which tells web pages This is a user control. It doesn't have any special meaning, just don't let IIS go directly to this code.

Now let's create a user control, look at the example below:

Basic.aspx <% @ page language = "VB"%> <% @ register tagprefix = "asp101samps" tagname = "sylext" src = "Basic.ascx"%>

ASP.NET User Control Sample - Basic </ title> </ head> <body bgcolor = "# ffffff"></p> <p><asp101samps: sylext runat = "server" /></p> <p></ body> </ html></p> <p>This code outputs a standard HTML page that displays the text in the user control instead of a mark. So how is it implemented? The key is in the registration (Register). To register your control, first define three properties:</p> <p>TagPRefix Defines the namespace of the control location. With namespace constraints, you can use different functionality controls in the same page.</p> <p>Tagname points to the name of the control used. The control name in the same namespace is unique. Control names generally indicate the functionality of the control.</p> <p>SRC points to the resource file of the control. The resource file uses the virtual path ("Control.ASCX" or "/Path/control.ascx", cannot use the physical path ("C: /Path/control.ascx."). After the control is registered, you can use it as other server controls. By defining the Tagrefix and Tagname (Tagname), you can use it as used with the server's built-in control. It also determines the use of server operations (Runat = "Server") mode. Below is the basic way to call user controls on the web page: <tagprefix: tagname runat = "server" /></p> <p>Add the attribute to the user control and assign the value. I add two properties to the control, one is color, the other is Text.</p> <p>Properties.ascx <script language = "vb" runat = "server"> public color as string = "Black" public text as string = "this is a user control ... really!" </ script></p> <p><p> <font color = "<font color =" <% = color%> "> <% = text%> </ font> </ p></p> <p>This allows you to use and change the color of the control and text. You can assign a value during initialization, and these two properties can also be dynamically modified.</p> <p>You can repeat this control in the same page and use different attribute values: proties.aspx <% @ page language = "vb"%> <% @reg tagprefix = "ASP101SAMPS" tagname = "SOMETEXT" SRC = "Properties. ASCX "%></p> <p><Script language = "VB" runat = "server"> Sub Page_Load (Sender As Object, E As EventArgs) UserCtrl1.Color = "green" UserCtrl1.Text = "This control's properties were" _ & "set programmatically!" End Sub </ script></p> <p><html> <head> <title> ASP.NET User Control Sample - Properties </ Title> </ head> <body bgcolor = "# ffffff"></p> <p><asp101samps: sylext runat = "server" /></p> <p><asp101samps: sometext color = "red" runat = "server" /></p> <p><asp101samps: sylext text = "this is quite cool!" runat = "server" /></p> <p><asp101samps: sometext color = "blue" text = "ain't it?" runat = "server" /> <ask101samps: sometext id = "userCtrl1" runat = "server" /></p> <p></ body> </ html></p> <p>I still want to be better, can the user control have an event handle? User controls can be almost anything. The following code demonstration control triggers the Page_Load event. With an event handle, you don't have to write other maintenance code to control the operation of the control. The control can trigger an event yourself.</p> <p>In the following code, the TextBox control of an ASP is encapsulated. I will hook my control name attribute with the contents of TextBox.</p> <p>events.ascx <script language = "VB" runat = "server"> Sub Page_Load (Src As Object, E As EventArgs) Dim strInitialText As String = "Please Enter a Name!" If Page.IsPostBack Then If txtName.Text = strInitialText TXTNAME.TEXT = "" end if else txtname.text = StrinitialText end if End Sub</p> <p>Public property name as string get return txtname.text end set txtname.text = value end vendy </ script></p> <p>Name: <asp: textbox id = "txtname" runat = "server" /></p> <p><ask: requiredfieldvalidator controloValidate = "txtName" id = "ValtxtName" display = "Dynamic" runat = server> please enter a name! </ ask: RequiredFieldValidator></p> <p>events.aspx <% @ Page Language = "VB" ClientTarget = "downlevel"%> <% @ Register TagPrefix = "asp101samps" TagName = "SomeText" Src = "properties.ascx"%> <% @ Register TagPrefix = "asp101samps "Tagname =" textbox "src =" Events.ascx "%></p> <p><script language = "vb" runat = "server"> SUB Page_load (sender as object, e as eventargs) txtLabel.text = ""</p> <p>'The textbox control handles it's own stuff' in it's own Page_Load event handler. End SubSub btnSubmit_Click (Sender As Object, E As EventArgs) 'Sets the label to the textbox's text txtLabel.Text = txtName.Name</p> <p>'I don't need to worry about validation since' my user control does it for me. End Sub </ script></p> <p><html> <head> <title> ASP.NET User Control Sample - Validation & Events </ Title> </ head> <body bgcolor = "# fffff"></p> <p><form runat = "server"></p> <p><asp101samps: textbox id = "txtname" runat = "server" /></p> <p><br /></p> <p><ask = "btnsubmit" onclick = "btnsubmit_click" text = "submit" runat = "server" /></p> <p></ form></p> <p><asp101samps: sometext id = "txtlabel" runat = "server" /></p> <p></ body> </ html></p> <p>This is a description of user controls and applications. Whether you think it is simple, it is definitely easier than using traditional ASPs.</p> <p>The following can be downloaded below.</p> <p>Http://www.chinaok.net/down/200204221838030.zip</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-27872.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="27872" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.064</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'wqowboxT3fvuf4pIonb6ejQE5cjeNLFUa4x0Ik1o4VhddVHkFCNDh0hg41BUojSDZoqzQq4gbMoI8VnmShgdwQ_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>