AddHandler的,button.click不使用VB.NET射击 [英] Addhandler, button.click not firing using VB.NET

查看:399
本文介绍了AddHandler的,button.click不使用VB.NET射击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了按钮和一个AddHandler的问题。它只是如果我使用的AddHandler Button1.click,AddressOf ......在Page_Load中,但如果我在子例程中的一个动态创建按钮,该事件不火的作品。

I am experiencing a problem with buttons and AddHandler. It only works if I use AddHandler Button1.click, AddressOf... in Page_load, but if I create the button dynamically in one of the sub routines, the event doesn't fire.

例如,

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
    <asp:ListItem>1</asp:ListItem>
    <asp:ListItem>2</asp:ListItem>
</asp:DropDownList>
<asp:ScriptManager id="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel id="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="False">
    <contenttemplate>
        <asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder>
     </contenttemplate>
</asp:UpdatePanel>
<asp:UpdatePanel id="UpdatePanel2" runat="server" UpdateMode="Conditional">
    <contenttemplate>
        <asp:Label id="Label2" runat="server" Text="Label"></asp:Label>
    </contenttemplate>
</asp:UpdatePanel>


Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Label1.Text = Date.Now
    ScriptManager1.RegisterAsyncPostBackControl(DropDownList1)
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Label2.Text = "Panel refreshed at " + Date.Now.ToString()
End Sub

Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
    Dim b As New Button
    b.Text = "Click"
    ScriptManager1.RegisterAsyncPostBackControl(b)
    AddHandler b.Click, AddressOf Button1_Click
    PlaceHolder1.Controls.Add(b)
    UpdatePanel1.Update()
End Sub

DropDownList的工作,但按钮不会。我在做什么错了?

The dropdownlist works, but the button doesn't. What am I doing wrong?

推荐答案

您必须重新生成每一个回发(最后在Page_Load中,更好地Page_Init)的动态创建的控件。你必须设置相应的控件的ID,因为ASP.Net需要它来识别控制造成回发,并处理相应的事件。

You have to regenerate your dynamically created controls on every postback (at last in Page_Load, better in Page_Init). You have to set the ID of the controls accordingly because ASP.Net needs it to identify which control caused a Postback and to handle the appropriate events.

您可以节省创建按钮在ViewState中的数量和使用再生他们的Page_Load。增加的数量,当您添加一个新的按钮。使用这一数字也使按钮的ID是唯一的(其追加到ID),以确保其在每次回发是一样的。

You could save the number of created buttons in ViewState and use this to regenerate them on Page_Load. Increase the number when you add a new button. Use this number also to make the Button's ID unique(append it to the ID) to ensure that its the same on every postback.

有关进一步的信息,深入了解页面生命周期和 使用ViewState的动态添加控件的。

For further informations, have a look the Page-Lifecycle and ViewState with dynamically added controls.

编辑:正如乔尔评论说,如果你只需要一个按钮就可以设置它的ID静态,但你必须去重新创造它在回发F.E.以处理其Click事件。

As Joel commented, if you only need one Button you can set it's ID statically, but you have to regenerate it on postback f.e. to handle its click-event.

这篇关于AddHandler的,button.click不使用VB.NET射击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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