Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hey,I like to open my popup window, when someone clicks on the button by call my JS method OpenDialogToTest(). When I added my method OpenDialogToTest() and CloseDialogToTest() in the onClick of the button, I get the Error CS1061 'ASP.default_aspx' does not contain a definition for 'CloseDialogToTest' and no extension method 'CloseDialogToTest' accepting a first argument of type 'ASP.default_aspx' could be found (are you missing a using directive or an assembly reference?)

This would be my current code.
Hope someone could help me!

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<script type="text/javascript">
$(document).ready(function () {
var dlg = $("#dialog-test").dialog({
autoOpen: false,
width: 600,
modal: true,
closeOnEscape: true
});
});

function OpenDialogToTest() {
$("#dialog-test").dialog("open");
}

function CloseDialogToTest() {
$("#dialog-test").dialog("close");
}
</script>

<div id="dialog-test" title="Test">
<asp:Button runat="server" ID="btnTest" Text="runs" OnClick="CloseDialogToTest()"/>
</div>

<div class="row welcome">
<div class="col-md-12 text-center">
<h3><b>Welcome to my page</b></h3>
<asp:Button runat="server" ID="btnOpenTest" Text="Start" OnClick="OpenDialogToTest()"/>
</div>
</div>
</asp:Content>

What I have tried:

I tried to use OnClientClick instead of OnClick, now my window appears and disappears immediately again.
Posted
Updated 3-Mar-21 22:07pm
Comments
F-ES Sitecore 12-Sep-18 4:49am    
You're mixing client-side and server-side calls and code. You can't call js functions from your .net code and you can't call .net methods from your js code.
Member 13981467 12-Sep-18 22:28pm    
Thanks, that solved my problem

You are getting that error because the compiler can't locate or find the CloseDialogToTest event. The OnClick event an ASP.NET Button Control is a Server event. That means, when you declare your markup like this:

ASP.NET
<asp:Button runat="server" id="btnTest" Text="Log In" OnClick="btnTest_Click" />


The matching event handler in your C# code would look something like this:

C#
protected void btnTest_Click(object sender, EventArgs e)
{
    // Do server side stuff here
}


If you are trying to call a JavaScript function then use the OnClientClick event of the Button instead like this:
ASP.NET
<asp:Button runat="server" ID="btnOpenTest" Text="Start" OnClientClick="OpenDialogToTest(); return false;"/>


The return false; line after calling your JavaScript function will prevent the Button to cause a full postback.
 
Share this answer
 
Look at the error message:
CS1061 'ASP.default_aspx' does not contain a definition for 'CloseDialogToTest' and no extension method 'CloseDialogToTest' accepting a first argument of type 'ASP.default_aspx' could be found (are you missing a using directive or an assembly reference?)
It can't explain itself much more clearly: the method you are calling - CloseDialogToTest - does not exist in that page's ASPX / C# code.

Check the ASPX file, see what methods it does declare, and either write the method or check it's parameters match a Click Event handler signature. Remember that C# is case sensitive, so upper and lower case letter must match exactly.
 
Share this answer
 
Comments
Member 13981467 12-Sep-18 3:51am    
I had already the same function, etc in the page and just duplicated it and change the names. Could the other dialog work and my new one not?
The code I have posted is my aspx code and I don't thought that I have to change something in my c# code, because I don't like to do my code behind something, when I open the window.
Sorry for my dumb questions, I don't have much experience with asp.net
Vincent Maverick Durano 13-Sep-18 15:18pm    
The issue is not with the naming. The error indicates that the function or method CloseDialogToTest() doesn't exist as the OnClick event tries to look for the corresponding Server event handler. Your CloseDialogToTest() function is a Client-side function and so it errors. Please check the solution I've provided.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900