0

Having an issue with my button click event. I needed to add this button from one page to another so naturally it was copied and variables changed out within the click event method to match the new page. in the HTML side I added the button there too (stating in case someone wonders if I forgot that part).

Everything is identical to the other page... The issue is... when I try to run it to test the button I get a build error with no errors being displayed. One time I managed to get an error that stated "Handles clause requires a WithEvents variable defined in the containing type or one of its base types." So I commented out the handles portion of the method and it runs yay! except... the button doesn't work. I have tried a few suggestions that stated to restart the IDE, and where someone also mentioned to comment out the handles portion. Is there something I am missing?

 <tr>
                    <td style="width: 209px" valign="middle" align="left"></td>
                    <td style="width: 7px;" valign="middle"></td>
                    <td valign="bottom" align="left">
                        <br>
                        <asp:Button ID="btnSaveAsExcel" runat="server" Width="264px" Text="Export to Excel"></asp:Button>
                    </td>
                    <td valign="middle" align="left"></td>
                </tr>



Private Sub btnSaveAsExcel_Click(sender As Object, e As EventArgs) Handles btnSaveAsExcel.Click
        If dgFacilitySummary.Visible Then

            Dim DummydgFacilitySummary As New DataGrid
            DummydgFacilitySummary.DataSource = Session("dsFacilitySummary").tables(0)
            DummydgFacilitySummary.DataBind()

            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" & "Faci+lity Summary Report" & ".xls")
            HttpContext.Current.Response.Charset = ""

            EnableViewState = False

            Dim sw As New StringWriter()
            Dim hw As New HtmlTextWriter(sw)

            DummydgFacilitySummary.RenderControl(hw)
            HttpContext.Current.Response.Write(sw.ToString())
            HttpContext.Current.Response.End()

        ElseIf dgFacilities.Visible Then
            Dim DummydgFacilities As New DataGrid
            DummydgFacilities.DataSource = Session("dsFacilityMatches").tables(0)
            DummydgFacilities.DataBind()

            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" & "Facility Summary Report" & ".xls")
            HttpContext.Current.Response.Charset = ""

            EnableViewState = False

            Dim sw As New StringWriter()
            Dim hw As New HtmlTextWriter(sw)

            DummydgFacilities.RenderControl(hw)
            HttpContext.Current.Response.Write(sw.ToString())
            HttpContext.Current.Response.End()
        End If
    End Sub

both of that is exactly how it is on another page and it works there.

ggiaquin16
  • 165
  • 1
  • 15

1 Answers1

0

Try deleting the btnSaveAsExcel_Click() method from your code behind file. Then select btnSaveAsExcel in the drop down list on the upper left of the IDE just above your code. That will populate the drop down list on the upper right with all the events for the btnSaveAsExel button. Select the OnClick event and that will create a btnSaveAsExcel_Click() method.

It would probably be a good idea to only copy the markup and then use the method I just described to create the event handlers in the code behind file.

Clint B
  • 4,610
  • 2
  • 18
  • 22
  • BTW, the handles portion tells the framework that this method handles the event. That's why nothing happened when you commented it out. You can name the method anything you want. The important part is the handles portion. – Clint B Apr 01 '16 at 21:41
  • Thanks for the suggestion! It did not work however :/ still get the same issue. Definitely good to know for the future and a good tip to remember though :) – ggiaquin16 Apr 01 '16 at 21:42
  • yes I understood that it would not work commenting it out but wanted to point out that it builds when it is and doesn't when it is not :) – ggiaquin16 Apr 01 '16 at 21:44
  • Which issue are you still having? Your project not compiling or the event handler not being called when you click the button? – Clint B Apr 01 '16 at 21:44
  • Ok, I'm assuming it's not compiling. Can you post the markup and the code behind? – Clint B Apr 01 '16 at 21:47
  • It isn't compiling when the Handles btnSaveAsExcel.Click is left uncommented which i know i need for it to fire. Let me edit the OP. – ggiaquin16 Apr 01 '16 at 22:00