以编程方式设置服务器端OnClick()事件 [英] Set Server Side OnClick() event Programmatically

查看:88
本文介绍了以编程方式设置服务器端OnClick()事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种以编程方式为TableCell对象设置OnClick事件处理程序的方法.我要尝试执行的ASP等效操作如下所示:

I am looking for a way to programmatically set the OnClick event handler for a TableCell object. The ASP equivalent of what I'm trying to do will look like this:

<asp:TableCell OnClick="clickHandler" runat="server">Click Me!</asp:TableCell>

在上面的示例中,"clickHandler"是.cs CodeBehind中定义的服务器端函数.

In the above example, "clickHandler" is a server-side function defined in the .cs CodeBehind.

public virtual void clickHandler(object sender, EventArgs args) {...}

但是,对于我来说,此TableCell对象需要动态创建,因此不能在ASP标记中设置它.我正在尝试在CodeBehind中执行以下操作:

However, for my situation, this TableCell object needs to be created dynamically, so setting it in an ASP tag is not an option. I am trying to do something like the following in the CodeBehind:

System.Web.UI.WebControls.TableRow row = new System.Web.UI.WebControls.TableRow();
System.Web.UI.WebControls.TableCell cell = new System.Web.UI.WebControls.TableCell();
cell.Text = "Click Me!";
cell.Attributes.Add("onClick", "clickHandler");
row.Cells.Add(cell);

不幸的是,在这种情况下:

Unfortunately, in this situation:

cell.Attributes.Add("onClick", "clickHandler");

"clickHandler"仅用作客户端javascript函数.我正在寻找一种将.cs CodeBehind中定义的服务器端clickHandler()函数链接到此表单元格的方法.

the "clickHandler" only works as a client-side javascript function. What I'm looking for is a way to link the server-side clickHandler() function, defined in the .cs CodeBehind, to this table cell.

经过一个下午的搜索,我一直无法提出有效的解决方案.预先感谢您的帮助.

After an afternoon of searching, I have been unable to come up with a working solution. Thanks in advance for any help.

推荐答案

经过大量的工作和研究,我能够拼凑出一个可行的解决方案,但是对于应该已经存在的某些事情来说,似乎需要进行大量的工作内置的.我所做的是扩展System.Web.UI.WebControls.TableCell对象以包括OnClick事件的句柄:

After a lot of work and research, I was able to cobble together a working solution, but it seems like an awful lot of work for something that should already be built-in. What I did was to extend the System.Web.UI.WebControls.TableCell object to include a handle for the OnClick event:

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyWebApp
{


    public class ExpandableTableCell : TableCell, IPostBackEventHandler, INamingContainer
    {
        private static readonly object click_event = new object();

        public ExpandableTableCell()
        {
        }

        // public handles for adding and removing functions to be called on the click event
        public event EventHandler Click
        {
            add
            {
                Events.AddHandler(click_event, value);
            }
            remove
            {
                Events.RemoveHandler(click_event, value);
            }
        }

        // define parent function that will be called when the container is clicked
        protected void Click(EventArgs e)
        {
            EventHandler h = Events[click_event] as EventHandler;
            if (h != null)
            {
                h(this, e);
            }
        }

        // specify the "post back event reference" or id of the click event
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            writer.AddAttribute(HtmlTextWriterAttribute.Onclick, 
                                Page.ClientScript.GetPostBackEventReference(this, "custom_click"));
        }

        // link the custom click id to the click function
        void System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
        {
            if(eventArgument == "custom_click")
            {
                this.OnClick(EventArgs.Empty);
            }
        }
    }
}

这是我使用新班级的方式(几乎与股票TableCell完全一样):

Here is how I use my new class (almost exactly like the stock TableCell):

System.Web.UI.WebControls.TableRow row = new System.Web.UI.WebControls.TableRow();
ExpandableTableCell click_cell = new ExpandableTableCell();
click_cell.Text = "Click Me!";
click_cell.Click += clickHandler;
// extra little touch for mouseover event
click_cell.Attributes.Add("onmouseover", "this.style.cursor='pointer'");
row.Cells.Add(click_cell);

正如我所说,似乎很难通过扩展类来在背后的代码中设置OnClick方法的麻烦.如果有人有任何其他想法或任何方式来清理或使上面的代码合法化,请告诉我.

As I have said, it seems like going through the trouble of extending the class to set the OnClick method in the codebehind is excessive. If anyone has any other ideas or any ways to clean up or legitimize the code above, please let me know.

这篇关于以编程方式设置服务器端OnClick()事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