Control name problem using client scripts in user controls

zhaozj2021-02-17  47

ASP.NET provides a good module-level multiplexing technology - user control, greatly facilitating the construction of a web site and improving efficiency. User controls are used, and the problems encountered will also increase. Recently, I have encountered a problem, that is, if you want to use the client script in a user control, the problem will appear in the control. The problem is that after a user control is included in an ASPX page, the name of the control in the entire user control will change, and they are no longer the name when you designing this user control, but There are two related names:

ID - Client You can access this client control through this ID, ASP.NET gives the ID form of each control to "User Control ID_ This control ID", if it is the case where the user control is nested, the form is "" Top User Control ID_ Underline User Control ID_ The ID of this Control

Name - Client can also access this client control through this name, ASP.NET gives the Name "User Control ID: Id: ID: This Control ID", if it is the case where the user control is nestled, " Top User Control ID: Under Level User Control ID: The ID of this Control.

Example:

When designing the user control, a TextBox is placed, the ID is TexBox1. This user control is placed on an ASPX page. The IDBOX of the user control is WebUserControl11, and the textBox of the last user control is above this.

When the client is submitted to the server, it is submitted according to the Name of the control, that is, for the server, the client's Name is meaningful, the ID is not required.

When writing a client script, you cannot predict your user controls will be added to the ASPX page, and cannot predict how many layers nestled nested, so you can't refer to these controls at the time of design.

Fortunately, ASP.NET's WebControl and HTMLControl controls have runtime properties UNIQUEID to get unique to the server control, with the identifier defined in a hierarchical form. Corresponding to the above example, the UniqueID of the control TextBox1 is "WebUserControl11: TextBox1", which is consistent with the Name of the generated client.

So we can use the control's uniqueid to get the NAME of the runtime client, and you can control the client control through this name.

OK, in accordance with this idea to design a client script:

User control is simple, place a TextBox, we will set a mouse to this TextBox to make a script of the client, give TextBox a value, the code is as follows:

<% @ Control Language = "c #" AutoEventWireup = "false" Codebehind = "WebUserControl1.ascx.cs" Inherits = "WebApplication3.WebUserControl1" TargetSchema = "http://schemas.microsoft.com/intellisense/ie5"%>

}

// ->

Drag and drop this user control into an ASPX page, compile this page, report a script error: missing ';'

When you move your mouse to TextBox, a script error is reported: "Lack of objects"

The inspection found that the label referenced by the client cannot contain ":" symbols, that is, refer to the Name of the client cannot use the control. ASP.NET WebControl and HTMLControl's control also have a runtime property ClientID, which is used to get the server control identifier generated by the ASP.NET, which is to get the client ID of the control, and we will use this property to come try it:

Document.all. <% = textbox1.clientID%>. Value = "Kent";

Run again, OK is successful, "Kent" appears in TextBox when the mouse moves TextBox.

转载请注明原文地址:https://www.9cbs.com/read-29380.html

New Post(0)