动态创建的按钮不会触发例程

时间:2015-08-03 01:37:20

标签: vb.net button gridview dynamic addressof

我有一个带有模板列的gridview:

<asp:UpdatePanel runat="server">  
 <ContentTemplate>  
  <asp:GridView ID="gridDay" runat="server" SkinID="gridviewSkinLight" AutoGenerateColumns="False" DataSourceID="DSAppointmentForDay">  
   <Columns>  
    <asp:BoundField DataField="TimeValue" HeaderText="" InsertVisible="False" ReadOnly="True" SortExpression="TimeValue" />  
    <asp:TemplateField HeaderText=" ">  
     <ItemStyle HorizontalAlign="Left" />  
    </asp:TemplateField>  
   </Columns>  
  </asp:GridView>  
 </ContentTemplate>  
 <Triggers>  
  <asp:AsyncPostBackTrigger ControlID="gridDay" />  
 </Triggers>  
</asp:UpdatePanel>  

在RowDataBound上,如果找到符合条件的数据,我会在单元格中创建按钮:

Dim cmdNew As New Button  
cmdNew.ID = "E" & dr("pkAppointment") & "|" & dr("ApptTopic")  
AddHandler cmdNew.Click, AddressOf mySub  
cmdNew.Text = dr("ApptTopic") & " >> " & dr("ApptLocation")  
cmdNew.ToolTip = "Topic: " & dr("ApptTopic") & vbLf &
                 "Location: " & dr("ApptLocation")  
e.Row.Cells(1).Controls.Add(cmdNew)  

到此为止,一切都很棒。按钮是在右侧单元格中创建的,带有所有的铃声和​​口哨声。

按钮应该调用的例程是:

Private Sub mySub(sender As System.Object, e As System.EventArgs)
    Try
        Dim btn As Button = DirectCast(sender, Button)
        MsgBox(btn.Text)

    Catch ex As Exception

    End Try
End Sub  

当我点击按钮时,页面会刷新,所有创建的按钮都会消失,并且不会调用mySub。

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

试试这个,

1)添加ScriptManager,

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

2)在gridview的OnRowCreated事件上创建按钮,将按钮注册为控件。

Dim cmdNew As New Button  
cmdNew.ID = "E" & dr("pkAppointment") & "|" & dr("ApptTopic")  
cmdNew.Text = dr("ApptTopic") & " >> " & dr("ApptLocation")  
cmdNew.ToolTip = "Topic: " & dr("ApptTopic") & vbLf &
                 "Location: " & dr("ApptLocation") 

ScriptManager1.RegisterAsyncPostBackControl(cmdNew) 
e.Row.Cells(1).Controls.Add(cmdNew)  

AddHandler cmdNew.Click, AddressOf mySub  
ScriptManager.GetCurrent(Me).RegisterAsyncPostBackControl(cmdNew)

更新(已测试)

1)由于您在母版页中已经有一个ScriptManager,因此您不需要新的。

2)使用UpdatePanel,就像这样

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="CreateButtons" >
            <Columns>
                <asp:BoundField DataField="Code" /> <!-- example column -->
                <asp:BoundField DataField="Text" /> <!-- example column -->
                <asp:CommandField />
            </Columns>
        </asp:GridView>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="GridView1"/>
    </Triggers>
</asp:UpdatePanel>

3)在您的代码隐藏中,在OnRowDataBound中创建按钮并在ScriptManager中注册按钮,使用类似的内容

Protected Sub CreateButtons(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim cmdNew As New Button
        cmdNew.ID = "Button" & e.Row.RowIndex
        cmdNew.Text = "Button" & e.Row.RowIndex
        cmdNew.ToolTip = "Button" & e.Row.RowIndex

        AddHandler cmdNew.Click, AddressOf CmdNewOnClick

        e.Row.Cells(2).Controls.Add(cmdNew)
        Dim myScriptManager As ScriptManager = Page.Master.FindControl("ScriptManager1")
        myScriptManager.RegisterAsyncPostBackControl(cmdNew)

    End If

End Sub

Private Sub CmdNewOnClick(ByVal sender As Object, ByVal e As EventArgs)
    Dim buttonClicked As Button = sender
    Debug.WriteLine("-----------------------------------------------")
    Debug.WriteLine("Button clicked:" & buttonClicked.ID)
    Debug.WriteLine("-----------------------------------------------")
End Sub

可用的演示项目here