njj56
njj56

Reputation: 621

adding onclick event to button causes error in page

below is the html code for a button in my asp.net application which uses vb as its serverside language. The button is handled on the server side, in which the code is listed below as well. My issue is when I add an onclick event (I want to call a javascript function, either in the same page or in the parent as this page is only displayed in an iframe in our application, never stand alone).

Currently, this is the html for the button

<asp:button class="smalltextbutton" id="Button1" runat="server" Text="Save"></asp:button>

When changed to the following below, I get an error when the page is launched stating "Compiler Error Message: BC30456: 'refreshParentTree' is not a member of 'ASP.tasks_modifytask_aspx'. "

<asp:button class="smalltextbutton" id="Button1" OnClick="refreshParentTree()" runat="server" Text="Save"></asp:button>

and for now the javascript function is very basic, outside of the form, here is what it looks like.

<script language="javascript">
 function refreshParentTree(){
        alert('refresh parent tree hit');
        }
</script>

On the server side, here is my code that handles the button click (vb)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'DEBUG: 
    If Trace.IsEnabled Then cGK_testSpot.Text += "<div style='border: solid thin orange;'>Button1_Click() STARTING<br>"
    'DEBUG: 
    If Trace.IsEnabled Then cGK_testSpot.Text += "<div style='border: solid thin green;'>printArrayList (delete ALL EXCEPT this list from P2_CustomFields_Detail): " + CustomFieldsLibrary.printArrayList(arlist_dynCompnts) + "</div>"

    If Page.IsValid Then
        CustomFieldsLibrary.deleteAllDynamicFieldsExceptThisList(lnCurrent, 13, -1, arlist_dynCompnts, (New SqlConnection((New ConnInfo).GetConnString)), True)
        UpdateDatabase()
        If Not Trace.IsEnabled Then Server.Transfer("viewTask.aspx?taskgrpid=" & lnCurrent & "&taskgrpcategory=13&ptaskgrpid=" & Request.QueryString("ptaskgrpid") & "&ptaskgrpcategory=" & Request.QueryString("ptaskgrpcategory") & "&refresh=true")
    Else
        populateDropDowns()
    End If

    If Trace.IsEnabled Then cGK_testSpot.Text += "<div style='border: solid thin green;'>printArrayList: " + CustomFieldsLibrary.printArrayList(arlist_dynCompnts) + "</div>"
    'DEBUG: 
    If Trace.IsEnabled Then cGK_testSpot.Text += "<br>Button1_Click() ENDED</div>"
End Sub

So basically, I am looking for what was done incorrectly here as to why the page is giving me errors, and once these are resolved, I will need the onclick to also call a javascript function either on the same page, which then will call the parent, or the parent directly. Thank you for your help.

Upvotes: 0

Views: 2634

Answers (2)

David
David

Reputation: 218877

Use OnClientClick instead of OnClick.

An <asp:button> is a server-side element, so it has its own ASP.NET-specific server-side attributes which are processed, well, server-side. So OnClick isn't what you think it is in terms of regular HTML elements. The compiler is looking for that code on the server, and it's not there.

OnClientClick, on the other hand, is just treated as a string and isn't evaluated by the compiler. (Except to emit the onclick HTML attribute in the output.) It assumes that there will be client-side code to handle it.

Upvotes: 2

dreamerkumar
dreamerkumar

Reputation: 1550

Try changing the OnClick to onclick (small letters). Since the tag has a runat="server" attribute, it is a server control and OnClick will then refer to a server side event that has to be defined in Code Behind.

Upvotes: 0

Related Questions