ASP.NET 动态命令按钮事件未触发 [英] ASP.NET dynamic Command Button event not firing

查看:22
本文介绍了ASP.NET 动态命令按钮事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试动态创建命令按钮,但单击有问题的按钮似乎不会引发相应的 CommandButton_Click 事件.我注意到在 SO 的示例中,为 Button.OnCommand 以及 CommandName 和 CommandArgument 设置了一个属性,但它不是智能感知中的一个选项.

I'm trying to create Command Buttons dynamically, but clicking the button in question doesn't seem to raise the corresponding CommandButton_Click event. I noticed that in the examples on SO a property is set for Button.OnCommand as well as the CommandName and CommandArgument but it isn't an option in intellisense.

所以问题是,我在这里做错了什么(下面的代码没有 OnCommand),它是否以其他方式访问 - 如果是这样,为什么我找到的示例都将其显示为 .OnCommand?

So the question is, what am I doing wrong here (code below without the OnCommand), is it accessed in some other way - if so, why do the examples I've found all show it as .OnCommand?

为了进一步提供帮助,我添加了处理程序,但事件仍未触发.按钮驻留在 UpdatePanel 中,并在每次回发时重建(与处理程序一起).我创建了一个我正在做的事情的简化示例,如下所示 如果按钮事件触发,它会将EVENT FIRED"写入 txtTestFired 文本框 - 可以说我从未见过.这真的让我发疯了,非常感谢您的帮助.

Further to help, I have added the handler however the event is still not firing. The buttons reside in an UpdatePanel and are rebuilt on every postback (along with the handler). I have created a simplified example of what I'm doing which is shown below If the button event fires, it writes "EVENT FIRED" to the txtTestFired Textbox - suffice to say I have never seen that. This is really driving me nuts, any help is very gratefully received.

.aspx 文件

<form id="frmMain" runat="server">
    <asp:ScriptManager ID="scmAddProducts" runat="server">
    </asp:ScriptManager>
    <asp:updatepanel runat="server">
        <ContentTemplate>
            <asp:TextBox ID="txtProduct" runat="server"></asp:TextBox>
            <br />
            <asp:Button ID="btnAddItem" runat="server" Text="Add Line" />&nbsp;
            <asp:TextBox ID="txtTestFired" runat="server"></asp:TextBox>
            <br />
            <br />
            <asp:Panel ID="pnlAddedLines" runat="server"></asp:Panel>
        </ContentTemplate>
    </asp:updatepanel>
</form>

.aspx.vb 文件

.aspx.vb file

Protected Sub btnAddItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddItem.Click
    Dim dtItems As New System.Data.DataTable

    If Session("Items") Is Nothing Then
        Dim dcColumn As New System.Data.DataColumn
        dcColumn.DataType = Type.GetType("System.String")
        dcColumn.ColumnName = "Product"
        dtItems.Columns.Add(dcColumn)
        Session("Items") = dtItems
    End If

    dtItems = CType(Session("Items"), System.Data.DataTable)
    Dim drRow As System.Data.DataRow
    drRow = dtItems.NewRow()
    drRow("Product") = txtProduct.Text
    dtItems.Rows.Add(drRow)

    Session("Items") = dtItems
    txtProduct.Text = ""
End Sub
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
    If Not Session("Items") Is Nothing Then
        Dim dtItems As System.Data.DataTable = CType(Session("Items"), System.Data.DataTable)
        Dim iItemIndex As Integer = 1
        For Each drRow In dtItems.Rows
            Dim btnClose As New Button
            btnClose.ID = "btnClose" & iItemIndex
            btnClose.CssClass = "formCloseButton"
            btnClose.Text = "X"
            AddHandler btnClose.Click, AddressOf Button_Clicked
            pnlAddedLines.Controls.Add(btnClose)
            btnClose = Nothing

            Dim txtProduct = New TextBox
            txtProduct.ID = "txtProduct" & iItemIndex
            txtProduct.CssClass = "formText"
            txtProduct.Text = drRow("Product")
            txtProduct.Columns = "40"
            pnlAddedLines.Controls.Add(txtProduct)
            iItemIndex += 1

            Dim litHR = New Literal
            litHR.Text = "<hr />"
            pnlAddedLines.Controls.Add(litHR)
            litHR = Nothing
        Next
    End If
End Sub
Private Sub Button_Clicked(ByVal sender As Object, ByVal e As System.EventArgs)
    txtTestFired.Text = "EVENT FIRED"
End Sub

推荐答案

本质上,您需要在页面加载时创建原始控件一次以便能够处理按钮单击,然后您需要清除控件并重新在按钮单击处理程序中或在预渲染中生成它们 - 我认为它在单击处理程序上更清晰,因此您需要在两者中使用相同的代码(最好重构为单独的方法)

Essentially you need to create the original controls once on page load in order to be able to handle the button click, you then need to clear the controls and re-generate them either in the button click handler or as you have done on pre-render - i think it is cleaner on the click handler, so you need the same code in both (preferable refactored out to a separate method)

下面的示例意味着对您的代码更改最少:

The example below would mean minimum code changes for you:

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
    If Not Session("Items") Is Nothing Then
        pnlAddedLines.Controls.Clear()
        GenerateControls()
    End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Session("Items") Is Nothing Then
        GenerateControls()
    End If
End Sub
Private Sub GenerateControls()
    Dim dtItems As System.Data.DataTable = CType(Session("Items"), System.Data.DataTable)
    Dim iItemIndex As Integer = 1
    For Each drRow In dtItems.Rows
        Dim btnClose As New Button
        btnClose.ID = "btnClose" & iItemIndex

这篇关于ASP.NET 动态命令按钮事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