C# Button Click - c#

I have the following button on a .aspx page:
<asp:Button runat="server" ID="bntLogin" OnClick="bntLogin_Click" Text="Login" />
With the following in the .aspx.cs:
protected void bntLogin_Click(object sender, EventArgs e)
{
//
}
When I try to build it I get the following error:
'ASP.reserve_aspx' does not contain a definition for 'bntLogin_Click' and no extension method 'bntLogin_Click' accepting a first argument of type 'ASP.reserve_aspx' could be found (are you missing a using directive or an assembly reference?)
However, when I move the click event from the code-behind to a script block inside the markup it builds.
Any ideas?

Are you sure that you placed the method in the correct code-behind file?
Check which file you are using as your code-behind file by looking at the #page directive at the top of the aspx file itself (check the inherits attribute) - you may be surprised.
A loosely-related side note: By setting the OnClick with a string value that corresponds to the method you wish to invoke you are implicitly relying on ASP.NET's AutoEventWireup feature which I don't consider to be a good approach. It is better to manually wire up your controls in your page's OnInit override method like this:
bntLogin.Click += bntLogin_Click;
By relying on AutoEventWireup you are allowing the ASP.NET runtime to create this code for you and since this happens at execution time you are incurring an execution time performance penalty as well as risking an exception like the one you are seeing now.

I usually hook up the events in the code-behind's OnInit method, which is a hold-over from ASP.NET 1.1 (that's where the designer would put it back then.)

If by any chance you're still running ASP.NET 1.1, delete the aspnet_client folder, rebuild and try again.

Related

Invalid postback or callback argument. Event validation is enabled using in configuration or <%# Page EnableEventvalidation =”true” %>

I am getting the below detailed error:
Invalid postback or callback argument. Event validation is enabled using in configuration or <%# Page EnableEventvalidation =”true” %> in a page. For security purpose, this feature verifies that arguments to postback or callback events originate from the server that originally rendered them.
I am having a webpart page with a webpart where i do have a UPdatePanel and a button Upon Button click i am getting this error.
the same package(wsp) which we are using in one of our environment is working fine and the same is not working in another environment.
Help in this is highly appreciable.
Without code it will be incredibly difficult to diagnose your issue.
You mentioned that your utilizing an Update Panel, which in essence will automatically store your page in memory, then it will render the proper Ajax to hide the Postback. The Microsoft Developer Network (MSDN) basically warns you when you use the EnableEventValidation with Client-Side modification.
If you write client script that changes a control in the client at run
time, you might have to use the RegisterForEventValidation method in
order to avoid false event validation errors.
Also if you initialize out of your web.config and directly in code, you should ensure that you call the validation before the page has been initialized. So I would potentially use:
protected void Page_PreInit()
{
// Before Page Initialization
}
Which would potentially eliminate any errors from the front-end, without code can't be of more assistance.
Ok without some code to go on I'll fire some rounds off into the dark, I know next to nothing about SharePoint so I maybe missing something obvious here:
Firstly, check your environments, one may have a different web.config which has EnableEventValidation set to false.
Secondly on Page_Load are you doing any DataBind? Depending on whatever your code is doing you may need make sure it's not binding on PostBack - or if it is already; try binding on PostBack.
Thirdly; any html or < > characters being included anywhere in the form? Also line breaks in data (my memory is a bit hazy on this one) I seem to remember, something like:
<option value="
my value
">select this</option>
Could cause it too. So doing a Trim() on data is recommended.
Lastly: Tag bleed out, check none of your tags are unclosed, something like <span <asp:TextBox [...] /> this could be the cause too.
add EnableEventValidation="false" in page directive <%# Page EnableEventValidation="false" %>

Using html <a> tag instead of LinkButton, can't access c# code-behind

