以编程方式设置服务器端 OnClick() 事件

发布于 2024-08-29 13:05:56 字数 963 浏览 3 评论 0原文

我正在寻找一种以编程方式设置 TableCell 对象的 OnClick 事件处理程序的方法。我想做的 ASP 等效项如下所示:

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

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

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

但是,对于我的情况,需要动态创建此 TableCell 对象,因此不能将其设置在 ASP 标记中。我正在尝试在 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);

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

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

“clickHandler”仅充当客户端 JavaScript 函数。我正在寻找一种将 .cs CodeBehind 中定义的服务器端 clickHandler() 函数链接到此表格单元格的方法。

经过一下午的搜索,我一直无法想出可行的解决方案。预先感谢您的任何帮助。

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>

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

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

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");

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

养猫人 2024-09-05 13:05:56

经过大量的工作和研究,我能够拼凑出一个可行的解决方案,但对于应该已经内置的东西来说,这似乎是一项巨大的工作。我所做的是扩展 System.Web.UI.WebControls.TableCell 对象以包含 OnClick 事件的句柄:

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 完全相同): 正如

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 方法的麻烦是过度的。如果有人有任何其他想法或任何方法来清理或使上述代码合法化,请告诉我。

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);
            }
        }
    }
}

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);

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.

冷情妓 2024-09-05 13:05:56

我不知道这是否与您的问题相关,但我试图将服务器端函数添加到 LinkBut​​ton 并发现以下(VB)代码:AddHandler cell.Click, AddressOf clickHandler ,这对我有用。

根据 此代码转换服务,这会转换为 cell .Click += clickHandler; 在 C# 中。

希望这有帮助!

I don't know if this is relevant to your problem, but I was trying to add a server-side function to a LinkButton and found the following (VB) code: AddHandler cell.Click, AddressOf clickHandler, which worked for me.

According to this code conversion service, this translates to cell.Click += clickHandler; in C#.

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文