0

I've got an issue with a messagebox user control. I wish for a control which can be given a message and the user can dismiss with a click of a button, which can be inserted into many places. I have applied the javascript into the messagebox control in a hope i can keep everything to do with the messagebox centralized, however when browsing to a page with the messagebox control added i get this error:

CS1061: 'ASP.components_messagebox_ascx' does not contain a definition for 'HideBox' and no extension method 'HideBox' accepting a first argument of type 'ASP.components_messagebox_ascx' could be found

The control is as thus:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Messagebox.ascx.cs" Inherits="FosterNetwork.Components.Messagebox" %>
<script type="text/Javascript">
    function HideBox() {
        document.getElementById("PNL_Messagebox").setAttribute("visible", false);
    }
</script>
<asp:Panel ID="PNL_Messagebox" runat="server">
    <asp:Label ID="LBL_Message" runat="server" />
    <asp:Button ID="BTN_Ok" Text="Ok" OnClick="HideBox()" runat="server" /> <!--Error happens on this line-->
</asp:Panel>

I'm fairly certain i've done this right but obviously i've done something wrong if it's not working. Any light on the situation at all would be grand.

Addendum: If i comment out the Button control the page loads fine, and the script loads fine too (Viewed page source)

2 Answers 2

1

The control ID's you're referencing are not the client ID's, but server ID's. So retrieve the 'ClientID' from the control in the JavaScript function and second, use the 'OnClientClick' property to show the JavaScript message.

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Messagebox.ascx.cs" Inherits="FosterNetwork.Components.Messagebox" %>
<script type="text/Javascript">
    function HideBox() {
        document.getElementById("<%= PNL_Messagebox.ClientID %>").setAttribute("visible", false);
    }
</script>
<asp:Panel ID="PNL_Messagebox" runat="server">
    <asp:Label ID="LBL_Message" runat="server" />
    <asp:Button ID="BTN_Ok" Text="Ok" OnClientClick="HideBox()" runat="server" /> <!--Error happens on this line-->
</asp:Panel>
Sign up to request clarification or add additional context in comments.

2 Comments

Bloody hell. Forgot about bloody OnClientClick. The serverID i completely didnt realise about. Would you be able to post an article where i can read up about the code you popped in getElemenyById so i dont fumble and fail next time :D, Will be accepting your answer when the timer bloody allows. ¬_¬
Sure, for the ClientID see: msdn.microsoft.com/en-us/library/…. For the inline tags (<%= is a simplified notation for Response.Write) see: naspinski.net/post/…. Good luck.
1

Onclick looks for a server side function, and not javascript. either, define your button as <input type='button' onclick='HideBox' or change the current code to:

<script type="text/Javascript">
function HideBox() {
    document.getElementById("<%= PNL_Messagebox.ClientID %>").setAttribute("visible", false);
    return false;
}
</script>
<asp:Button ID="BTN_Ok" Text="Ok" OnClientClick="return HideBox()" runat="server" />

returning false in OnClientClick, prevents the asp button from postback.

Edit: as Monty mentioned, your panel control's client id is not correctly set in your code.

1 Comment

@Skintkingle: There's a great tutorial about "webforms inline markup language" at naspinski.net/post/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.