如何在ASP.NET中使用OnCommand应用AddHandler

时间:2022-10-30 13:01:30

I've been racking my brain trying to get this to work. My event for my LinkButton isn't firing. I'm figuring that it has SOMETHING to do with ViewState and that the button is not there when it tries to fire the event after the Postback or something.

我一直绞尽脑汁想要让它发挥作用。我的LinkBut​​ton活动没有解雇。我正在确定它与ViewState有关,并且当它在Postback之后尝试触发事件时按钮不存在。

When I click the Add button,it adds the link to the page and then when I click the Diplay Time" linkbutton it should fire the event and display the CommandArgument data but it's not and i can't figure out why.

当我单击“添加”按钮时,它会添加到页面的链接,然后当我单击“Diplay Time”链接按钮时,它应该触发事件并显示CommandArgument数据,但它不是,我无法弄清楚原因。

Here's my code:

这是我的代码:

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

    Protected Sub btnDelete_OnCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
        Response.Write(e.CommandArgument)
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim btn As New LinkButton()
        btn.ID = "lbn"
        btn.Text = "Display Time"
        btn.ValidationGroup = "vgDeleteSigner"
        AddHandler btn.Command, AddressOf btnDelete_OnCommand
        btn.CommandArgument = Now.TimeOfDay.ToString()
        btn.EnableViewState = True
        Panel1.Controls.Add(btn)
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        <asp:Panel ID="Panel1" runat="server">
        </asp:Panel>
    </div>
    </form>
</body>
</html>

1 个解决方案

#1


The reason that's happening is that your dynamic button "lbn" needs to be drawn again on post back when it's clicked because the button doesn't exist after you click it.

发生这种情况的原因是,当您点击它时,您的动态按钮“lbn”需要在回发后再次绘制,因为单击后该按钮不存在。

Basically you just have to dynamically add the button to the page again on post back of click of that button.

基本上你只需要在点击该按钮的帖子后再次动态地将按钮添加到页面。

I would recommend having the button already on the page but visible = false and then just showing it when you click the other button.

我建议在页面上已经有按钮但是visible = false,然后在你点击另一个按钮时显示它。

#1


The reason that's happening is that your dynamic button "lbn" needs to be drawn again on post back when it's clicked because the button doesn't exist after you click it.

发生这种情况的原因是,当您点击它时,您的动态按钮“lbn”需要在回发后再次绘制,因为单击后该按钮不存在。

Basically you just have to dynamically add the button to the page again on post back of click of that button.

基本上你只需要在点击该按钮的帖子后再次动态地将按钮添加到页面。

I would recommend having the button already on the page but visible = false and then just showing it when you click the other button.

我建议在页面上已经有按钮但是visible = false,然后在你点击另一个按钮时显示它。