C# Errors in ASP.NET - c#

Server Error in '/asppub' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1061: 'ASP.mis3200_unit4_ringu4pos_aspx' does not contain a definition for 'rblSnacks_SelectedIndexChanged' and no extension method 'rblSnacks_SelectedIndexChanged' accepting a first argument of type 'ASP.mis3200_unit4_ringu4pos_aspx' could be found (are you missing a using directive or an assembly reference?)
Source Error:
Line 102: Would you like any snacks?</td>
Line 103: <td class="style7">
Line 104: <asp:RadioButtonList ID="rblSnacks" runat="server"
Line 105: RepeatDirection="Horizontal" Width="96px" AutoPostBack="True"
Line 106: onselectedindexchanged="rblSnacks_SelectedIndexChanged">
Source File: c:\Users\Ryan\Desktop\asppub\MIS3200\Unit4\RingU4POS.aspx Line: 104
The given error message in my coding class was this:
Snacks: toggle visiblity based on yes/no selection: -2
The list of snacks should only be visible when somebody selects Yes (otherwise, it should be invisible). You needed to double-click the RadioButtonList to create a method which had functionality to toggle the visibility property of the CheckBoxList. For this method to work, you must enable AutoPostBack for the RadioButtonList. "
What am I doing wrong? Any help would be appreciated.

You have missed this in server side
protected void rblSnacks_SelectedIndexChanged(object sender, EventArgs e)
{
}

Add this code in your code behind
protected void rblSnacks_SelectedIndexChanged(object sender, EventArgs e)
{
// some code
}

You should define a rblSnacks_SelectedIndexChanged method in a code behind that will handle the SelectedIndexChange event for the radio button. And if you do not need it, you should remove it.

As the error suggests, you have to have the SelectedIndexChanged in your code behind. So add this code,
protected void rblSnacks_SelectedIndexChanged(object sender, EventArgs e)
{
//your code;
}

Related

Can´t reach function when onclick is located in Site.Master

Coding a page in asp.net webforms, I put a button and textbox in site.master file.
I want to do a specific action (defined in a method) when I click in button, so I put this method in site.master.cs. This method, besides other things, take the value of textbox.
I can´t reach the method when I am clicking in it.
Am I coding it in a proper way?
Are there another way to do it?
This is the relevant code.
site.master
<input id="mailListaD" type="text" value="Introduce tu Email…" runat="server"
onfocus="this.value=(this.value=='Introduce tu Email…')? '' : this.value ;" />
<asp:Button id="news_go" Text="GO" OnClick="listaDistribucion_Click" runat="server"/>
site.master.cs
protected void listaDistribucion_Click(object sender, EventArgs e)
{
string query = "INSERT INTO listaDistribucion VALUES(''," + mailListaD.Value +")";
ExecuteQuery(query);
Close();
}
The error when I compile the proyect is:
error CS7036: There is no argument given that corresponds to the required formal parameter 'sender' of 'SiteMaster.listaDistribucion_Click(object, EventArgs)'
Try generating the event from the IntelliSense, delete the event that is attached to the button and then write OnClick=.. when intellisense appears click Create New Event and in the code behind you get an event that has the name of the id of the button and onclick appended to it.
Also check if you have the correct code behind page in your masterpage in the first line above CodeBehind="MyPage.aspx.cs", check if the name of your code behind is correct.

Onclick event not working/redirecting

This is the first webservice I have made and I am having issues with my current functionality. My button in Visual Studio will only accept OnClientClick without the error below. My button is defined as such in my .aspx:
<asp:Button ID="btnSearch" runat="server" OnClick="btnSearch_Click" Text="Search">
The aspx.cs file contains this:
protected void btnSearch_Click(object sender, EventArgs e)
{
Response.Redirect("www.google.com"); //just for a testing
}
The error thrown is:
CS1061: 'ASP.main_aspx' does not contain a definition for
'btnSearch_Click' and no extension method 'btnSearch_Click' accepting a
first argument of type 'ASP.main_aspx' could be found
I have looked at various posts about onclick events and nothing I have tried has been the solution. Any ides on why when I click my button it doesn't redirect? Also I would like to use OnClick rather than OnClient.
As per discussion in comment, issue is here:
CodeBehind="Main.aspx.cs
Change it to
CodeFile="Main.aspx.cs"
This will solved your error.

Can't figure out what I'm doing wrong ASP.NET C#

Server Error in '/asppub' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1061: 'ASP.mis3200_unit4_ringu4l2_2_aspx'
does not contain a definition for 'cblFees_SelectedIndexChanged' and
no extension method 'cblFees_SelectedIndexChanged' accepting a first
argument of type 'ASP.mis3200_unit4_ringu4l2_2_aspx' could be found
(are you missing a using directive or an assembly reference?)
Source Error:
Line 112: </p>
Line 113: <p>
Line 114: <asp:CheckBoxList ID="cblFees" runat="server"
Line 115: onselectedindexchanged="cblFees_SelectedIndexChanged" RepeatLayout="Flow"
Line 116: ValidationGroup="L2.2">
Source File: c:\Users\Ryan\Desktop\asppub\MIS3200\Unit4\RingU4L2.2.aspx Line: 114
You need to declare the event handler cblFees_SelectedIndexChanged in code behind, if you are not using it simple delete onselectedindexchanged="cblFees_SelectedIndexChanged" from the asp:CheckBoxList tag.
Usually asp:CheckBoxList does not do the postback and does not require selectedindexchanged event.
The compilation error is coming because, the compiler is trying to find out the event handler or method namely cblFees_SelectedIndexChanged. Seems like you have not added this in your aspx.cs class. If you are not using it delete it from the code else define the handler for the same.
If you don't have the cblFees_SelectedIndexChanged in your code behind, delete it from the checkbox tag. Then click in between the quotes of your OnSelectedIndexChanged and press Ctrl_Spce in your keyboard. It shows you a list of events. Choose New event from it. The event will be created in your code behind.

Trying to fire a method in code behind on button click

I am working on an ASP.NET project, I have a button that I want to fire a method in code behind to update database with updated client data when clicked.
This is my button:
<asp:Button ID="button1" runat="server" OnClick="submitChanges_OnClick" Text="Submit" />
Code behind:
private void submitChanges_OnClick(object sender, EventArgs e)
{
//Code here
}
On page load I get this error message:
Compiler Error Message: CS1061: 'ASP.account_heatingcontrols_aspx' does not contain a
definition for 'submitChanges_OnClick' and no extension method 'submitChanges_OnClick'
accepting a first argument of type 'ASP.account_heatingcontrols_aspx' could be found
(are you missing a using directive or an assembly reference?)
Can anybody tell me what I am doing wrong here, I have googled, but no luck.
Any help would be great!
Change private to protected.
protected void submitChanges_OnClick(object sender, EventArgs e)

C# Button Click

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.

Categories