I'm tryna create an ASP.NET Empty Web application that allows users to sign up and log in to access different pages from the public. I'm currently using a masterpage that holds the navigational links, using the html tag. However, for one of my buttons it is used to Sign Out of the user's account to redirect them back to the page where the public that isn't logged in are able to access. However, to use a code behind, it'll probably require me to use a LinkButton to access the c# code behind for my sign out code, which is
FormsAuthentication.SignOut();
Response.Redirect("Index.aspx");
However, if I use a LinkButton it is required for me to use a tag, which screws up my page layout (the navigational links). Is there a way I can use the tag and still access my c# code behind to implement the codes I've pasted above?
Anyway I tried this
<a id="signOutBtn" onclick="signOut_Click" onserverclick="signOut_Click" runat="server">Sign Out</a>
but I got this error:
Compiler Error Message: CS1061: 'ASP.masterpageuser_master' does not contain a definition for 'signOut_Click' and no extension method 'signOut_Click' accepting a first argument of type 'ASP.masterpageuser_master' could be found (are you missing a using directive or an assembly reference?)
Try runat="server":
<a runat="server" id="signOutBtn" onserverclick="signOut_Click">Sign Out</a>
And in your code behind, define your function:
public void signOut_Click(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
Response.Redirect("Index.aspx");
}

DotNetNuke DDRMenu set NodeSelector in code behind

I'm using DDRMenu in DotNetNuke to select a menu node from my site structure and display only a subnode in a specific navigation in my template
<%# Register TagPrefix="dnn" TagName="MENU" Src="~/DesktopModules/DDRMenu/Menu.ascx" %>
<dnn:MENU ID="MenuFooter" MenuStyle="MenuFooter" IncludeHidden="true" NodeSelector="FooterNavigation,0,1" runat="server" ></dnn:MENU>
Now I want to be able to set the NodeSelector attribute in the code behind file, because I want to be able to dynamically set the value on Page_Load
// load footer navigation node from a config file
protected void Page_Load(object sender, EventArgs e)
{
var footerNode = Config.Instance.Navigation.FooterNode;
MenuFooter.NodeSelector = footerNode + ",0,1";
}
But this doesn't work, as there is no NodeSelector attribute on System.Web.UI.UserControl.
Error 'System.Web.UI.UserControl' does not contain a definition for 'NodeSelector' and no extension method 'NodeSelector' accepting a first argument of type 'System.Web.UI.UserControl' could be found (are you missing a using directive or an assembly reference?) C:\Projects\eWolf2012\dev\DNN\Portals_default\Skins\JWEwolfSkin2012\Simple.ascx.cs 141 24 JWEwolfSkin2012
Is there any way to achieve this?
Kind regards
Usually the Menu.ascx in DDRMenu inherits from the DDRMenu SkinObject:
<%# Control Language="C#" AutoEventWireup="false" EnableViewState="false" Inherits="DotNetNuke.Web.DDRMenu.SkinObject" %>
Since you are talking about changing the code behind I guess that you are using a custom control that embeds the Menu.ascx. In which case you should be able to access the NodeSelector property since it exists in the SkinObject class.
What I am suspecting is happening is that your control type is not loaded correctly by the designer, and that it falls back on the UserControl type which doesn't have the NodeSelector property.
Try the following:
Include the DDRMenu assembly in your current project (because it won't load the type if it doesn't find the assembly), then rewrite the include to kick the designer into motion. I'm pretty confident this is the cause of the problem, but if not:
Fiddle with your src attribute and check in the *.designer file what type is defined.
Define it manually in your code-behind file instead of letting the designer do it.

Derived class in UC gives "type exist in both dlls" error

User Control has a class ImButtonLink derived from Image button.
protected void reserv_Click(object sender, ImageClickEventArgs e)
{
Response.Redirect(((ImButtonLink)sender).GetURL);
}
In Click Action function I get following error:
The type 'ImButtonLink' exists in both 'App_Web_carstable.ascx.6bb32623.yi5kff3g.dll' and 'App_Web_carstable.ascx.6bb32623.urge3_if.dll'
Any ideas? My derived class has only string parameter in addition to ImageButton class for storing navigation link. Maybe there's another way to store link in ImageButton?
I guess the control is CodeFile (and not Codebehind) mode, or included in a CodeFile page. You may transform it to CodeBehind and rebuild your solution.
If you want to keep it CodeFile, you may set batch="false" in the compilation element of your web.config like this :
<compilation defaultLanguage="c#" batch="false" ...>
Be aware that there are performance issues when a page is first accessed.
And don't forget to delete your temporary asp.net files (you may need to stop your web server)
You may also have a look at this, very interesting solution though I never had the opportunity to test it by now :
https://stackoverflow.com/a/15253750/1236044

