将 LinkBut​​ton 的 OnClick 事件设置为代码隐藏中的方法

发布于 2024-09-30 05:53:10 字数 272 浏览 6 评论 0原文

我正在从我的代码隐藏构造一个 LinkBut​​ton,我需要将 onclick 分配给一个方法,并用它传递一个参数。到目前为止我已经有了:

LinkButton lnkdel = new LinkButton();
lnkdel.Text = "Delete";

我想要将其传递给的方法如下所示:

 protected void delline(string id)
        {

        }

I'm constructing a LinkButton from my codebehind, and I need to assign the onclick to a method, and pass a parameter with it too. I have this so far:

LinkButton lnkdel = new LinkButton();
lnkdel.Text = "Delete";

The method I want to pass it to looks like this:

 protected void delline(string id)
        {

        }

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

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

发布评论

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

评论(2

缪败 2024-10-07 05:53:10

那么您不能将其传递给该方法,您需要将单击事件委托分配给能够处理它的方法。

像这样:

public void DynamicClick(object sender, EventArgs e) {
    // do something
}

像分配任何事件一样分配点击事件:

lnkdel.Click += new EventHandler(DynamicClick);

如果你想传递一个参数,请使用CommandArgument,你将需要一个不同的委托:

void DynamicCommand(Object sender, CommandEventArgs e) 
      {
         Label1.Text = "You chose: " + e.CommandName + " Item " + e.CommandArgument;
      }

然后:

lnkDel.Command += new CommandEventHandler(DynamicCommand)
lnkDel.CommandArgument = 1234;

顺便说一句,如果你在> = C#3,你还可以使用酷炫的匿名方法

lnkDel.Command += (s, e) => { 
   Label1.Text = "You chose: " + e.CommandName + " Item " + e.CommandArgument;
};

Well you can't pass it to that method, you need to assign the click event delegate to a method capable of handling it.

Like this:

public void DynamicClick(object sender, EventArgs e) {
    // do something
}

Assign the click event like any event:

lnkdel.Click += new EventHandler(DynamicClick);

If you want to pass an argument, use CommandArgument, and you'll need a different delegate:

void DynamicCommand(Object sender, CommandEventArgs e) 
      {
         Label1.Text = "You chose: " + e.CommandName + " Item " + e.CommandArgument;
      }

And then:

lnkDel.Command += new CommandEventHandler(DynamicCommand)
lnkDel.CommandArgument = 1234;

BTW if you're on >= C#3, you can also use the coolness of anonymous methods:

lnkDel.Command += (s, e) => { 
   Label1.Text = "You chose: " + e.CommandName + " Item " + e.CommandArgument;
};
清风不识月 2024-10-07 05:53:10

该事件的函数原型是:

protected void lnkdel_OnClick(object _sender, EventArgs _args)
{
    LinkButton src = (LinkButton)_sender;
    // do something here...
}

将其分配为:

LinkButton lnkdel = new LinkButton();
lnkdel.Text = "Delete";
lnkdel.OnClick += new EventHandler(lnkdel_OnClick);

The function prototype for this event is:

protected void lnkdel_OnClick(object _sender, EventArgs _args)
{
    LinkButton src = (LinkButton)_sender;
    // do something here...
}

Assign it with:

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