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.
Related
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.
I seem to be having a problem with my button's OnClick, it doesn't even react at all to the event. It responds to OnClientClick and executes the code, but when I try to relate it o a function in my .aspx.cs class it just ignores it, and it should be executing very simple code (writing to the output debug)
I've checked every other stackoverflow post about this I could find and no one had the same issue.
I know that my Index.aspx is executing the code inside Index.aspx.cs because the Page_Load function is outputting to the debug window, so that's not the problem.
Here's the important code:
<asp:Button ID="btnCreateOrder" runat="server" Text="Create Order" OnClick="btnClick" />
protected void btnClick(object sender, EventArgs e)
{
System.Diagnostics.Debug.Write("Testing3");
}
Like I said the Page_Load function is working fine so it's not an issue of them not being connected.
DIFFERENT PROBLEM IF YOU FEEL SO INCLINED:
While you're reading, I also have a problem of not being able to find objects from my HTML in my CS file.
What I mean by that is, if I reference this label:
<asp:Label ID="testLabel" runat="server" Text="test"></asp:Label>
by saying
testLabel = "Test2";
I get the error "The name 'testLabel does not exist in the current context"
And if I try to reference it by saying this:
Index.testLabel = "Test2";
I get "'MyProject.Views.Home.Index' does not contain a definition for 'testLabel'" even though it clearly does.
Maybe the .designer file is bad.
Try this: delete the .designer.cs file of your .aspx page, right-click on it and select the 'Convert to web application' option.
EDIT: As for the Button event, how did you generate the method stub for the click event? Did you click the button on the designer view twice and the visual studio generated id for you or did you handcoded it?
Try to delete both the button and the button click event, and when you declare the button again, double click it on the designer view.
I'm currently working on a website project and I'm almost done, except that I need to get my searching to work.
I would like it to work like this: On my masterpage there is a asp:textbox and a asp:button. When I type in a search word and click on my button I would like it to redirect to a search.aspx page - The problem is, that I don't know how to do that. I only got the method and it looks like this
public DataTable Search(string Keyword)
{
return db.GetData(
"SELECT fldTitle, fldLang, fldCode from tblSnipets LIKE #1",
"%" + Keyword + "%");
}
From there I don't know what to do.
Using classic ASP.NET and PostBack event without any patterns it will look like this:
1 - Add an event handler to the button click event.
<asp:Button id="Button1" Text="Search" OnClick="SearchBtn_Click" runat="server"/>
2 - Add SearchBtn_Click handler to the code behind file of your page and do a redirect to your Search page.
it will look like this:
void SearchBtn_Click(Object sender, EventArgs e)
{
}
3 - In this event handler write code that will redirect to your Search.aspx with parameters of your search criteria:
Response.Redirect("~/Search.aspx?criteria=" + Server.HtmlEncode(myTextBox.Text));
or close to this statement (check the MSDN)
4 - On the Search.aspx code behinf page in the Page_Load handler catch the parameters and call your method to get the data.
This is not the best solution, but it should work.
i have a page where on click of a button, a javascript function runs. It then aggregates some data and places the data on a hidden field in this page. It then opens a new window. This new window picks up this aggregated data like so :-
$('#accepted').val(window.opener.$('#accepted').val());
where accepted is the hidden field in both parent and child window (no runat="server" was used). The issue now is that i require this data to databind two grids. Currently I've done a doPostback on both grids, but what i really want to do is doPostback for the form once and handle the databinding the PageLoad event. So two questions :-
1) How do i doPostback the form?
2) How do i do this while still being able to differentiate from the actual form submission?
To post the form you should just be able to add a call to __doPostback in your javascript, after the accepted field is set. You can use the EventTarget and EventArgument parameters of the __doPostback to control the binding in your grid.
So, you could put this in your js:
__doPostback('rebindGrid', '');
and then this in your page load event:
if (Request.Form["__EVENTTARGET"] == "rebindGrid")
{
//....Do so stuff
}
In order to tie it in more directly with the postback model I wrap mine with some C#
C# Extension Method
public static string GetPostBackLink (this Control c, string argument = "") {
return c.Page.ClientScript.GetPostBackEventReference(ctl, argument, true) + ";";
}
ASPX
<asp:LinkButton id="lnkDoThis" runat="server" onclick="lnkDoThis_Click"
style="display: none;"></asp:LinkButton>
<asp:HiddenField id="hdnParamHolder" runat="server" />
JS
function DoSomething(param) {
$("[id$='hdnDealTemp']").val(param);
<%= lnkDoThis.GetPostBackLink() %>
}
CodeBehind
protected void lnkDoThis_Click (object sender, EventArgs e) {
var myParam = hdnParamHolder.Value;
// Do server actions here
}
As for the opening in a second window ... I am not sure I follow when you want this to happen? If it is after the postback you will need to read from the hdnParamHolder control when the page reloads.
I'm working with a legacy project in C# (.NET 2.0). In this project there are two validationgroups. One for custom login control and one for users to submit to a newsletter. The problem I ran into is that when a user submits to subscribe to a newsletter some custom code is triggered in the page_prerender() method which only should be triggered when a user tries to login.
I have been looking for a solution to recognize which of the two groups is used on postback so I can ignore the custom code when needed. My idea was to try and check which of the two validation groups is being used to validate. Unfortunately after spending a fruitless few hours on google I've not been able to find anything to let me know how to actually known which validationgroup is used when validating. Is there any way to find out?
<asp:Button ID="btn_newsletter"
runat="server"
Text="Verzend"
ValidationGroup="newsLetter"
meta:resourcekey="bnt_newsletter"
OnClick="handleNewsLetter"
CssClass="roundedButtonBig"
/>
<asp:Button ID="LoginButton"
runat="server"
CommandName="Login"
Text="Inloggen"
ValidationGroup="lgnUser"
meta:resourcekey="LoginButtonResource1"
CssClass="roundedButtonBig"
/>
The following code should only trigger when the LoginButton is pressed and it needs to be done on Pre_render(). Or alternatively pass the correct ValidationGroup (where now null is passed).
protected void Page_PreRender(object sender, EventArgs e)
{
//Register custom ValdiationErrorService added errors to JavaScript so they can be added into the popup.
ValidationErrorService.RegisterServerValidationMessageScript(Page, null);
}
to check which validation group is valid, call:
Page.Validate(“newLetter”);
then check
Page.IsValid;
this will return the value. Scott Gu has more on his blog
edit you are also wanting to know which button was clicked within the prerender event it sounds like as well. While you can't find that out from the parameters passed into the page prerender, you can rely on the button events occuring prior to the page_prerender event. within the aspx pages code behind, create a member variable. this variable will be used to denote if the prerender logic should be executed.
next, within the click events of the two buttons, set that local variable to denote if that button should fire the logic you want in the page_prerender event.
last, check your local variable within the page_prerender method, and encapsulate your logic within an if statement based upon your new member variable.
Happy Trails!