ASP.net CS1061 Compilation Error on deployment

I'm having a bit of a pain here and I just can't figure out what's wrong.
I have an ASP.net project which I deployed on a server. At first everything seemed allright, no Errors whatsoever. However, as a last adition I wanted to add a search function to a fairly large list so I added the following syntax to my mark-up:
<td>
Search Server:
<asp:TextBox ID="txtSearch" runat="server" />
<asp:Button ID="btnLookup" runat="server" OnClick="btnLookup_Clicked" Text="Search" />
<asp:Label ID="lblFeedback" runat="server" />
</td>
and the following in code behind:
protected void btnLookup_Clicked(object sender, EventArgs e)
{
lblFeedback.Text = "";
Session["IsSearch"] = true;
LoadServerList();
}
When I run this locally it works just fine as I expect.
HOWEVER!
When I copy these files to the server I get a compilation error:
Compiler Error Message: CS1061: ' ASP.ntservice_ reports_ reports_ serverlist_ manage_ aspx ' does not contain a definition for 'btnLookup_ Clicked' and no extension method 'btnLookup_ Clicked' accepting a first argument of type 'ASP.ntservice_ reports_ reports_ serverlist_ manage_ aspx' could be found (are you missing a using directive or an assembly reference?)
it says there is nothing that handles my Clicked event although it does work when I run it through Visual studio.
any ideas?
EDIT:
What I tried myself is
renaming button
deleting and readding the button
add through designer
renaming click event
removing the event from markup allows normal execution ... :/
Is your project a web site or a web application? I would guess that it's a web application project and that perhaps you don't have the latest DLL deployed from the bin folder. Check the versions between your machine and the server to verify that they are the same.
I think this error is generated when you change an object Name, ex.
Text1 -» txtSerialNo I had the same problem but I could fix it. Here is the solution:
Go to Split Mode, click on the textbox/object, in the code remove the line
[**ontextchanged="txtSerialNo_TextChanged"**]
From:
[<asp:TextBox ID="txtSerialNo" runat="server"
ontextchanged="txtSerialNo_TextChanged"></asp:TextBox> //remove this line]
To:
[<asp:TextBox ID="txtSerialNo" runat="server"</asp:TextBox>]
I hope it works for you. Bless you.
I experienced same problem. And I made the event handler method public, problem solved!
Thanks!
Did you deploy the dll?
Do you set
<%# Page ... Language="C#" CodeBehind="... .aspx.cs" Inherits="..." .../>
in your page directive correctly?
This should fix it if you did everything else right:
Try to make an entirely new click event (using the split view, just to be sure you get the method signature right.)
Then copy your code from your previous clicked event into the newly created event, copy the new event's method name to your original button and see if it works.
Try renaming the event handler to some thing OnClick="buttonLookup_Clicked"... and changing the event handler signature to match it.
having the same issue myself, I made the method public so that removed the compiler error, then followed that with including everything within
<form runat="server">
.....
<asp:Button id... OnClick="MethodName"/>
</form>
Hope this helps
Make the events your onclick events point to public.
This can happen if the event is signed-up for programmatically AND also in the markup (via the properties)
go to Build ==> Clean Solution ==> Run (f5)
I just restarted my ASP.NET Development server then its running again.
I experienced the same problem with you, but I fix it by include CodeFile="xxxx.aspx.cs" into my page directive, hope this help.
Right Click on the form which affected and go to properties, change the Build Action to Compile. typically this error comes when the Build Action changed to resource.
In my case this issue occurs when there are two projects in solution, and the second project has reference of first project dll.
When you add new members to the class in first project make sure you build the first project first and then second project will compile successfully

Categories