以编程方式设置服务器端 OnClick() 事件
我正在寻找一种以编程方式设置 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过大量的工作和研究,我能够拼凑出一个可行的解决方案,但对于应该已经内置的东西来说,这似乎是一项巨大的工作。我所做的是扩展 System.Web.UI.WebControls.TableCell 对象以包含 OnClick 事件的句柄:
这是我使用新类的方式(几乎与库存 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:
Here is how I use my new class (almost exactly like the stock TableCell):
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.
我不知道这是否与您的问题相关,但我试图将服务器端函数添加到 LinkButton 并发现以下(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!