在 ASP.NET 中动态添加命令按钮

发布于 2024-10-29 09:44:49 字数 2253 浏览 1 评论 0原文

我有一个带有动态创建的命令按钮的页面。

我需要动态连接一个单击事件,以便我可以获取所单击按钮的 CommandArgument。

Button b = new Button();
b.ID = "btnTrigger2";
b.CssClass = "hiddenBtn";
b.CommandName = "lbl3";
b.Click += new EventHandler(btnTrigger_Click);

问题是最后一行 - 我无法以这种方式连接 EventHandler - 我需要使用标准 EventArgs 而不是 CommandEventArgs。

有人对让它发挥作用有什么建议吗?必须有一种方法...

编辑

认为我会发布最终有效的代码,以防其他人尝试做同样的事情。

`字符串 tabLoadedScript = string.Empty; 字符串 postBackScript = string.Empty;

string script = " <script language='javascript' type='text/javascript'>" + System.Environment.NewLine;
script += "function clientActiveTabChanged(sender, args) {" + System.Environment.NewLine;


int i = 0;

foreach(TabPanel tp in tc1.Tabs)
    {
    Button b = new Button();
    b.ID = "btn" + tp.ClientID;
    b.CssClass = "hiddenBtn";
    b.CommandName = tp.ID;
    b.Command += btnTrigger_Click;
    this.form1.Controls.Add(b);
    AsyncPostBackTrigger trg = new AsyncPostBackTrigger();
    trg.ControlID = "btn" + tp.ClientID;

    tabLoadedScript += "var isTab" + tp.ClientID + "loaded=$get('" + tp.Controls[0].ClientID + "');" + System.Environment.NewLine;
    postBackScript += "if(!isTab" + tp.ClientID + "loaded && sender.get_activeTabIndex() == " + i + ") {" + System.Environment.NewLine;
    postBackScript += "__doPostBack('" + b.ClientID + "','');}" + System.Environment.NewLine;
    i++;
    }
script += tabLoadedScript;
script += postBackScript;
script += "}" + System.Environment.NewLine;
script += "</script>";

this.ClientScript.RegisterClientScriptBlock(this.GetType(), "cs", script, false);

protected void btnTrigger_Click(对象发送者,CommandEventArgs e) { 系统.Threading.Thread.Sleep(2500); 面板 ctrl = (面板) FindControlRecursive(this, "pnl" + e.CommandName); ctrl.Visible = true; }

public static Control FindControlRecursive(Control Root, string Id)
    {

    if(Root.ID == Id)
        return Root;

    foreach(Control Ctl in Root.Controls)
        {

        Control FoundCtl = FindControlRecursive(Ctl, Id);
        if(FoundCtl != null)
        return FoundCtl;

        }
    return null;

    }

`

I have a page with a dynamically created command button.

I need to dynamically wire up a click event so I can grab the CommandArgument of the clicked button.

Button b = new Button();
b.ID = "btnTrigger2";
b.CssClass = "hiddenBtn";
b.CommandName = "lbl3";
b.Click += new EventHandler(btnTrigger_Click);

The problem is the last line - I can't wire up an EventHandler this way - I need to use the standard EventArgs instead of CommandEventArgs.

Anyone have any suggestions on getting this to work? There's got to be a way...

EDIT

Figured I'd post the code that finally worked in case anyone else tries to do the same thing.

`string tabLoadedScript = string.Empty;
string postBackScript = string.Empty;

string script = " <script language='javascript' type='text/javascript'>" + System.Environment.NewLine;
script += "function clientActiveTabChanged(sender, args) {" + System.Environment.NewLine;


int i = 0;

foreach(TabPanel tp in tc1.Tabs)
    {
    Button b = new Button();
    b.ID = "btn" + tp.ClientID;
    b.CssClass = "hiddenBtn";
    b.CommandName = tp.ID;
    b.Command += btnTrigger_Click;
    this.form1.Controls.Add(b);
    AsyncPostBackTrigger trg = new AsyncPostBackTrigger();
    trg.ControlID = "btn" + tp.ClientID;

    tabLoadedScript += "var isTab" + tp.ClientID + "loaded=$get('" + tp.Controls[0].ClientID + "');" + System.Environment.NewLine;
    postBackScript += "if(!isTab" + tp.ClientID + "loaded && sender.get_activeTabIndex() == " + i + ") {" + System.Environment.NewLine;
    postBackScript += "__doPostBack('" + b.ClientID + "','');}" + System.Environment.NewLine;
    i++;
    }
script += tabLoadedScript;
script += postBackScript;
script += "}" + System.Environment.NewLine;
script += "</script>";

this.ClientScript.RegisterClientScriptBlock(this.GetType(), "cs", script, false);

protected void btnTrigger_Click(object sender, CommandEventArgs e)
{
System.Threading.Thread.Sleep(2500);
Panel ctrl = (Panel) FindControlRecursive(this, "pnl" + e.CommandName);
ctrl.Visible = true;
}

public static Control FindControlRecursive(Control Root, string Id)
    {

    if(Root.ID == Id)
        return Root;

    foreach(Control Ctl in Root.Controls)
        {

        Control FoundCtl = FindControlRecursive(Ctl, Id);
        if(FoundCtl != null)
        return FoundCtl;

        }
    return null;

    }

`

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

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

发布评论

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

评论(2

揪着可爱 2024-11-05 09:44:50

您仍然可以使用传递给单击处理程序的第一个参数来访问 CommandArgument。

像这样的东西:

protected void btnTrigger_Click(object sender, EventArgs e)
{
  Button btnTrigger = sender as Button;
  String commandArgument = btnTrigger.CommandArgument;
  .
  .
  .
  .
  //Other code....

}

You can still access the CommandArgument using the the first argument passed to the click handler.

Something like:

protected void btnTrigger_Click(object sender, EventArgs e)
{
  Button btnTrigger = sender as Button;
  String commandArgument = btnTrigger.CommandArgument;
  .
  .
  .
  .
  //Other code....

}
沉睡月亮 2024-11-05 09:44:50

您需要使用 Command 事件而不是 Click 事件。另外,假设您使用的是最新版本的 Visual Studio 和 .Net,您可以简单地将事件注册从 更改

b.Click += new EventHandler(btnTrigger_Click);

b.Command += btnTrigger_Click

委托的显式类型是多余的,将由编译器推断。您现在可以将事件处理程序的签名更改为:

protected void btnTrigger_Click(object sender, CommandEventArgs e)

并且代码应该按需要工作。

不幸的是,Visual Studio 中的默认代码片段仍然生成此旧式事件侦听器代码。

You need to use the Command event instead of the Click event. Also, assuming you're using a recent version of Visual Studio and .Net, you can simply change the event registration from

b.Click += new EventHandler(btnTrigger_Click);

to

b.Command += btnTrigger_Click

The explicit typing of the delegate is redundant and will be inferred by the compiler. You can now change the signature of your event handler to:

protected void btnTrigger_Click(object sender, CommandEventArgs e)

And the code should work as desired.

Unfortunately, the default code snippets in Visual Studio still generate this old-style event listener code.

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