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?

1 Answers1

0

Try this,

1) Add a ScriptManager,

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

2) Create the buttons on OnRowCreated event of the gridview, register the buttons as control.

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)

UPDATE (tested)

1) Since you already have a ScriptManager in the master page, you don't need a new one.

2) With an UpdatePanel, like this

<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) In your codebehind, create the buttons in OnRowDataBound and register the buttons in the ScriptManager, with something like this

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

A demo project available here

davcs86
  • 3,926
  • 2
  • 18
  • 30
  • I use Site.Master which contains the ScriptManager. I replaced your code with `Dim myScriptManager As ScriptManager = Page.Master.FindControl("CPScriptManager") myScriptManager.RegisterAsyncPostBackControl(cmdNew)`, yet it still clears the grid when I click on the control, and doesn't fire the function for the handling of the event. – Dieter Stalmann Aug 04 '15 at 04:04
  • I updated my answer, and I posted the relevant code and a link for a dummy demo. – davcs86 Aug 04 '15 at 19:26