G'day,
I'm using PreviousPage to get values from controls from another page like this:
sPORyear1 = PreviousPage.year1.SelectedItem.Text.ToString();
It works just fine, however I noticed that the events from C# Code Behind (ex. Button click) are not handled properly. I tried making a breakpoint inside the event and it doesn't stop.
After looking at my aspx I noticed this:
AutoEventWireup="true"
When I set that to false, the buttons work properly, but the PreviousPage functionality doesn't work anymore.
The button code:
<asp:Button ID="export" runat="server" onserverclick="export_Click" Text="Export" />
Thanks!
When the AutoEventWireup is set to "False" Change the Page load Handler signature as below for the another page in which you are trying to access the value of previous page
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
Please note that the access modifier should be protected not Private in case of Autoeventwireup = "False" Otherwise you will not be able to capture the OnLoad event
this fixed it
Button On Click event not firing
Related
So in ASP.NET I would simply do this:
<asp:ListBox OnSelectedIndexChanged="UpdateModels" runat="server" > </asp:ListBox>
But my listbox is inside an .ascx file. which is registered and included in my actual webpage massupdate.aspx like this
<%# Register TagPrefix="mass" TagName="make" Src="~/DynamicData/Make.ascx" %>
<mass:make id="makeControl" runat="server"/>
and in my make.ascx.cs I have this
public ListBox getlistbox()
{
return DropDownList1;
}
So I have access to the original listbox, but I don't know how to replicate the first code snippet in C# in my codebehind for mass.update.aspx .
First of all, the event registered in the ascx should fire even though it is registered as a control onto the page. Events should bubble up (or maybe down?) through all of the controls on a page, that's part of the "magic" of webforms.
Secondly, if you do need to set an event handler in code, you can either google "add event handler .NET control" or you can type something similar below and Visual Studio intellisense should auto complete it for you after you type the "+=" and you can take it from there.
makeControl.getlistbox().OnSelectedIndexChanged += this.Index_Changed
void Index_Changed(Object sender, EventArgs e) {
// whatever you need to do on the event
}
I’m having trouble with my DropDownList. The events don’t fire! I've tested it on a separate project, with a DropDownList and a literal. Every time the selected value would change, I would add a little star “*” to the literal. No problems what so ever. But every time I try it on my webpage in the project, it fails.
Here is an image.
protected void ddlConsole_SelectedIndexChanged(object sender, EventArgs e)
{
ltlTesting.Text += "*";
}
UPDATE:
I've tried some things but still with no succes. I hope someone can tell me what i'm doing wrong. I'm wiring the events in the code behind now, but i've added a linkbutton next to the dropdownlist to see if it works.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
ddlConsole.SelectedIndexChanged += new EventHandler(ddlConsole_SelectedIndexChanged);
lnkGet.Click += new EventHandler(ddlConsole_SelectedIndexChanged);
}
Here is an image to see what's going on. The stripe in the literal at the beginning is added in pageload with the same code the star is added. Just to be sure it doesn't load twice. The "GET" linkbutton works fine. The dropdownlist doesn't...
Have you Set
AutoPostBack="true"
in control properties??
EDIT:
Remove
OnSelectedIndexChanged="ddlConsole_SelectedIndexChanged"
from the markup in the ASPX page and try again only with AutoPostback true and the
event defined in codebehind. The aspx page should look like this:
<asp:DropDownList runat="server" ID="ddlConsole" AutoPostBack="True"></asp:DropDownList>
Is the AutoPostBack of the dropdownlist true ?
Check AutopostBack property of Dropdownlist set it to true :
If picture is right and the AutoPostBack="True", is there any code that sets the value of ltlTesting when page loads?
Add the AutoPostback="True" and OnSelectedIndexChanged="ddlConsole_SelectedIndexChanged" to the ddlConsole attributes. You can delete the OnInit method, since you bound the SeletedIndexChanged event at design time.
protected void Button1_Click(object sender, EventArgs e)
{
}
My application contains a update button. when I click the button the browser must automatically refresh the page.
Is there any code behind code for button?
there is none. Doing so will create a postback on your page, and so will "refresh" your page, at least everything that was done in the page load event and in the prerender event.
In a updatePanel in ajax, it will create a callback. So that only the updatePanel will undergo the postback on the client side, whereas the whole page cycle will be run on the server side.
with server-side button control - you cant.
not sure what are you trying to achieve, but here's a JS workaround for the issue (using JS location.reload() to reload the page):
<input type="button" value="refresh" onclick="javascript:location.reload()" />
Here is one way to do it
I followed the directions from MSDN on transferring data between asp.net pages.
But when using the 'PreviousPage' property to access the previous pages controls, I get a null ref exception that PreviousPage is not set to an instance of an object.
Here is my code:
public partial class Portal : System.Web.UI.Page
{
public string Username
{
get
{
return txt_User.Text;
}
}
And this is the submit button on initial page:
<asp:Button ID="btn_Submit" runat="server" onclick="btn_Submit_Click"
PostBackUrl="~/Query.aspx"
Previous page property on second page:
protected void Page_Load(object sender, EventArgs e)
{
Username = PreviousPage.Username;
}
As per MSDN instructions I also added this at the top of the second pages markup file:
<%# PreviousPageType VirtualPath="~/Portal.aspx" %>
Also note I have tried Server.Transfer to switch pages instead and that produces the same error.
EDIT, here is using Server.Transfer on the initial page click event:
protected void btn_Submit_Click(object sender, EventArgs e)
{
Server.Transfer("Query.aspx");
}
EDIT, button code without event handler:
<asp:Button ID="btn_Submit" runat="server"
PostBackUrl="~/Query.aspx"
style="height: 26px" Text="Submit" />
This works fine for me. If PreviousPage is null, that generally indicates the current page was not displayed as a result of a cross-page postback.
Can you confirm the error is being raised on the second page?
Also, what the onclick="btn_Submit_Click" in your button definition? There should be no code responding to the click event on the original page. Remember, it will be handled by the target page.
EDIT: Now that you've updated your question, my last point seems to be the issue. You are doing a server transfer from the original page. This is NOT a cross-page postback and that's why PreviousPage is null.
Remove the onclick attribute from your button and delete btn_Submit_Click.
I have a question about creating and managing events inside an ascx custom web control.
I have created a very stupid control consisting in a div containing a asp:Label control, it is a very simple structure:
<div id="mydiv" runat="server">
<asp:Label id="mylabel" text="Text"... />
</div>
That is, very simple.
I would like to add an event: clicked. I want to let the user add this control on the page and attach handlers to this event so that when this control is clicked it is possible to do something. It might seem a strange solution, it's like i'm trying to invent again the button control, that is: my custom button.
Well to fire the event I would like to add a javascript in my div and call a js function that calls, using ajax mechanism, a server side function.
Well how to call a server side function from here. I posted a question about how to call a server side function from a client side one and got some answers (many of them told me to use PageMethods), well it seems that pagemethod does not work, it compiles but when running and clicking on my control and executing the js (in the line PageMethods.mymethod()) here I have an error --> Java script exception: unrecognized method. It seems not finding the PageMethod.
Well, considering my objective, how can I do?
Ah, a solution like: use the click event in the label is not what I want because the click event must fire when I click in the div, consider that I might set a large padding so that a large empty space can provide a large clickable area.
Thanks in advance.
Create a Event in your User Control
E.g: -
public event EventHandler<CustomEventArgs> Click;
and a handler
public void OnClick()
{
if(this.Click !=null)
{
//what ever else you do
this.Click(new CustomEventArgs(...));
}
}
Also handle the mouse click (mouseleftbuttonup) on the User Control (and fire your event from there). i.e.
protected void OnMouseDown(...){ this.OnClick(); }
From the ASP.NET page, you can register the user control event and handle it:
MyControl.Click += new EventHandler<CustomEventArgs>(myctr_click)
protected myctr_click(object sender, CustomEventArgs e){
//do anything
}
PageMethods uses a web service within the page class; I assume you want a postback to the server and process this click in the page, right?
All controls that postback use a __doPostBack('clientid', '') method to postback to the server. Your label would need to do the same. Within the control, the control needs to implement IPostBackEventHandler to process these events, and raise the click event.
Check out this example: http://www.myviewstate.net/blog/post/2009/05/14/Implementing-IPostBackEventHandler-in-an-ASPNET-Control.aspx
I may have misinterpreted your requirements, but can't you just wrap the div in an asp:linkbutton?
<asp:LinkButton ID="lnkSomething" runat="server" OnClick="lnk_something_Click">
<div id="mydiv" runat="server">
<asp:Label id="mylabel" text="Text"... />
</div>
</asp:LinkButton>
and then on the server side:
protected void lnk_something_Click(object sender, EventArgs e)
{
}
As yellowfrog mentioned in the comments, you could then extend this by further wrapping it in an update panel for an async postback.