0

I have a gridview with a template column:

<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>  

On the RowDataBound, I create buttons in the cell if data is found that matches a condition:

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)  

Up to here, everything is great. The buttons are created in the right cell with all their bells and whistles.

The routine that the button should call is:

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  

The moment I click on the button, the page does a refresh, all the created buttons disappear and mySub is not called.

Am I missing something?

4

1 回答 1

0

尝试这个,

1)添加一个脚本管理器,

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

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

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

此处提供演示项目

于 2015-08-03T02:10:02.283 回答