Talk about client scripts and webcontrol

Source: Internet
Author: User

In, when adding a client script (such as an onclick event) to a server-side control, a common method is to setAttributes. For example, Control. attributes ["onclick"] = "Return (window. Confirm ('....'));";. However, when using the above method, we must have reference to this control. However, in some cases, we are not very good at obtaining (or getting very troublesome) the reference of the required control. For example, in a DataGrid Control, you need to add a confirmation script for the control to be deleted:

1 <asp: DataGrid runat = "server">
2 <columns>
3 <asp: templatecolumn>
4 <itemtemplate>
5 <asp: linkbutton runat = "server" CommonName = "delete"> Delete <asp: linkbutton>
6 </itemtemplate>
7 </ASP: templatecolumn>
8 </columns>
9 </ASP: DataGrid>

To implement the above functions, we certainly hope to set a property just like a common HTML control. However, we also know that regards the onclick attribute as a server method and cannot compile successfully. Finally, you may need to use DataGrid. itemcreate and so on, which is very troublesome.

Let's take a closer look. In fact, our goal is to set onclick in its attributes Attribute before the linkbutton control is displayed (that is, before the render method is executed. In this case, we can write a subclass inherited from the system. Web. UI. webcontrols. linkbutton class, which is responsible for setting the onclick value. The Code is as follows:

1 using system;
2 using system. Web. UI;
3 using system. Web. UI. webcontrols;
5 namespace chenglin. webcontrols
6 {
7 [toolboxdata ("<{0}: scriptlinkbutton runat = server/>")]
8 public class scriptlinkbutton: system. Web. UI. webcontrols. linkbutton
9 {
10 private string _ script;
12 [Bindable (true)]
13 [category ("appearance")]
14 [defaultvalue ("")]
15 Public String script
16 {
17 get {return _ script ;}
18 set {_ script = value ;}
21 protected override void render (htmltextwriter writer)
22 {
23 if (_ script! = NULL & _ script. length> 0 ){
24 attributes ["onclick"] = _ script;
26 base. Render (writer );

With the above control, we can rewrite the above Code:

1 <asp: DataGrid runat = "server">
2 <columns>
3 <asp: templatecolumn>
4 <itemtemplate>
5 <chenglin: scriptlinkbutton
6 script = "Return (window. Confirm ('Are you sure? '));"
7 runat = "server" CommonName = "delete"> Delete
8 <chenglin: scriptlinkbutton>
9 </itemtemplate>
10 </ASP: templatecolumn>
11 </columns>
12 </ASP: DataGrid>

Now it seems that the entire code is much refreshed.

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: 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.